ProviderUserAssignmentDao.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Authentication\TwoFactorAuth\Db;
  25. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  26. use OCP\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\IDBConnection;
  28. /**
  29. * Data access object to query and assign (provider_id, uid, enabled) tuples of
  30. * 2FA providers
  31. */
  32. class ProviderUserAssignmentDao {
  33. const TABLE_NAME = 'twofactor_providers';
  34. /** @var IDBConnection */
  35. private $conn;
  36. public function __construct(IDBConnection $dbConn) {
  37. $this->conn = $dbConn;
  38. }
  39. /**
  40. * Get all assigned provider IDs for the given user ID
  41. *
  42. * @return string[] where the array key is the provider ID (string) and the
  43. * value is the enabled state (bool)
  44. */
  45. public function getState(string $uid): array {
  46. $qb = $this->conn->getQueryBuilder();
  47. $query = $qb->select('provider_id', 'enabled')
  48. ->from(self::TABLE_NAME)
  49. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  50. $result = $query->execute();
  51. $providers = [];
  52. foreach ($result->fetchAll() as $row) {
  53. $providers[$row['provider_id']] = 1 === (int)$row['enabled'];
  54. }
  55. $result->closeCursor();
  56. return $providers;
  57. }
  58. /**
  59. * Persist a new/updated (provider_id, uid, enabled) tuple
  60. */
  61. public function persist(string $providerId, string $uid, int $enabled) {
  62. $qb = $this->conn->getQueryBuilder();
  63. try {
  64. // Insert a new entry
  65. $insertQuery = $qb->insert(self::TABLE_NAME)->values([
  66. 'provider_id' => $qb->createNamedParameter($providerId),
  67. 'uid' => $qb->createNamedParameter($uid),
  68. 'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT),
  69. ]);
  70. $insertQuery->execute();
  71. } catch (UniqueConstraintViolationException $ex) {
  72. // There is already an entry -> update it
  73. $updateQuery = $qb->update(self::TABLE_NAME)
  74. ->set('enabled', $qb->createNamedParameter($enabled))
  75. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
  76. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  77. $updateQuery->execute();
  78. }
  79. }
  80. public function deleteAll(string $providerId) {
  81. $qb = $this->conn->getQueryBuilder();
  82. $deleteQuery = $qb->delete(self::TABLE_NAME)
  83. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)));
  84. $deleteQuery->execute();
  85. }
  86. }