Skip to content

Commit

Permalink
Merge pull request #41213 from nextcloud/backport/fix/aborted_pgsql_t…
Browse files Browse the repository at this point in the history
…ransaction_on_error/stable26

 [stable26] fix(twofactor): avoid DB error on Twofactor (en/dis)abled event
  • Loading branch information
Altahrim authored Nov 15, 2023
2 parents f77bb96 + 69b4317 commit 1cdd501
Showing 1 changed file with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
namespace OC\Authentication\TwoFactorAuth\Db;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use function array_map;

Expand Down Expand Up @@ -70,25 +68,24 @@ public function getState(string $uid): array {
* 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();
}

/**
Expand Down

0 comments on commit 1cdd501

Please sign in to comment.