]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix(twofactor): avoid error in pgsql for duplicate entry 41213/head
authorBenjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
Mon, 18 Sep 2023 14:01:21 +0000 (16:01 +0200)
committerBenjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
Tue, 31 Oct 2023 14:51:32 +0000 (15:51 +0100)
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
lib/private/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDao.php

index 19d802185628c3e69759a072ed18cf45f8ff53cc..66bb9df8271fd4152eb7b7a0702e162b1b8f060a 100644 (file)
@@ -25,8 +25,6 @@ declare(strict_types=1);
  */
 namespace OC\Authentication\TwoFactorAuth\Db;
 
-use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
-use OCP\DB\QueryBuilder\IQueryBuilder;
 use OCP\IDBConnection;
 use function array_map;
 
@@ -70,25 +68,24 @@ class ProviderUserAssignmentDao {
         * Persist a new/updated (provider_id, uid, enabled) tuple
         */
        public function persist(string $providerId, string $uid, int $enabled) {
-               $qb = $this->conn->getQueryBuilder();
-
-               try {
-                       // Insert a new entry
-                       $insertQuery = $qb->insert(self::TABLE_NAME)->values([
-                               'provider_id' => $qb->createNamedParameter($providerId),
-                               'uid' => $qb->createNamedParameter($uid),
-                               'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT),
-                       ]);
-
-                       $insertQuery->execute();
-               } catch (UniqueConstraintViolationException $ex) {
-                       // There is already an entry -> update it
-                       $updateQuery = $qb->update(self::TABLE_NAME)
-                               ->set('enabled', $qb->createNamedParameter($enabled))
-                               ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
-                               ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
-                       $updateQuery->execute();
+               $conn = $this->conn;
+
+               // Insert a new entry
+               if ($conn->insertIgnoreConflict(self::TABLE_NAME, [
+                       'provider_id' => $providerId,
+                       'uid' => $uid,
+                       'enabled' => $enabled,
+               ])) {
+                       return;
                }
+
+               // There is already an entry -> update it
+               $qb = $conn->getQueryBuilder();
+               $updateQuery = $qb->update(self::TABLE_NAME)
+                       ->set('enabled', $qb->createNamedParameter($enabled))
+                       ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
+                       ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
+               $updateQuery->executeStatement();
        }
 
        /**