| Conditions | 21 |
| Paths | 265 |
| Total Lines | 110 |
| Code Lines | 86 |
| Lines | 11 |
| Ratio | 10 % |
| 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 |
||
| 70 | public static function changeUserPassword($args) { |
||
| 71 | // Check if we are an user |
||
| 72 | \OC_JSON::callCheck(); |
||
| 73 | \OC_JSON::checkLoggedIn(); |
||
| 74 | |||
| 75 | $l = \OC::$server->getL10NFactory()->get('settings'); |
||
| 76 | if (isset($_POST['username'])) { |
||
| 77 | $username = $_POST['username']; |
||
| 78 | } else { |
||
| 79 | \OC_JSON::error(['data' => ['message' => $l->t('No user supplied')]]); |
||
| 80 | exit(); |
||
| 81 | } |
||
| 82 | |||
| 83 | $password = isset($_POST['password']) ? $_POST['password'] : null; |
||
| 84 | $recoveryPassword = isset($_POST['recoveryPassword']) ? $_POST['recoveryPassword'] : null; |
||
| 85 | |||
| 86 | $isUserAccessible = false; |
||
| 87 | $currentUserObject = \OC::$server->getUserSession()->getUser(); |
||
| 88 | $targetUserObject = \OC::$server->getUserManager()->get($username); |
||
| 89 | View Code Duplication | if($currentUserObject !== null && $targetUserObject !== null) { |
|
| 90 | $isUserAccessible = \OC::$server->getGroupManager()->getSubAdmin()->isUserAccessible($currentUserObject, $targetUserObject); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (\OC_User::isAdminUser(\OC_User::getUser())) { |
||
| 94 | $userstatus = 'admin'; |
||
| 95 | } elseif ($isUserAccessible) { |
||
| 96 | $userstatus = 'subadmin'; |
||
| 97 | } else { |
||
| 98 | \OC_JSON::error(['data' => ['message' => $l->t('Authentication error')]]); |
||
| 99 | exit(); |
||
| 100 | } |
||
| 101 | |||
| 102 | if (\OC_App::isEnabled('encryption')) { |
||
| 103 | //handle the recovery case |
||
| 104 | $crypt = new \OCA\Encryption\Crypto\Crypt( |
||
| 105 | \OC::$server->getLogger(), |
||
| 106 | \OC::$server->getUserSession(), |
||
| 107 | \OC::$server->getConfig(), |
||
| 108 | \OC::$server->getL10N('encryption')); |
||
| 109 | $keyStorage = \OC::$server->getEncryptionKeyStorage(); |
||
| 110 | $util = new \OCA\Encryption\Util( |
||
| 111 | new \OC\Files\View(), |
||
| 112 | $crypt, |
||
| 113 | \OC::$server->getLogger(), |
||
| 114 | \OC::$server->getUserSession(), |
||
| 115 | \OC::$server->getConfig(), |
||
| 116 | \OC::$server->getUserManager()); |
||
| 117 | $keyManager = new \OCA\Encryption\KeyManager( |
||
| 118 | $keyStorage, |
||
| 119 | $crypt, |
||
| 120 | \OC::$server->getConfig(), |
||
| 121 | \OC::$server->getUserSession(), |
||
| 122 | new \OCA\Encryption\Session(\OC::$server->getSession()), |
||
| 123 | \OC::$server->getLogger(), |
||
| 124 | $util); |
||
| 125 | $recovery = new \OCA\Encryption\Recovery( |
||
| 126 | \OC::$server->getUserSession(), |
||
| 127 | $crypt, |
||
| 128 | \OC::$server->getSecureRandom(), |
||
| 129 | $keyManager, |
||
| 130 | \OC::$server->getConfig(), |
||
| 131 | $keyStorage, |
||
| 132 | \OC::$server->getEncryptionFilesHelper(), |
||
| 133 | new \OC\Files\View()); |
||
| 134 | $recoveryAdminEnabled = $recovery->isRecoveryKeyEnabled(); |
||
| 135 | |||
| 136 | $validRecoveryPassword = false; |
||
| 137 | $recoveryEnabledForUser = false; |
||
| 138 | if ($recoveryAdminEnabled) { |
||
| 139 | $validRecoveryPassword = $keyManager->checkRecoveryPassword($recoveryPassword); |
||
| 140 | $recoveryEnabledForUser = $recovery->isRecoveryEnabledForUser($username); |
||
| 141 | } |
||
| 142 | |||
| 143 | if ($recoveryEnabledForUser && $recoveryPassword === '') { |
||
| 144 | \OC_JSON::error(['data' => [ |
||
| 145 | 'message' => $l->t('Please provide an admin recovery password; otherwise, all user data will be lost.') |
||
| 146 | ]]); |
||
| 147 | View Code Duplication | } elseif ($recoveryEnabledForUser && ! $validRecoveryPassword) { |
|
| 148 | \OC_JSON::error(['data' => [ |
||
| 149 | 'message' => $l->t('Wrong admin recovery password. Please check the password and try again.') |
||
| 150 | ]]); |
||
| 151 | } else { // now we know that everything is fine regarding the recovery password, let's try to change the password |
||
| 152 | $result = \OC_User::setPassword($username, $password, $recoveryPassword); |
||
| 153 | if (!$result && $recoveryEnabledForUser) { |
||
| 154 | \OC_JSON::error([ |
||
| 155 | "data" => [ |
||
| 156 | "message" => $l->t("Backend doesn't support password change, but the user's encryption key was successfully updated.") |
||
| 157 | ] |
||
| 158 | ]); |
||
| 159 | View Code Duplication | } elseif (!$result && !$recoveryEnabledForUser) { |
|
| 160 | \OC_JSON::error(["data" => ["message" => $l->t("Unable to change password" )]]); |
||
| 161 | } else { |
||
| 162 | self::sendNotificationMail($username); |
||
| 163 | \OC_JSON::success(["data" => ["username" => $username]]); |
||
| 164 | } |
||
| 165 | |||
| 166 | } |
||
| 167 | } else { // if encryption is disabled, proceed |
||
| 168 | try { |
||
| 169 | if (!is_null($password) && \OC_User::setPassword($username, $password)) { |
||
| 170 | self::sendNotificationMail($username); |
||
| 171 | \OC_JSON::success(['data' => ['username' => $username]]); |
||
| 172 | } else { |
||
| 173 | \OC_JSON::error(['data' => ['message' => $l->t('Unable to change password')]]); |
||
| 174 | } |
||
| 175 | } catch (\Exception $e) { |
||
| 176 | \OC_JSON::error(['data' => ['message' => $e->getMessage()]]); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 208 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.