BackupCodeMapper.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author J0WI <J0WI@users.noreply.github.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\TwoFactorBackupCodes\Db;
  28. use OCP\AppFramework\Db\QBMapper;
  29. use OCP\DB\QueryBuilder\IQueryBuilder;
  30. use OCP\IDBConnection;
  31. use OCP\IUser;
  32. /**
  33. * @template-extends QBMapper<BackupCode>
  34. */
  35. class BackupCodeMapper extends QBMapper {
  36. public function __construct(IDBConnection $db) {
  37. parent::__construct($db, 'twofactor_backupcodes');
  38. }
  39. /**
  40. * @param IUser $user
  41. * @return BackupCode[]
  42. */
  43. public function getBackupCodes(IUser $user): array {
  44. /* @var IQueryBuilder $qb */
  45. $qb = $this->db->getQueryBuilder();
  46. $qb->select('id', 'user_id', 'code', 'used')
  47. ->from('twofactor_backupcodes')
  48. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())));
  49. return self::findEntities($qb);
  50. }
  51. /**
  52. * @param IUser $user
  53. */
  54. public function deleteCodes(IUser $user): void {
  55. $this->deleteCodesByUserId($user->getUID());
  56. }
  57. /**
  58. * @param string $uid
  59. */
  60. public function deleteCodesByUserId(string $uid): void {
  61. /* @var IQueryBuilder $qb */
  62. $qb = $this->db->getQueryBuilder();
  63. $qb->delete('twofactor_backupcodes')
  64. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($uid)));
  65. $qb->execute();
  66. }
  67. }