RenewPasswordController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP\Controller;
  7. use OCP\AppFramework\Controller;
  8. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  9. use OCP\AppFramework\Http\Attribute\OpenAPI;
  10. use OCP\AppFramework\Http\Attribute\PublicPage;
  11. use OCP\AppFramework\Http\Attribute\UseSession;
  12. use OCP\AppFramework\Http\RedirectResponse;
  13. use OCP\AppFramework\Http\TemplateResponse;
  14. use OCP\HintException;
  15. use OCP\IConfig;
  16. use OCP\IL10N;
  17. use OCP\IRequest;
  18. use OCP\ISession;
  19. use OCP\IURLGenerator;
  20. use OCP\IUser;
  21. use OCP\IUserManager;
  22. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  23. class RenewPasswordController extends Controller {
  24. /**
  25. * @param string $appName
  26. * @param IRequest $request
  27. * @param IUserManager $userManager
  28. * @param IConfig $config
  29. * @param IURLGenerator $urlGenerator
  30. */
  31. public function __construct(
  32. $appName,
  33. IRequest $request,
  34. private IUserManager $userManager,
  35. private IConfig $config,
  36. protected IL10N $l10n,
  37. private ISession $session,
  38. private IURLGenerator $urlGenerator,
  39. ) {
  40. parent::__construct($appName, $request);
  41. }
  42. /**
  43. * @return RedirectResponse
  44. */
  45. #[PublicPage]
  46. #[NoCSRFRequired]
  47. public function cancel() {
  48. return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
  49. }
  50. /**
  51. * @param string $user
  52. *
  53. * @return TemplateResponse|RedirectResponse
  54. */
  55. #[PublicPage]
  56. #[NoCSRFRequired]
  57. #[UseSession]
  58. public function showRenewPasswordForm($user) {
  59. if ($this->config->getUserValue($user, 'user_ldap', 'needsPasswordReset') !== 'true') {
  60. return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
  61. }
  62. $parameters = [];
  63. $renewPasswordMessages = $this->session->get('renewPasswordMessages');
  64. $errors = [];
  65. $messages = [];
  66. if (is_array($renewPasswordMessages)) {
  67. [$errors, $messages] = $renewPasswordMessages;
  68. }
  69. $this->session->remove('renewPasswordMessages');
  70. foreach ($errors as $value) {
  71. $parameters[$value] = true;
  72. }
  73. $parameters['messages'] = $messages;
  74. $parameters['user'] = $user;
  75. $parameters['canResetPassword'] = true;
  76. $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', '');
  77. if (!$parameters['resetPasswordLink']) {
  78. $userObj = $this->userManager->get($user);
  79. if ($userObj instanceof IUser) {
  80. $parameters['canResetPassword'] = $userObj->canChangePassword();
  81. }
  82. }
  83. $parameters['cancelLink'] = $this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm');
  84. return new TemplateResponse(
  85. $this->appName, 'renewpassword', $parameters, 'guest'
  86. );
  87. }
  88. /**
  89. * @param string $user
  90. * @param string $oldPassword
  91. * @param string $newPassword
  92. *
  93. * @return RedirectResponse
  94. */
  95. #[PublicPage]
  96. #[UseSession]
  97. public function tryRenewPassword($user, $oldPassword, $newPassword) {
  98. if ($this->config->getUserValue($user, 'user_ldap', 'needsPasswordReset') !== 'true') {
  99. return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
  100. }
  101. $args = !is_null($user) ? ['user' => $user] : [];
  102. $loginResult = $this->userManager->checkPassword($user, $oldPassword);
  103. if ($loginResult === false) {
  104. $this->session->set('renewPasswordMessages', [
  105. ['invalidpassword'], []
  106. ]);
  107. return new RedirectResponse($this->urlGenerator->linkToRoute('user_ldap.renewPassword.showRenewPasswordForm', $args));
  108. }
  109. try {
  110. if (!is_null($newPassword) && \OC_User::setPassword($user, $newPassword)) {
  111. $this->session->set('loginMessages', [
  112. [], [$this->l10n->t('Please login with the new password')]
  113. ]);
  114. $this->config->setUserValue($user, 'user_ldap', 'needsPasswordReset', 'false');
  115. return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args));
  116. } else {
  117. $this->session->set('renewPasswordMessages', [
  118. ['internalexception'], []
  119. ]);
  120. }
  121. } catch (HintException $e) {
  122. $this->session->set('renewPasswordMessages', [
  123. [], [$e->getHint()]
  124. ]);
  125. }
  126. return new RedirectResponse($this->urlGenerator->linkToRoute('user_ldap.renewPassword.showRenewPasswordForm', $args));
  127. }
  128. /**
  129. * @return RedirectResponse
  130. */
  131. #[PublicPage]
  132. #[NoCSRFRequired]
  133. #[UseSession]
  134. public function showLoginFormInvalidPassword($user) {
  135. $args = !is_null($user) ? ['user' => $user] : [];
  136. $this->session->set('loginMessages', [
  137. ['invalidpassword'], []
  138. ]);
  139. return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args));
  140. }
  141. }