| @@ -34,256 +34,256 @@ | ||
| 34 | 34 | |
| 35 | 35 |  class ChangePasswordController extends Controller { | 
| 36 | 36 | |
| 37 | - /** @var string */ | |
| 38 | - private $userId; | |
| 37 | + /** @var string */ | |
| 38 | + private $userId; | |
| 39 | 39 | |
| 40 | - /** @var IUserManager */ | |
| 41 | - private $userManager; | |
| 40 | + /** @var IUserManager */ | |
| 41 | + private $userManager; | |
| 42 | 42 | |
| 43 | - /** @var IL10N */ | |
| 44 | - private $l; | |
| 43 | + /** @var IL10N */ | |
| 44 | + private $l; | |
| 45 | 45 | |
| 46 | - /** @var IGroupManager */ | |
| 47 | - private $groupManager; | |
| 46 | + /** @var IGroupManager */ | |
| 47 | + private $groupManager; | |
| 48 | 48 | |
| 49 | - /** @var IUserSession */ | |
| 50 | - private $userSession; | |
| 49 | + /** @var IUserSession */ | |
| 50 | + private $userSession; | |
| 51 | 51 | |
| 52 | - /** @var IAppManager */ | |
| 53 | - private $appManager; | |
| 52 | + /** @var IAppManager */ | |
| 53 | + private $appManager; | |
| 54 | 54 | |
| 55 | - /** | |
| 56 | - * ChangePasswordController constructor. | |
| 57 | - * | |
| 58 | - * @param string $appName | |
| 59 | - * @param IRequest $request | |
| 60 | - * @param $userId | |
| 61 | - * @param IUserManager $userManager | |
| 62 | - * @param IUserSession $userSession | |
| 63 | - * @param IGroupManager $groupManager | |
| 64 | - * @param IAppManager $appManager | |
| 65 | - * @param IL10N $l | |
| 66 | - */ | |
| 67 | - public function __construct($appName, | |
| 68 | - IRequest $request, | |
| 69 | - $userId, | |
| 70 | - IUserManager $userManager, | |
| 71 | - IUserSession $userSession, | |
| 72 | - IGroupManager $groupManager, | |
| 73 | - IAppManager $appManager, | |
| 74 | -								IL10N $l) { | |
| 75 | - parent::__construct($appName, $request); | |
| 55 | + /** | |
| 56 | + * ChangePasswordController constructor. | |
| 57 | + * | |
| 58 | + * @param string $appName | |
| 59 | + * @param IRequest $request | |
| 60 | + * @param $userId | |
| 61 | + * @param IUserManager $userManager | |
| 62 | + * @param IUserSession $userSession | |
| 63 | + * @param IGroupManager $groupManager | |
| 64 | + * @param IAppManager $appManager | |
| 65 | + * @param IL10N $l | |
| 66 | + */ | |
| 67 | + public function __construct($appName, | |
| 68 | + IRequest $request, | |
| 69 | + $userId, | |
| 70 | + IUserManager $userManager, | |
| 71 | + IUserSession $userSession, | |
| 72 | + IGroupManager $groupManager, | |
| 73 | + IAppManager $appManager, | |
| 74 | +                                IL10N $l) { | |
| 75 | + parent::__construct($appName, $request); | |
| 76 | 76 | |
| 77 | - $this->userId = $userId; | |
| 78 | - $this->userManager = $userManager; | |
| 79 | - $this->userSession = $userSession; | |
| 80 | - $this->groupManager = $groupManager; | |
| 81 | - $this->appManager = $appManager; | |
| 82 | - $this->l = $l; | |
| 83 | - } | |
| 77 | + $this->userId = $userId; | |
| 78 | + $this->userManager = $userManager; | |
| 79 | + $this->userSession = $userSession; | |
| 80 | + $this->groupManager = $groupManager; | |
| 81 | + $this->appManager = $appManager; | |
| 82 | + $this->l = $l; | |
| 83 | + } | |
| 84 | 84 | |
| 85 | - /** | |
| 86 | - * @NoAdminRequired | |
| 87 | - * @NoSubadminRequired | |
| 88 | - * @BruteForceProtection(action=changePersonalPassword) | |
| 89 | - * | |
| 90 | - * @param string $oldpassword | |
| 91 | - * @param string $newpassword | |
| 92 | - * | |
| 93 | - * @return JSONResponse | |
| 94 | - */ | |
| 95 | -	public function changePersonalPassword($oldpassword = '', $newpassword = null) { | |
| 96 | - /** @var IUser $user */ | |
| 97 | - $user = $this->userManager->checkPassword($this->userId, $oldpassword); | |
| 98 | -		if ($user === false) { | |
| 99 | - $response = new JSONResponse([ | |
| 100 | - 'status' => 'error', | |
| 101 | - 'data' => [ | |
| 102 | -					'message' => $this->l->t('Wrong password'), | |
| 103 | - ], | |
| 104 | - ]); | |
| 105 | - $response->throttle(); | |
| 106 | - return $response; | |
| 107 | - } | |
| 85 | + /** | |
| 86 | + * @NoAdminRequired | |
| 87 | + * @NoSubadminRequired | |
| 88 | + * @BruteForceProtection(action=changePersonalPassword) | |
| 89 | + * | |
| 90 | + * @param string $oldpassword | |
| 91 | + * @param string $newpassword | |
| 92 | + * | |
| 93 | + * @return JSONResponse | |
| 94 | + */ | |
| 95 | +    public function changePersonalPassword($oldpassword = '', $newpassword = null) { | |
| 96 | + /** @var IUser $user */ | |
| 97 | + $user = $this->userManager->checkPassword($this->userId, $oldpassword); | |
| 98 | +        if ($user === false) { | |
| 99 | + $response = new JSONResponse([ | |
| 100 | + 'status' => 'error', | |
| 101 | + 'data' => [ | |
| 102 | +                    'message' => $this->l->t('Wrong password'), | |
| 103 | + ], | |
| 104 | + ]); | |
| 105 | + $response->throttle(); | |
| 106 | + return $response; | |
| 107 | + } | |
| 108 | 108 | |
| 109 | -		try { | |
| 110 | -			if ($newpassword === null || $user->setPassword($newpassword) === false) { | |
| 111 | - return new JSONResponse([ | |
| 112 | - 'status' => 'error' | |
| 113 | - ]); | |
| 114 | - } | |
| 115 | - // password policy app throws exception | |
| 116 | -		} catch(HintException $e) { | |
| 117 | - return new JSONResponse([ | |
| 118 | - 'status' => 'error', | |
| 119 | - 'data' => [ | |
| 120 | - 'message' => $e->getHint(), | |
| 121 | - ], | |
| 122 | - ]); | |
| 123 | - } | |
| 109 | +        try { | |
| 110 | +            if ($newpassword === null || $user->setPassword($newpassword) === false) { | |
| 111 | + return new JSONResponse([ | |
| 112 | + 'status' => 'error' | |
| 113 | + ]); | |
| 114 | + } | |
| 115 | + // password policy app throws exception | |
| 116 | +        } catch(HintException $e) { | |
| 117 | + return new JSONResponse([ | |
| 118 | + 'status' => 'error', | |
| 119 | + 'data' => [ | |
| 120 | + 'message' => $e->getHint(), | |
| 121 | + ], | |
| 122 | + ]); | |
| 123 | + } | |
| 124 | 124 | |
| 125 | - $this->userSession->updateSessionTokenPassword($newpassword); | |
| 125 | + $this->userSession->updateSessionTokenPassword($newpassword); | |
| 126 | 126 | |
| 127 | - return new JSONResponse([ | |
| 128 | - 'status' => 'success', | |
| 129 | - 'data' => [ | |
| 130 | -				'message' => $this->l->t('Saved'), | |
| 131 | - ], | |
| 132 | - ]); | |
| 133 | - } | |
| 127 | + return new JSONResponse([ | |
| 128 | + 'status' => 'success', | |
| 129 | + 'data' => [ | |
| 130 | +                'message' => $this->l->t('Saved'), | |
| 131 | + ], | |
| 132 | + ]); | |
| 133 | + } | |
| 134 | 134 | |
| 135 | - /** | |
| 136 | - * @NoAdminRequired | |
| 137 | - * @PasswordConfirmationRequired | |
| 138 | - * | |
| 139 | - * @param string $username | |
| 140 | - * @param string $password | |
| 141 | - * @param string $recoveryPassword | |
| 142 | - * | |
| 143 | - * @return JSONResponse | |
| 144 | - */ | |
| 145 | -	public function changeUserPassword($username = null, $password = null, $recoveryPassword = null) { | |
| 146 | -		if ($username === null) { | |
| 147 | - return new JSONResponse([ | |
| 148 | - 'status' => 'error', | |
| 149 | - 'data' => [ | |
| 150 | -					'message' => $this->l->t('No user supplied'), | |
| 151 | - ], | |
| 152 | - ]); | |
| 153 | - } | |
| 135 | + /** | |
| 136 | + * @NoAdminRequired | |
| 137 | + * @PasswordConfirmationRequired | |
| 138 | + * | |
| 139 | + * @param string $username | |
| 140 | + * @param string $password | |
| 141 | + * @param string $recoveryPassword | |
| 142 | + * | |
| 143 | + * @return JSONResponse | |
| 144 | + */ | |
| 145 | +    public function changeUserPassword($username = null, $password = null, $recoveryPassword = null) { | |
| 146 | +        if ($username === null) { | |
| 147 | + return new JSONResponse([ | |
| 148 | + 'status' => 'error', | |
| 149 | + 'data' => [ | |
| 150 | +                    'message' => $this->l->t('No user supplied'), | |
| 151 | + ], | |
| 152 | + ]); | |
| 153 | + } | |
| 154 | 154 | |
| 155 | -		if ($password === null) { | |
| 156 | - return new JSONResponse([ | |
| 157 | - 'status' => 'error', | |
| 158 | - 'data' => [ | |
| 159 | -					'message' => $this->l->t('Unable to change password'), | |
| 160 | - ], | |
| 161 | - ]); | |
| 162 | - } | |
| 155 | +        if ($password === null) { | |
| 156 | + return new JSONResponse([ | |
| 157 | + 'status' => 'error', | |
| 158 | + 'data' => [ | |
| 159 | +                    'message' => $this->l->t('Unable to change password'), | |
| 160 | + ], | |
| 161 | + ]); | |
| 162 | + } | |
| 163 | 163 | |
| 164 | - $currentUser = $this->userSession->getUser(); | |
| 165 | - $targetUser = $this->userManager->get($username); | |
| 166 | - if ($currentUser === null || $targetUser === null || | |
| 167 | - !($this->groupManager->isAdmin($this->userId) || | |
| 168 | - $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser)) | |
| 169 | -		) { | |
| 170 | - return new JSONResponse([ | |
| 171 | - 'status' => 'error', | |
| 172 | - 'data' => [ | |
| 173 | -					'message' => $this->l->t('Authentication error'), | |
| 174 | - ], | |
| 175 | - ]); | |
| 176 | - } | |
| 164 | + $currentUser = $this->userSession->getUser(); | |
| 165 | + $targetUser = $this->userManager->get($username); | |
| 166 | + if ($currentUser === null || $targetUser === null || | |
| 167 | + !($this->groupManager->isAdmin($this->userId) || | |
| 168 | + $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser)) | |
| 169 | +        ) { | |
| 170 | + return new JSONResponse([ | |
| 171 | + 'status' => 'error', | |
| 172 | + 'data' => [ | |
| 173 | +                    'message' => $this->l->t('Authentication error'), | |
| 174 | + ], | |
| 175 | + ]); | |
| 176 | + } | |
| 177 | 177 | |
| 178 | -		if ($this->appManager->isEnabledForUser('encryption')) { | |
| 179 | - //handle the recovery case | |
| 180 | - $crypt = new \OCA\Encryption\Crypto\Crypt( | |
| 181 | - \OC::$server->getLogger(), | |
| 182 | - \OC::$server->getUserSession(), | |
| 183 | - \OC::$server->getConfig(), | |
| 184 | -				\OC::$server->getL10N('encryption')); | |
| 185 | - $keyStorage = \OC::$server->getEncryptionKeyStorage(); | |
| 186 | - $util = new \OCA\Encryption\Util( | |
| 187 | - new \OC\Files\View(), | |
| 188 | - $crypt, | |
| 189 | - \OC::$server->getLogger(), | |
| 190 | - \OC::$server->getUserSession(), | |
| 191 | - \OC::$server->getConfig(), | |
| 192 | - \OC::$server->getUserManager()); | |
| 193 | - $keyManager = new \OCA\Encryption\KeyManager( | |
| 194 | - $keyStorage, | |
| 195 | - $crypt, | |
| 196 | - \OC::$server->getConfig(), | |
| 197 | - \OC::$server->getUserSession(), | |
| 198 | - new \OCA\Encryption\Session(\OC::$server->getSession()), | |
| 199 | - \OC::$server->getLogger(), | |
| 200 | - $util); | |
| 201 | - $recovery = new \OCA\Encryption\Recovery( | |
| 202 | - \OC::$server->getUserSession(), | |
| 203 | - $crypt, | |
| 204 | - \OC::$server->getSecureRandom(), | |
| 205 | - $keyManager, | |
| 206 | - \OC::$server->getConfig(), | |
| 207 | - $keyStorage, | |
| 208 | - \OC::$server->getEncryptionFilesHelper(), | |
| 209 | - new \OC\Files\View()); | |
| 210 | - $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); | |
| 178 | +        if ($this->appManager->isEnabledForUser('encryption')) { | |
| 179 | + //handle the recovery case | |
| 180 | + $crypt = new \OCA\Encryption\Crypto\Crypt( | |
| 181 | + \OC::$server->getLogger(), | |
| 182 | + \OC::$server->getUserSession(), | |
| 183 | + \OC::$server->getConfig(), | |
| 184 | +                \OC::$server->getL10N('encryption')); | |
| 185 | + $keyStorage = \OC::$server->getEncryptionKeyStorage(); | |
| 186 | + $util = new \OCA\Encryption\Util( | |
| 187 | + new \OC\Files\View(), | |
| 188 | + $crypt, | |
| 189 | + \OC::$server->getLogger(), | |
| 190 | + \OC::$server->getUserSession(), | |
| 191 | + \OC::$server->getConfig(), | |
| 192 | + \OC::$server->getUserManager()); | |
| 193 | + $keyManager = new \OCA\Encryption\KeyManager( | |
| 194 | + $keyStorage, | |
| 195 | + $crypt, | |
| 196 | + \OC::$server->getConfig(), | |
| 197 | + \OC::$server->getUserSession(), | |
| 198 | + new \OCA\Encryption\Session(\OC::$server->getSession()), | |
| 199 | + \OC::$server->getLogger(), | |
| 200 | + $util); | |
| 201 | + $recovery = new \OCA\Encryption\Recovery( | |
| 202 | + \OC::$server->getUserSession(), | |
| 203 | + $crypt, | |
| 204 | + \OC::$server->getSecureRandom(), | |
| 205 | + $keyManager, | |
| 206 | + \OC::$server->getConfig(), | |
| 207 | + $keyStorage, | |
| 208 | + \OC::$server->getEncryptionFilesHelper(), | |
| 209 | + new \OC\Files\View()); | |
| 210 | + $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); | |
| 211 | 211 | |
| 212 | - $validRecoveryPassword = false; | |
| 213 | - $recoveryEnabledForUser = false; | |
| 214 | -			if ($recoveryAdminEnabled) { | |
| 215 | - $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); | |
| 216 | - $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); | |
| 217 | - } | |
| 212 | + $validRecoveryPassword = false; | |
| 213 | + $recoveryEnabledForUser = false; | |
| 214 | +            if ($recoveryAdminEnabled) { | |
| 215 | + $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); | |
| 216 | + $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); | |
| 217 | + } | |
| 218 | 218 | |
| 219 | -			if ($recoveryEnabledForUser && $recoveryPassword === '') { | |
| 220 | - return new JSONResponse([ | |
| 221 | - 'status' => 'error', | |
| 222 | - 'data' => [ | |
| 223 | -						'message' => $this->l->t('Please provide an admin recovery password; otherwise, all user data will be lost.'), | |
| 224 | - ] | |
| 225 | - ]); | |
| 226 | -			} elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) { | |
| 227 | - return new JSONResponse([ | |
| 228 | - 'status' => 'error', | |
| 229 | - 'data' => [ | |
| 230 | -						'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'), | |
| 231 | - ] | |
| 232 | - ]); | |
| 233 | -			} else { // now we know that everything is fine regarding the recovery password, let's try to change the password | |
| 234 | -				try { | |
| 235 | - $result = $targetUser->setPassword($password, $recoveryPassword); | |
| 236 | - // password policy app throws exception | |
| 237 | -				} catch(HintException $e) { | |
| 238 | - return new JSONResponse([ | |
| 239 | - 'status' => 'error', | |
| 240 | - 'data' => [ | |
| 241 | - 'message' => $e->getHint(), | |
| 242 | - ], | |
| 243 | - ]); | |
| 244 | - } | |
| 245 | -				if (!$result && $recoveryEnabledForUser) { | |
| 246 | - return new JSONResponse([ | |
| 247 | - 'status' => 'error', | |
| 248 | - 'data' => [ | |
| 249 | -							'message' => $this->l->t('Backend doesn\'t support password change, but the user\'s encryption key was successfully updated.'), | |
| 250 | - ] | |
| 251 | - ]); | |
| 252 | -				} elseif (!$result && !$recoveryEnabledForUser) { | |
| 253 | - return new JSONResponse([ | |
| 254 | - 'status' => 'error', | |
| 255 | - 'data' => [ | |
| 256 | -							'message' => $this->l->t('Unable to change password'), | |
| 257 | - ] | |
| 258 | - ]); | |
| 259 | - } | |
| 260 | - } | |
| 261 | -		} else { | |
| 262 | -			try { | |
| 263 | -				if ($targetUser->setPassword($password) === false) { | |
| 264 | - return new JSONResponse([ | |
| 265 | - 'status' => 'error', | |
| 266 | - 'data' => [ | |
| 267 | -							'message' => $this->l->t('Unable to change password'), | |
| 268 | - ], | |
| 269 | - ]); | |
| 270 | - } | |
| 271 | - // password policy app throws exception | |
| 272 | -			} catch(HintException $e) { | |
| 273 | - return new JSONResponse([ | |
| 274 | - 'status' => 'error', | |
| 275 | - 'data' => [ | |
| 276 | - 'message' => $e->getHint(), | |
| 277 | - ], | |
| 278 | - ]); | |
| 279 | - } | |
| 280 | - } | |
| 219 | +            if ($recoveryEnabledForUser && $recoveryPassword === '') { | |
| 220 | + return new JSONResponse([ | |
| 221 | + 'status' => 'error', | |
| 222 | + 'data' => [ | |
| 223 | +                        'message' => $this->l->t('Please provide an admin recovery password; otherwise, all user data will be lost.'), | |
| 224 | + ] | |
| 225 | + ]); | |
| 226 | +            } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) { | |
| 227 | + return new JSONResponse([ | |
| 228 | + 'status' => 'error', | |
| 229 | + 'data' => [ | |
| 230 | +                        'message' => $this->l->t('Wrong admin recovery password. Please check the password and try again.'), | |
| 231 | + ] | |
| 232 | + ]); | |
| 233 | +            } else { // now we know that everything is fine regarding the recovery password, let's try to change the password | |
| 234 | +                try { | |
| 235 | + $result = $targetUser->setPassword($password, $recoveryPassword); | |
| 236 | + // password policy app throws exception | |
| 237 | +                } catch(HintException $e) { | |
| 238 | + return new JSONResponse([ | |
| 239 | + 'status' => 'error', | |
| 240 | + 'data' => [ | |
| 241 | + 'message' => $e->getHint(), | |
| 242 | + ], | |
| 243 | + ]); | |
| 244 | + } | |
| 245 | +                if (!$result && $recoveryEnabledForUser) { | |
| 246 | + return new JSONResponse([ | |
| 247 | + 'status' => 'error', | |
| 248 | + 'data' => [ | |
| 249 | +                            'message' => $this->l->t('Backend doesn\'t support password change, but the user\'s encryption key was successfully updated.'), | |
| 250 | + ] | |
| 251 | + ]); | |
| 252 | +                } elseif (!$result && !$recoveryEnabledForUser) { | |
| 253 | + return new JSONResponse([ | |
| 254 | + 'status' => 'error', | |
| 255 | + 'data' => [ | |
| 256 | +                            'message' => $this->l->t('Unable to change password'), | |
| 257 | + ] | |
| 258 | + ]); | |
| 259 | + } | |
| 260 | + } | |
| 261 | +        } else { | |
| 262 | +            try { | |
| 263 | +                if ($targetUser->setPassword($password) === false) { | |
| 264 | + return new JSONResponse([ | |
| 265 | + 'status' => 'error', | |
| 266 | + 'data' => [ | |
| 267 | +                            'message' => $this->l->t('Unable to change password'), | |
| 268 | + ], | |
| 269 | + ]); | |
| 270 | + } | |
| 271 | + // password policy app throws exception | |
| 272 | +            } catch(HintException $e) { | |
| 273 | + return new JSONResponse([ | |
| 274 | + 'status' => 'error', | |
| 275 | + 'data' => [ | |
| 276 | + 'message' => $e->getHint(), | |
| 277 | + ], | |
| 278 | + ]); | |
| 279 | + } | |
| 280 | + } | |
| 281 | 281 | |
| 282 | - return new JSONResponse([ | |
| 283 | - 'status' => 'success', | |
| 284 | - 'data' => [ | |
| 285 | - 'username' => $username, | |
| 286 | - ], | |
| 287 | - ]); | |
| 288 | - } | |
| 282 | + return new JSONResponse([ | |
| 283 | + 'status' => 'success', | |
| 284 | + 'data' => [ | |
| 285 | + 'username' => $username, | |
| 286 | + ], | |
| 287 | + ]); | |
| 288 | + } | |
| 289 | 289 | } |