1
0

ChangePasswordController.php 7.8 KB

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