1
0

ChangePasswordController.php 7.6 KB

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