SettingsController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\TwoFactorBackupCodes\Controller;
  27. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http\JSONResponse;
  30. use OCP\IRequest;
  31. use OCP\IUserSession;
  32. class SettingsController extends Controller {
  33. /** @var BackupCodeStorage */
  34. private $storage;
  35. /** @var IUserSession */
  36. private $userSession;
  37. /**
  38. * @param string $appName
  39. * @param IRequest $request
  40. * @param BackupCodeStorage $storage
  41. * @param IUserSession $userSession
  42. */
  43. public function __construct($appName, IRequest $request, BackupCodeStorage $storage, IUserSession $userSession) {
  44. parent::__construct($appName, $request);
  45. $this->userSession = $userSession;
  46. $this->storage = $storage;
  47. }
  48. /**
  49. * @NoAdminRequired
  50. * @PasswordConfirmationRequired
  51. *
  52. * @return JSONResponse
  53. */
  54. public function createCodes(): JSONResponse {
  55. $user = $this->userSession->getUser();
  56. $codes = $this->storage->createCodes($user);
  57. return new JSONResponse([
  58. 'codes' => $codes,
  59. 'state' => $this->storage->getBackupCodesState($user),
  60. ]);
  61. }
  62. }