Skip to content

Commit

Permalink
Merge pull request #10943 from nextcloud/fix/2fa-provider-dao-concurr…
Browse files Browse the repository at this point in the history
…ent-insert

Fix handlng of concurrent inserts of the 2FA provider registry DAO
  • Loading branch information
MorrisJobke authored Aug 31, 2018
2 parents 3693d95 + fb98db7 commit 7159ac1
Showing 1 changed file with 9 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OC\Authentication\TwoFactorAuth\Db;

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

Expand Down Expand Up @@ -72,25 +73,7 @@ public function getState(string $uid): array {
public function persist(string $providerId, string $uid, int $enabled) {
$qb = $this->conn->getQueryBuilder();

$this->conn->beginTransaction();
// To prevent duplicate primary key, we have to first check if an INSERT
// or UPDATE is required
$query = $qb->select('*')
->from(self::TABLE_NAME)
->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
$result = $query->execute();
$rowCount = count($result->fetchAll());
$result->closeCursor();

if ($rowCount > 0) {
// There is 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();
} else {
try {
// Insert a new entry
$insertQuery = $qb->insert(self::TABLE_NAME)->values([
'provider_id' => $qb->createNamedParameter($providerId),
Expand All @@ -99,8 +82,14 @@ public function persist(string $providerId, string $uid, int $enabled) {
]);

$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();
}
$this->conn->commit();

}

Expand Down

0 comments on commit 7159ac1

Please sign in to comment.