SettingsController.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\Controller;
  8. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  11. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  12. use OCP\AppFramework\Http\JSONResponse;
  13. use OCP\IRequest;
  14. use OCP\IUserSession;
  15. class SettingsController extends Controller {
  16. /** @var BackupCodeStorage */
  17. private $storage;
  18. /** @var IUserSession */
  19. private $userSession;
  20. /**
  21. * @param string $appName
  22. * @param IRequest $request
  23. * @param BackupCodeStorage $storage
  24. * @param IUserSession $userSession
  25. */
  26. public function __construct($appName, IRequest $request, BackupCodeStorage $storage, IUserSession $userSession) {
  27. parent::__construct($appName, $request);
  28. $this->userSession = $userSession;
  29. $this->storage = $storage;
  30. }
  31. /**
  32. * @return JSONResponse
  33. */
  34. #[NoAdminRequired]
  35. #[PasswordConfirmationRequired]
  36. public function createCodes(): JSONResponse {
  37. $user = $this->userSession->getUser();
  38. $codes = $this->storage->createCodes($user);
  39. return new JSONResponse([
  40. 'codes' => $codes,
  41. 'state' => $this->storage->getBackupCodesState($user),
  42. ]);
  43. }
  44. }