You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChangePasswordController.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  8. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Matthew Setter <matthew@matthewsetter.com>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. *
  16. * @license GNU AGPL version 3 or any later version
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as
  20. * published by the Free Software Foundation, either version 3 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. */
  32. // FIXME: disabled for now to be able to inject IGroupManager and also use
  33. // getSubAdmin()
  34. //declare(strict_types=1);
  35. namespace OCA\Settings\Controller;
  36. use OC\Group\Manager as GroupManager;
  37. use OC\User\Session;
  38. use OCP\App\IAppManager;
  39. use OCP\AppFramework\Controller;
  40. use OCP\AppFramework\Http\JSONResponse;
  41. use OCP\HintException;
  42. use OCP\IGroupManager;
  43. use OCP\IL10N;
  44. use OCP\IRequest;
  45. use OCP\IUser;
  46. use OCP\IUserManager;
  47. use OCP\IUserSession;
  48. class ChangePasswordController extends Controller {
  49. private ?string $userId;
  50. private IUserManager $userManager;
  51. private IL10N $l;
  52. private GroupManager $groupManager;
  53. private Session $userSession;
  54. private IAppManager $appManager;
  55. public function __construct(string $appName,
  56. IRequest $request,
  57. ?string $userId,
  58. IUserManager $userManager,
  59. IUserSession $userSession,
  60. IGroupManager $groupManager,
  61. IAppManager $appManager,
  62. IL10N $l) {
  63. parent::__construct($appName, $request);
  64. $this->userId = $userId;
  65. $this->userManager = $userManager;
  66. $this->userSession = $userSession;
  67. $this->groupManager = $groupManager;
  68. $this->appManager = $appManager;
  69. $this->l = $l;
  70. }
  71. /**
  72. * @NoAdminRequired
  73. * @NoSubAdminRequired
  74. * @BruteForceProtection(action=changePersonalPassword)
  75. */
  76. public function changePersonalPassword(string $oldpassword = '', string $newpassword = null): JSONResponse {
  77. $loginName = $this->userSession->getLoginName();
  78. /** @var IUser $user */
  79. $user = $this->userManager->checkPassword($loginName, $oldpassword);
  80. if ($user === false) {
  81. $response = new JSONResponse([
  82. 'status' => 'error',
  83. 'data' => [
  84. 'message' => $this->l->t('Wrong password'),
  85. ],
  86. ]);
  87. $response->throttle();
  88. return $response;
  89. }
  90. try {
  91. if ($newpassword === null || strlen($newpassword) > 469 || $user->setPassword($newpassword) === false) {
  92. return new JSONResponse([
  93. 'status' => 'error',
  94. 'data' => [
  95. 'message' => $this->l->t('Unable to change personal password'),
  96. ],
  97. ]);
  98. }
  99. // password policy app throws exception
  100. } catch (HintException $e) {
  101. return new JSONResponse([
  102. 'status' => 'error',
  103. 'data' => [
  104. 'message' => $e->getHint(),
  105. ],
  106. ]);
  107. }
  108. $this->userSession->updateSessionTokenPassword($newpassword);
  109. return new JSONResponse([
  110. 'status' => 'success',
  111. 'data' => [
  112. 'message' => $this->l->t('Saved'),
  113. ],
  114. ]);
  115. }
  116. /**
  117. * @NoAdminRequired
  118. * @PasswordConfirmationRequired
  119. */
  120. public function changeUserPassword(string $username = null, string $password = null, string $recoveryPassword = null): JSONResponse {
  121. if ($username === null) {
  122. return new JSONResponse([
  123. 'status' => 'error',
  124. 'data' => [
  125. 'message' => $this->l->t('No user supplied'),
  126. ],
  127. ]);
  128. }
  129. if ($password === null) {
  130. return new JSONResponse([
  131. 'status' => 'error',
  132. 'data' => [
  133. 'message' => $this->l->t('Unable to change password'),
  134. ],
  135. ]);
  136. }
  137. if (strlen($password) > 469) {
  138. return new JSONResponse([
  139. 'status' => 'error',
  140. 'data' => [
  141. 'message' => $this->l->t('Unable to change password. Password too long.'),
  142. ],
  143. ]);
  144. }
  145. $currentUser = $this->userSession->getUser();
  146. $targetUser = $this->userManager->get($username);
  147. if ($currentUser === null || $targetUser === null ||
  148. !($this->groupManager->isAdmin($this->userId) ||
  149. $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser))
  150. ) {
  151. return new JSONResponse([
  152. 'status' => 'error',
  153. 'data' => [
  154. 'message' => $this->l->t('Authentication error'),
  155. ],
  156. ]);
  157. }
  158. if ($this->appManager->isEnabledForUser('encryption')) {
  159. //handle the recovery case
  160. $crypt = new \OCA\Encryption\Crypto\Crypt(
  161. \OC::$server->getLogger(),
  162. \OC::$server->getUserSession(),
  163. \OC::$server->getConfig(),
  164. \OC::$server->getL10N('encryption'));
  165. $keyStorage = \OC::$server->getEncryptionKeyStorage();
  166. $util = new \OCA\Encryption\Util(
  167. new \OC\Files\View(),
  168. $crypt,
  169. \OC::$server->getLogger(),
  170. \OC::$server->getUserSession(),
  171. \OC::$server->getConfig(),
  172. \OC::$server->getUserManager());
  173. $keyManager = new \OCA\Encryption\KeyManager(
  174. $keyStorage,
  175. $crypt,
  176. \OC::$server->getConfig(),
  177. \OC::$server->getUserSession(),
  178. new \OCA\Encryption\Session(\OC::$server->getSession()),
  179. \OC::$server->getLogger(),
  180. $util,
  181. \OC::$server->getLockingProvider()
  182. );
  183. $recovery = new \OCA\Encryption\Recovery(
  184. \OC::$server->getUserSession(),
  185. $crypt,
  186. $keyManager,
  187. \OC::$server->getConfig(),
  188. \OC::$server->getEncryptionFilesHelper(),
  189. new \OC\Files\View());
  190. $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled();
  191. $validRecoveryPassword = false;
  192. $recoveryEnabledForUser = false;
  193. if ($recoveryAdminEnabled) {
  194. $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword);
  195. $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username);
  196. }
  197. if ($recoveryEnabledForUser && $recoveryPassword === '') {
  198. return new JSONResponse([
  199. 'status' => 'error',
  200. 'data' => [
  201. 'message' => $this->l->t('Please provide an admin recovery password; otherwise, all user data will be lost.'),
  202. ]
  203. ]);
  204. } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) {
  205. return new JSONResponse([
  206. 'status' => 'error',
  207. 'data' => [
  208. 'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'),
  209. ]
  210. ]);
  211. } else { // now we know that everything is fine regarding the recovery password, let's try to change the password
  212. try {
  213. $result = $targetUser->setPassword($password, $recoveryPassword);
  214. // password policy app throws exception
  215. } catch (HintException $e) {
  216. return new JSONResponse([
  217. 'status' => 'error',
  218. 'data' => [
  219. 'message' => $e->getHint(),
  220. ],
  221. ]);
  222. }
  223. if (!$result && $recoveryEnabledForUser) {
  224. return new JSONResponse([
  225. 'status' => 'error',
  226. 'data' => [
  227. 'message' => $this->l->t('Backend does not support password change, but the user\'s encryption key was updated.'),
  228. ]
  229. ]);
  230. } elseif (!$result && !$recoveryEnabledForUser) {
  231. return new JSONResponse([
  232. 'status' => 'error',
  233. 'data' => [
  234. 'message' => $this->l->t('Unable to change password'),
  235. ]
  236. ]);
  237. }
  238. }
  239. } else {
  240. try {
  241. if ($targetUser->setPassword($password) === false) {
  242. return new JSONResponse([
  243. 'status' => 'error',
  244. 'data' => [
  245. 'message' => $this->l->t('Unable to change password'),
  246. ],
  247. ]);
  248. }
  249. // password policy app throws exception
  250. } catch (HintException $e) {
  251. return new JSONResponse([
  252. 'status' => 'error',
  253. 'data' => [
  254. 'message' => $e->getHint(),
  255. ],
  256. ]);
  257. }
  258. }
  259. return new JSONResponse([
  260. 'status' => 'success',
  261. 'data' => [
  262. 'username' => $username,
  263. ],
  264. ]);
  265. }
  266. }