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