ProviderUserAssignmentDao.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Authentication\TwoFactorAuth\Db;
  8. use OCP\IDBConnection;
  9. use function array_map;
  10. /**
  11. * Data access object to query and assign (provider_id, uid, enabled) tuples of
  12. * 2FA providers
  13. */
  14. class ProviderUserAssignmentDao {
  15. public const TABLE_NAME = 'twofactor_providers';
  16. /** @var IDBConnection */
  17. private $conn;
  18. public function __construct(IDBConnection $dbConn) {
  19. $this->conn = $dbConn;
  20. }
  21. /**
  22. * Get all assigned provider IDs for the given user ID
  23. *
  24. * @return array<string, bool> where the array key is the provider ID (string) and the
  25. * value is the enabled state (bool)
  26. */
  27. public function getState(string $uid): array {
  28. $qb = $this->conn->getQueryBuilder();
  29. $query = $qb->select('provider_id', 'enabled')
  30. ->from(self::TABLE_NAME)
  31. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  32. $result = $query->execute();
  33. $providers = [];
  34. foreach ($result->fetchAll() as $row) {
  35. $providers[(string)$row['provider_id']] = (int)$row['enabled'] === 1;
  36. }
  37. $result->closeCursor();
  38. return $providers;
  39. }
  40. /**
  41. * Persist a new/updated (provider_id, uid, enabled) tuple
  42. */
  43. public function persist(string $providerId, string $uid, int $enabled): void {
  44. $conn = $this->conn;
  45. // Insert a new entry
  46. if ($conn->insertIgnoreConflict(self::TABLE_NAME, [
  47. 'provider_id' => $providerId,
  48. 'uid' => $uid,
  49. 'enabled' => $enabled,
  50. ])) {
  51. return;
  52. }
  53. // There is already an entry -> update it
  54. $qb = $conn->getQueryBuilder();
  55. $updateQuery = $qb->update(self::TABLE_NAME)
  56. ->set('enabled', $qb->createNamedParameter($enabled))
  57. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
  58. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  59. $updateQuery->executeStatement();
  60. }
  61. /**
  62. * Delete all provider states of a user and return the provider IDs
  63. *
  64. * @return list<array{provider_id: string, uid: string, enabled: bool}>
  65. */
  66. public function deleteByUser(string $uid): array {
  67. $qb1 = $this->conn->getQueryBuilder();
  68. $selectQuery = $qb1->select('*')
  69. ->from(self::TABLE_NAME)
  70. ->where($qb1->expr()->eq('uid', $qb1->createNamedParameter($uid)));
  71. $selectResult = $selectQuery->executeQuery();
  72. $rows = $selectResult->fetchAll();
  73. $selectResult->closeCursor();
  74. $qb2 = $this->conn->getQueryBuilder();
  75. $deleteQuery = $qb2
  76. ->delete(self::TABLE_NAME)
  77. ->where($qb2->expr()->eq('uid', $qb2->createNamedParameter($uid)));
  78. $deleteQuery->executeStatement();
  79. return array_values(array_map(function (array $row) {
  80. return [
  81. 'provider_id' => (string)$row['provider_id'],
  82. 'uid' => (string)$row['uid'],
  83. 'enabled' => ((int) $row['enabled']) === 1,
  84. ];
  85. }, $rows));
  86. }
  87. public function deleteAll(string $providerId): void {
  88. $qb = $this->conn->getQueryBuilder();
  89. $deleteQuery = $qb->delete(self::TABLE_NAME)
  90. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)));
  91. $deleteQuery->execute();
  92. }
  93. }