BackupCodeMapper.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Db;
  8. use OCP\AppFramework\Db\QBMapper;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\IDBConnection;
  11. use OCP\IUser;
  12. /**
  13. * @template-extends QBMapper<BackupCode>
  14. */
  15. class BackupCodeMapper extends QBMapper {
  16. public function __construct(IDBConnection $db) {
  17. parent::__construct($db, 'twofactor_backupcodes');
  18. }
  19. /**
  20. * @param IUser $user
  21. * @return BackupCode[]
  22. */
  23. public function getBackupCodes(IUser $user): array {
  24. /* @var IQueryBuilder $qb */
  25. $qb = $this->db->getQueryBuilder();
  26. $qb->select('id', 'user_id', 'code', 'used')
  27. ->from('twofactor_backupcodes')
  28. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())));
  29. return self::findEntities($qb);
  30. }
  31. /**
  32. * @param IUser $user
  33. */
  34. public function deleteCodes(IUser $user): void {
  35. $this->deleteCodesByUserId($user->getUID());
  36. }
  37. /**
  38. * @param string $uid
  39. */
  40. public function deleteCodesByUserId(string $uid): void {
  41. /* @var IQueryBuilder $qb */
  42. $qb = $this->db->getQueryBuilder();
  43. $qb->delete('twofactor_backupcodes')
  44. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid)));
  45. $qb->execute();
  46. }
  47. }