Skip to content

Commit 69b4317

Browse files
committed
fix(twofactor): avoid error in pgsql for duplicate entry
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
1 parent 6039510 commit 69b4317

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

lib/private/Authentication/TwoFactorAuth/Db/ProviderUserAssignmentDao.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
*/
2626
namespace OC\Authentication\TwoFactorAuth\Db;
2727

28-
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
29-
use OCP\DB\QueryBuilder\IQueryBuilder;
3028
use OCP\IDBConnection;
3129
use function array_map;
3230

@@ -70,25 +68,24 @@ public function getState(string $uid): array {
7068
* Persist a new/updated (provider_id, uid, enabled) tuple
7169
*/
7270
public function persist(string $providerId, string $uid, int $enabled) {
73-
$qb = $this->conn->getQueryBuilder();
74-
75-
try {
76-
// Insert a new entry
77-
$insertQuery = $qb->insert(self::TABLE_NAME)->values([
78-
'provider_id' => $qb->createNamedParameter($providerId),
79-
'uid' => $qb->createNamedParameter($uid),
80-
'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT),
81-
]);
82-
83-
$insertQuery->execute();
84-
} catch (UniqueConstraintViolationException $ex) {
85-
// There is already an entry -> update it
86-
$updateQuery = $qb->update(self::TABLE_NAME)
87-
->set('enabled', $qb->createNamedParameter($enabled))
88-
->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
89-
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
90-
$updateQuery->execute();
71+
$conn = $this->conn;
72+
73+
// Insert a new entry
74+
if ($conn->insertIgnoreConflict(self::TABLE_NAME, [
75+
'provider_id' => $providerId,
76+
'uid' => $uid,
77+
'enabled' => $enabled,
78+
])) {
79+
return;
9180
}
81+
82+
// There is already an entry -> update it
83+
$qb = $conn->getQueryBuilder();
84+
$updateQuery = $qb->update(self::TABLE_NAME)
85+
->set('enabled', $qb->createNamedParameter($enabled))
86+
->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
87+
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
88+
$updateQuery->executeStatement();
9289
}
9390

9491
/**

0 commit comments

Comments
 (0)