Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate inserts in the 2fa provider registry DAO #10462

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

/**
* @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
Expand Down Expand Up @@ -59,7 +59,7 @@ public function getState(string $uid): array {
$result = $query->execute();
$providers = [];
foreach ($result->fetchAll() as $row) {
$providers[$row['provider_id']] = 1 === (int) $row['enabled'];
$providers[$row['provider_id']] = 1 === (int)$row['enabled'];
}
$result->closeCursor();

Expand All @@ -72,15 +72,23 @@ public function getState(string $uid): array {
public function persist(string $providerId, string $uid, int $enabled) {
$qb = $this->conn->getQueryBuilder();

// TODO: concurrency? What if (providerId, uid) private key is inserted
// twice at the same time?
$query = $qb->insert(self::TABLE_NAME)->values([
'provider_id' => $qb->createNamedParameter($providerId),
'uid' => $qb->createNamedParameter($uid),
'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT),
]);
// First, try to update an existing entry
$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)));
$updatedRows = $updateQuery->execute();

$query->execute();
// If this (providerId, UID) key tuple is new, we have to insert it
if (0 === (int)$updatedRows) {
$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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,23 @@ public function testPersist() {
$this->assertCount(1, $data);
}

public function testPersistTwice() {
$qb = $this->dbConn->getQueryBuilder();

$this->dao->persist('twofactor_totp', 'user123', 0);
$this->dao->persist('twofactor_totp', 'user123', 1);

$q = $qb
->select('*')
->from(ProviderUserAssignmentDao::TABLE_NAME)
->where($qb->expr()->eq('provider_id', $qb->createNamedParameter('twofactor_totp')))
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter('user123')))
->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(1)));
$res = $q->execute();
$data = $res->fetchAll();
$res->closeCursor();

$this->assertCount(1, $data);
}

}