@@ -36,61 +36,61 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | class ProviderUserAssignmentDao { |
| 38 | 38 | |
| 39 | - const TABLE_NAME = 'twofactor_providers'; |
|
| 40 | - |
|
| 41 | - /** @var IDBConnection */ |
|
| 42 | - private $conn; |
|
| 43 | - |
|
| 44 | - public function __construct(IDBConnection $dbConn) { |
|
| 45 | - $this->conn = $dbConn; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Get all assigned provider IDs for the given user ID |
|
| 50 | - * |
|
| 51 | - * @return string[] where the array key is the provider ID (string) and the |
|
| 52 | - * value is the enabled state (bool) |
|
| 53 | - */ |
|
| 54 | - public function getState(string $uid): array { |
|
| 55 | - $qb = $this->conn->getQueryBuilder(); |
|
| 56 | - |
|
| 57 | - $query = $qb->select('provider_id', 'enabled') |
|
| 58 | - ->from(self::TABLE_NAME) |
|
| 59 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); |
|
| 60 | - $result = $query->execute(); |
|
| 61 | - $providers = []; |
|
| 62 | - foreach ($result->fetchAll() as $row) { |
|
| 63 | - $providers[$row['provider_id']] = 1 === (int)$row['enabled']; |
|
| 64 | - } |
|
| 65 | - $result->closeCursor(); |
|
| 66 | - |
|
| 67 | - return $providers; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Persist a new/updated (provider_id, uid, enabled) tuple |
|
| 72 | - */ |
|
| 73 | - public function persist(string $providerId, string $uid, int $enabled) { |
|
| 74 | - $qb = $this->conn->getQueryBuilder(); |
|
| 75 | - |
|
| 76 | - try { |
|
| 77 | - // Insert a new entry |
|
| 78 | - $insertQuery = $qb->insert(self::TABLE_NAME)->values([ |
|
| 79 | - 'provider_id' => $qb->createNamedParameter($providerId), |
|
| 80 | - 'uid' => $qb->createNamedParameter($uid), |
|
| 81 | - 'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT), |
|
| 82 | - ]); |
|
| 83 | - |
|
| 84 | - $insertQuery->execute(); |
|
| 85 | - } catch (UniqueConstraintViolationException $ex) { |
|
| 86 | - // There is already an entry -> update it |
|
| 87 | - $updateQuery = $qb->update(self::TABLE_NAME) |
|
| 88 | - ->set('enabled', $qb->createNamedParameter($enabled)) |
|
| 89 | - ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId))) |
|
| 90 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); |
|
| 91 | - $updateQuery->execute(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - } |
|
| 39 | + const TABLE_NAME = 'twofactor_providers'; |
|
| 40 | + |
|
| 41 | + /** @var IDBConnection */ |
|
| 42 | + private $conn; |
|
| 43 | + |
|
| 44 | + public function __construct(IDBConnection $dbConn) { |
|
| 45 | + $this->conn = $dbConn; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Get all assigned provider IDs for the given user ID |
|
| 50 | + * |
|
| 51 | + * @return string[] where the array key is the provider ID (string) and the |
|
| 52 | + * value is the enabled state (bool) |
|
| 53 | + */ |
|
| 54 | + public function getState(string $uid): array { |
|
| 55 | + $qb = $this->conn->getQueryBuilder(); |
|
| 56 | + |
|
| 57 | + $query = $qb->select('provider_id', 'enabled') |
|
| 58 | + ->from(self::TABLE_NAME) |
|
| 59 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); |
|
| 60 | + $result = $query->execute(); |
|
| 61 | + $providers = []; |
|
| 62 | + foreach ($result->fetchAll() as $row) { |
|
| 63 | + $providers[$row['provider_id']] = 1 === (int)$row['enabled']; |
|
| 64 | + } |
|
| 65 | + $result->closeCursor(); |
|
| 66 | + |
|
| 67 | + return $providers; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Persist a new/updated (provider_id, uid, enabled) tuple |
|
| 72 | + */ |
|
| 73 | + public function persist(string $providerId, string $uid, int $enabled) { |
|
| 74 | + $qb = $this->conn->getQueryBuilder(); |
|
| 75 | + |
|
| 76 | + try { |
|
| 77 | + // Insert a new entry |
|
| 78 | + $insertQuery = $qb->insert(self::TABLE_NAME)->values([ |
|
| 79 | + 'provider_id' => $qb->createNamedParameter($providerId), |
|
| 80 | + 'uid' => $qb->createNamedParameter($uid), |
|
| 81 | + 'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT), |
|
| 82 | + ]); |
|
| 83 | + |
|
| 84 | + $insertQuery->execute(); |
|
| 85 | + } catch (UniqueConstraintViolationException $ex) { |
|
| 86 | + // There is already an entry -> update it |
|
| 87 | + $updateQuery = $qb->update(self::TABLE_NAME) |
|
| 88 | + ->set('enabled', $qb->createNamedParameter($enabled)) |
|
| 89 | + ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId))) |
|
| 90 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); |
|
| 91 | + $updateQuery->execute(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | 96 | } |