ChangePasswordController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 OCP\App\IAppManager;
  24. use OCP\AppFramework\Controller;
  25. use OCP\AppFramework\Http\JSONResponse;
  26. use OCP\IGroupManager;
  27. use OCP\IL10N;
  28. use OCP\IRequest;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use OCP\IUserSession;
  32. class ChangePasswordController extends Controller {
  33. /** @var string */
  34. private $userId;
  35. /** @var IUserManager */
  36. private $userManager;
  37. /** @var IL10N */
  38. private $l;
  39. /** @var IGroupManager */
  40. private $groupManager;
  41. /** @var IUserSession */
  42. private $userSession;
  43. /** @var IAppManager */
  44. private $appManager;
  45. /**
  46. * ChangePasswordController constructor.
  47. *
  48. * @param string $appName
  49. * @param IRequest $request
  50. * @param $userId
  51. * @param IUserManager $userManager
  52. * @param IUserSession $userSession
  53. * @param IGroupManager $groupManager
  54. * @param IAppManager $appManager
  55. * @param IL10N $l
  56. */
  57. public function __construct($appName,
  58. IRequest $request,
  59. $userId,
  60. IUserManager $userManager,
  61. IUserSession $userSession,
  62. IGroupManager $groupManager,
  63. IAppManager $appManager,
  64. IL10N $l) {
  65. parent::__construct($appName, $request);
  66. $this->userId = $userId;
  67. $this->userManager = $userManager;
  68. $this->userSession = $userSession;
  69. $this->groupManager = $groupManager;
  70. $this->appManager = $appManager;
  71. $this->l = $l;
  72. }
  73. /**
  74. * @NoAdminRequired
  75. *
  76. * @param string $oldpassword
  77. * @param string $newpassword
  78. *
  79. * @return JSONResponse
  80. */
  81. public function changePersonalPassword($oldpassword = '', $newpassword = null) {
  82. $user = $this->userManager->checkPassword($this->userId, $oldpassword);
  83. if ($user === false) {
  84. return new JSONResponse([
  85. 'status' => 'error',
  86. 'data' => [
  87. 'message' => $this->l->t('Wrong password'),
  88. ],
  89. ]);
  90. }
  91. /** @var IUser $user */
  92. if ($newpassword === null || $user->setPassword($newpassword) === false) {
  93. return new JSONResponse([
  94. 'status' => 'error'
  95. ]);
  96. }
  97. $this->userSession->updateSessionTokenPassword($newpassword);
  98. return new JSONResponse([
  99. 'status' => 'success',
  100. 'data' => [
  101. 'message' => $this->l->t('Saved'),
  102. ],
  103. ]);
  104. }
  105. /**
  106. * @NoAdminRequired
  107. *
  108. * @param string $username
  109. * @param string $password
  110. * @param string $recoveryPassword
  111. *
  112. * @return JSONResponse
  113. */
  114. public function changeUserPassword($username = null, $password = null, $recoveryPassword = null) {
  115. if ($username === null) {
  116. return new JSONResponse([
  117. 'status' => 'error',
  118. 'data' => [
  119. 'message' => $this->l->t('No user supplied'),
  120. ],
  121. ]);
  122. }
  123. if ($password === null) {
  124. return new JSONResponse([
  125. 'status' => 'error',
  126. 'data' => [
  127. 'message' => $this->l->t('Unable to change password'),
  128. ],
  129. ]);
  130. }
  131. $currentUser = $this->userSession->getUser();
  132. $targetUser = $this->userManager->get($username);
  133. if ($currentUser === null || $targetUser === null ||
  134. !($this->groupManager->isAdmin($this->userId) ||
  135. $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser))
  136. ) {
  137. return new JSONResponse([
  138. 'status' => 'error',
  139. 'data' => [
  140. 'message' => $this->l->t('Authentication error'),
  141. ],
  142. ]);
  143. }
  144. if ($this->appManager->isEnabledForUser('encryption')) {
  145. //handle the recovery case
  146. $crypt = new \OCA\Encryption\Crypto\Crypt(
  147. \OC::$server->getLogger(),
  148. \OC::$server->getUserSession(),
  149. \OC::$server->getConfig(),
  150. \OC::$server->getL10N('encryption'));
  151. $keyStorage = \OC::$server->getEncryptionKeyStorage();
  152. $util = new \OCA\Encryption\Util(
  153. new \OC\Files\View(),
  154. $crypt,
  155. \OC::$server->getLogger(),
  156. \OC::$server->getUserSession(),
  157. \OC::$server->getConfig(),
  158. \OC::$server->getUserManager());
  159. $keyManager = new \OCA\Encryption\KeyManager(
  160. $keyStorage,
  161. $crypt,
  162. \OC::$server->getConfig(),
  163. \OC::$server->getUserSession(),
  164. new \OCA\Encryption\Session(\OC::$server->getSession()),
  165. \OC::$server->getLogger(),
  166. $util);
  167. $recovery = new \OCA\Encryption\Recovery(
  168. \OC::$server->getUserSession(),
  169. $crypt,
  170. \OC::$server->getSecureRandom(),
  171. $keyManager,
  172. \OC::$server->getConfig(),
  173. $keyStorage,
  174. \OC::$server->getEncryptionFilesHelper(),
  175. new \OC\Files\View());
  176. $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled();
  177. $validRecoveryPassword = false;
  178. $recoveryEnabledForUser = false;
  179. if ($recoveryAdminEnabled) {
  180. $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword);
  181. $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username);
  182. }
  183. if ($recoveryEnabledForUser && $recoveryPassword === '') {
  184. return new JSONResponse([
  185. 'status' => 'error',
  186. 'data' => [
  187. 'message' => $this->l->t('Please provide an admin recovery password, otherwise all user data will be lost'),
  188. ]
  189. ]);
  190. } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) {
  191. return new JSONResponse([
  192. 'status' => 'error',
  193. 'data' => [
  194. 'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'),
  195. ]
  196. ]);
  197. } else { // now we know that everything is fine regarding the recovery password, let's try to change the password
  198. $result = $targetUser->setPassword($password, $recoveryPassword);
  199. if (!$result && $recoveryEnabledForUser) {
  200. return new JSONResponse([
  201. 'status' => 'error',
  202. 'data' => [
  203. 'message' => $this->l->t('Backend doesn\'t support password change, but the user\'s encryption key was successfully updated.'),
  204. ]
  205. ]);
  206. } elseif (!$result && !$recoveryEnabledForUser) {
  207. return new JSONResponse([
  208. 'status' => 'error',
  209. 'data' => [
  210. 'message' => $this->l->t('Unable to change password'),
  211. ]
  212. ]);
  213. }
  214. }
  215. } else {
  216. if ($targetUser->setPassword($password) === false) {
  217. return new JSONResponse([
  218. 'status' => 'error',
  219. 'data' => [
  220. 'message' => $this->l->t('Unable to change password'),
  221. ],
  222. ]);
  223. }
  224. }
  225. return new JSONResponse([
  226. 'status' => 'success',
  227. 'data' => [
  228. 'username' => $username,
  229. ],
  230. ]);
  231. }
  232. }