ProviderUserAssignmentDao.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 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 OCP\IDBConnection;
  26. use function array_map;
  27. /**
  28. * Data access object to query and assign (provider_id, uid, enabled) tuples of
  29. * 2FA providers
  30. */
  31. class ProviderUserAssignmentDao {
  32. public const TABLE_NAME = 'twofactor_providers';
  33. /** @var IDBConnection */
  34. private $conn;
  35. public function __construct(IDBConnection $dbConn) {
  36. $this->conn = $dbConn;
  37. }
  38. /**
  39. * Get all assigned provider IDs for the given user ID
  40. *
  41. * @return array<string, bool> where the array key is the provider ID (string) and the
  42. * value is the enabled state (bool)
  43. */
  44. public function getState(string $uid): array {
  45. $qb = $this->conn->getQueryBuilder();
  46. $query = $qb->select('provider_id', 'enabled')
  47. ->from(self::TABLE_NAME)
  48. ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  49. $result = $query->execute();
  50. $providers = [];
  51. foreach ($result->fetchAll() as $row) {
  52. $providers[(string)$row['provider_id']] = (int)$row['enabled'] === 1;
  53. }
  54. $result->closeCursor();
  55. return $providers;
  56. }
  57. /**
  58. * Persist a new/updated (provider_id, uid, enabled) tuple
  59. */
  60. public function persist(string $providerId, string $uid, int $enabled): void {
  61. $conn = $this->conn;
  62. // Insert a new entry
  63. if ($conn->insertIgnoreConflict(self::TABLE_NAME, [
  64. 'provider_id' => $providerId,
  65. 'uid' => $uid,
  66. 'enabled' => $enabled,
  67. ])) {
  68. return;
  69. }
  70. // There is already an entry -> update it
  71. $qb = $conn->getQueryBuilder();
  72. $updateQuery = $qb->update(self::TABLE_NAME)
  73. ->set('enabled', $qb->createNamedParameter($enabled))
  74. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)))
  75. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  76. $updateQuery->executeStatement();
  77. }
  78. /**
  79. * Delete all provider states of a user and return the provider IDs
  80. *
  81. * @param string $uid
  82. *
  83. * @return list<array{provider_id: string, uid: string, enabled: bool}>
  84. */
  85. public function deleteByUser(string $uid): array {
  86. $qb1 = $this->conn->getQueryBuilder();
  87. $selectQuery = $qb1->select('*')
  88. ->from(self::TABLE_NAME)
  89. ->where($qb1->expr()->eq('uid', $qb1->createNamedParameter($uid)));
  90. $selectResult = $selectQuery->execute();
  91. $rows = $selectResult->fetchAll();
  92. $selectResult->closeCursor();
  93. $qb2 = $this->conn->getQueryBuilder();
  94. $deleteQuery = $qb2
  95. ->delete(self::TABLE_NAME)
  96. ->where($qb2->expr()->eq('uid', $qb2->createNamedParameter($uid)));
  97. $deleteQuery->execute();
  98. return array_map(function (array $row) {
  99. return [
  100. 'provider_id' => (string) $row['provider_id'],
  101. 'uid' => (string) $row['uid'],
  102. 'enabled' => (int) $row['enabled'] === 1,
  103. ];
  104. }, $rows);
  105. }
  106. public function deleteAll(string $providerId): void {
  107. $qb = $this->conn->getQueryBuilder();
  108. $deleteQuery = $qb->delete(self::TABLE_NAME)
  109. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter($providerId)));
  110. $deleteQuery->execute();
  111. }
  112. }