| Conditions | 20 |
| Paths | 19 |
| Total Lines | 141 |
| Code Lines | 100 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 131 | public function changeUserPassword(string $username = null, string $password = null, string $recoveryPassword = null): JSONResponse { |
||
| 132 | if ($username === null) { |
||
| 133 | return new JSONResponse([ |
||
| 134 | 'status' => 'error', |
||
| 135 | 'data' => [ |
||
| 136 | 'message' => $this->l->t('No user supplied'), |
||
| 137 | ], |
||
| 138 | ]); |
||
| 139 | } |
||
| 140 | |||
| 141 | if ($password === null) { |
||
| 142 | return new JSONResponse([ |
||
| 143 | 'status' => 'error', |
||
| 144 | 'data' => [ |
||
| 145 | 'message' => $this->l->t('Unable to change password'), |
||
| 146 | ], |
||
| 147 | ]); |
||
| 148 | } |
||
| 149 | |||
| 150 | $currentUser = $this->userSession->getUser(); |
||
| 151 | $targetUser = $this->userManager->get($username); |
||
| 152 | if ($currentUser === null || $targetUser === null || |
||
| 153 | !($this->groupManager->isAdmin($this->userId) || |
||
| 154 | $this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $targetUser)) |
||
| 155 | ) { |
||
| 156 | return new JSONResponse([ |
||
| 157 | 'status' => 'error', |
||
| 158 | 'data' => [ |
||
| 159 | 'message' => $this->l->t('Authentication error'), |
||
| 160 | ], |
||
| 161 | ]); |
||
| 162 | } |
||
| 163 | |||
| 164 | if ($this->appManager->isEnabledForUser('encryption')) { |
||
| 165 | //handle the recovery case |
||
| 166 | $crypt = new \OCA\Encryption\Crypto\Crypt( |
||
| 167 | \OC::$server->getLogger(), |
||
| 168 | \OC::$server->getUserSession(), |
||
| 169 | \OC::$server->getConfig(), |
||
| 170 | \OC::$server->getL10N('encryption')); |
||
| 171 | $keyStorage = \OC::$server->getEncryptionKeyStorage(); |
||
| 172 | $util = new \OCA\Encryption\Util( |
||
| 173 | new \OC\Files\View(), |
||
| 174 | $crypt, |
||
| 175 | \OC::$server->getLogger(), |
||
| 176 | \OC::$server->getUserSession(), |
||
| 177 | \OC::$server->getConfig(), |
||
| 178 | \OC::$server->getUserManager()); |
||
| 179 | $keyManager = new \OCA\Encryption\KeyManager( |
||
| 180 | $keyStorage, |
||
| 181 | $crypt, |
||
| 182 | \OC::$server->getConfig(), |
||
| 183 | \OC::$server->getUserSession(), |
||
| 184 | new \OCA\Encryption\Session(\OC::$server->getSession()), |
||
| 185 | \OC::$server->getLogger(), |
||
| 186 | $util); |
||
| 187 | $recovery = new \OCA\Encryption\Recovery( |
||
| 188 | \OC::$server->getUserSession(), |
||
| 189 | $crypt, |
||
| 190 | \OC::$server->getSecureRandom(), |
||
| 191 | $keyManager, |
||
| 192 | \OC::$server->getConfig(), |
||
| 193 | $keyStorage, |
||
| 194 | \OC::$server->getEncryptionFilesHelper(), |
||
| 195 | new \OC\Files\View()); |
||
| 196 | $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); |
||
| 197 | |||
| 198 | $validRecoveryPassword = false; |
||
| 199 | $recoveryEnabledForUser = false; |
||
| 200 | if ($recoveryAdminEnabled) { |
||
| 201 | $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); |
||
| 202 | $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); |
||
| 203 | } |
||
| 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 | |||
| 268 | return new JSONResponse([ |
||
| 269 | 'status' => 'success', |
||
| 270 | 'data' => [ |
||
| 271 | 'username' => $username, |
||
| 272 | ], |
||
| 276 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.