ProviderUserAssignmentDao.php 4.0 KB

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