ChangePasswordController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. // FIXME: disabled for now to be able to inject IGroupManager and also use
  7. // getSubAdmin()
  8. //declare(strict_types=1);
  9. namespace OCA\Settings\Controller;
  10. use OC\Group\Manager as GroupManager;
  11. use OC\User\Session;
  12. use OCA\Encryption\KeyManager;
  13. use OCA\Encryption\Recovery;
  14. use OCP\App\IAppManager;
  15. use OCP\AppFramework\Controller;
  16. use OCP\AppFramework\Http\Attribute\BruteForceProtection;
  17. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  18. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  19. use OCP\AppFramework\Http\JSONResponse;
  20. use OCP\HintException;
  21. use OCP\IL10N;
  22. use OCP\IRequest;
  23. use OCP\IUser;
  24. use OCP\IUserManager;
  25. use OCP\IUserSession;
  26. use OCP\Server;
  27. class ChangePasswordController extends Controller {
  28. private Session $userSession;
  29. public function __construct(
  30. string $appName,
  31. IRequest $request,
  32. private ?string $userId,
  33. private IUserManager $userManager,
  34. IUserSession $userSession,
  35. private GroupManager $groupManager,
  36. private IAppManager $appManager,
  37. private IL10N $l,
  38. ) {
  39. parent::__construct($appName, $request);
  40. $this->userSession = $userSession;
  41. }
  42. /**
  43. * @NoSubAdminRequired
  44. */
  45. #[NoAdminRequired]
  46. #[BruteForceProtection(action: 'changePersonalPassword')]
  47. public function changePersonalPassword(string $oldpassword = '', ?string $newpassword = null): JSONResponse {
  48. $loginName = $this->userSession->getLoginName();
  49. /** @var IUser $user */
  50. $user = $this->userManager->checkPassword($loginName, $oldpassword);
  51. if ($user === false) {
  52. $response = new JSONResponse([
  53. 'status' => 'error',
  54. 'data' => [
  55. 'message' => $this->l->t('Wrong password'),
  56. ],
  57. ]);
  58. $response->throttle();
  59. return $response;
  60. }
  61. try {
  62. if ($newpassword === null || strlen($newpassword) > IUserManager::MAX_PASSWORD_LENGTH || $user->setPassword($newpassword) === false) {
  63. return new JSONResponse([
  64. 'status' => 'error',
  65. 'data' => [
  66. 'message' => $this->l->t('Unable to change personal password'),
  67. ],
  68. ]);
  69. }
  70. // password policy app throws exception
  71. } catch (HintException $e) {
  72. return new JSONResponse([
  73. 'status' => 'error',
  74. 'data' => [
  75. 'message' => $e->getHint(),
  76. ],
  77. ]);
  78. }
  79. $this->userSession->updateSessionTokenPassword($newpassword);
  80. return new JSONResponse([
  81. 'status' => 'success',
  82. 'data' => [
  83. 'message' => $this->l->t('Saved'),
  84. ],
  85. ]);
  86. }
  87. #[NoAdminRequired]
  88. #[PasswordConfirmationRequired]
  89. public function changeUserPassword(?string $username = null, ?string $password = null, ?string $recoveryPassword = null): JSONResponse {
  90. if ($username === null) {
  91. return new JSONResponse([
  92. 'status' => 'error',
  93. 'data' => [
  94. 'message' => $this->l->t('No Login supplied'),
  95. ],
  96. ]);
  97. }
  98. if ($password === null) {
  99. return new JSONResponse([
  100. 'status' => 'error',
  101. 'data' => [
  102. 'message' => $this->l->t('Unable to change password'),
  103. ],
  104. ]);
  105. }
  106. if (strlen($password) > IUserManager::MAX_PASSWORD_LENGTH) {
  107. return new JSONResponse([
  108. 'status' => 'error',
  109. 'data' => [
  110. 'message' => $this->l->t('Unable to change password. Password too long.'),
  111. ],
  112. ]);
  113. }
  114. $currentUser = $this->userSession->getUser();
  115. $targetUser = $this->userManager->get($username);
  116. if ($currentUser === null || $targetUser === null ||
  117. !($this->groupManager->isAdmin($this->userId) ||
  118. $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser))
  119. ) {
  120. return new JSONResponse([
  121. 'status' => 'error',
  122. 'data' => [
  123. 'message' => $this->l->t('Authentication error'),
  124. ],
  125. ]);
  126. }
  127. if ($this->appManager->isEnabledForUser('encryption')) {
  128. //handle the recovery case
  129. $keyManager = Server::get(KeyManager::class);
  130. $recovery = Server::get(Recovery::class);
  131. $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled();
  132. $validRecoveryPassword = false;
  133. $recoveryEnabledForUser = false;
  134. if ($recoveryAdminEnabled) {
  135. $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword);
  136. $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username);
  137. }
  138. if ($recoveryEnabledForUser && $recoveryPassword === '') {
  139. return new JSONResponse([
  140. 'status' => 'error',
  141. 'data' => [
  142. 'message' => $this->l->t('Please provide an admin recovery password; otherwise, all account data will be lost.'),
  143. ]
  144. ]);
  145. } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) {
  146. return new JSONResponse([
  147. 'status' => 'error',
  148. 'data' => [
  149. 'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'),
  150. ]
  151. ]);
  152. } else { // now we know that everything is fine regarding the recovery password, let's try to change the password
  153. try {
  154. $result = $targetUser->setPassword($password, $recoveryPassword);
  155. // password policy app throws exception
  156. } catch (HintException $e) {
  157. return new JSONResponse([
  158. 'status' => 'error',
  159. 'data' => [
  160. 'message' => $e->getHint(),
  161. ],
  162. ]);
  163. }
  164. if (!$result && $recoveryEnabledForUser) {
  165. return new JSONResponse([
  166. 'status' => 'error',
  167. 'data' => [
  168. 'message' => $this->l->t('Backend does not support password change, but the encryption of the account key was updated.'),
  169. ]
  170. ]);
  171. } elseif (!$result && !$recoveryEnabledForUser) {
  172. return new JSONResponse([
  173. 'status' => 'error',
  174. 'data' => [
  175. 'message' => $this->l->t('Unable to change password'),
  176. ]
  177. ]);
  178. }
  179. }
  180. } else {
  181. try {
  182. if ($targetUser->setPassword($password) === false) {
  183. return new JSONResponse([
  184. 'status' => 'error',
  185. 'data' => [
  186. 'message' => $this->l->t('Unable to change password'),
  187. ],
  188. ]);
  189. }
  190. // password policy app throws exception
  191. } catch (HintException $e) {
  192. return new JSONResponse([
  193. 'status' => 'error',
  194. 'data' => [
  195. 'message' => $e->getHint(),
  196. ],
  197. ]);
  198. }
  199. }
  200. return new JSONResponse([
  201. 'status' => 'success',
  202. 'data' => [
  203. 'username' => $username,
  204. ],
  205. ]);
  206. }
  207. }