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