ChangePasswordController.php 6.2 KB

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