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

[stable26] fix(twofactor): avoid DB error on Twofactor (en/dis)abled event #41213

Merged
Merged
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
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
Loading