RenewPasswordController.php 4.7 KB

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