SettingsController.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. /**
  17. * @param string $appName
  18. * @param IRequest $request
  19. * @param BackupCodeStorage $storage
  20. * @param IUserSession $userSession
  21. */
  22. public function __construct(
  23. $appName,
  24. IRequest $request,
  25. private BackupCodeStorage $storage,
  26. private IUserSession $userSession,
  27. ) {
  28. parent::__construct($appName, $request);
  29. }
  30. /**
  31. * @return JSONResponse
  32. */
  33. #[NoAdminRequired]
  34. #[PasswordConfirmationRequired]
  35. public function createCodes(): JSONResponse {
  36. $user = $this->userSession->getUser();
  37. $codes = $this->storage->createCodes($user);
  38. return new JSONResponse([
  39. 'codes' => $codes,
  40. 'state' => $this->storage->getBackupCodesState($user),
  41. ]);
  42. }
  43. }