Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 35 | class Controller { |
||
| 36 | public static function changePersonalPassword($args) { |
||
|
|
|||
| 37 | // Check if we are an user |
||
| 38 | \OC_JSON::callCheck(); |
||
| 39 | \OC_JSON::checkLoggedIn(); |
||
| 40 | |||
| 41 | $username = \OC_User::getUser(); |
||
| 42 | $password = isset($_POST['personal-password']) ? $_POST['personal-password'] : null; |
||
| 43 | $oldPassword = isset($_POST['oldpassword']) ? $_POST['oldpassword'] : ''; |
||
| 44 | |||
| 45 | View Code Duplication | if (!\OC_User::checkPassword($username, $oldPassword)) { |
|
| 46 | $l = \OC::$server->getL10NFactory()->get('settings'); |
||
| 47 | \OC_JSON::error(["data" => ["message" => $l->t("Wrong password")]]); |
||
| 48 | exit(); |
||
| 49 | } |
||
| 50 | View Code Duplication | if ($oldPassword === $password) { |
|
| 51 | $l = \OC::$server->getL10NFactory()->get('settings'); |
||
| 52 | \OC_JSON::error(["data" => ["message" => $l->t("The new password can not be the same as the previous one")]]); |
||
| 53 | exit(); |
||
| 54 | } |
||
| 55 | try { |
||
| 56 | if (!is_null($password) && \OC_User::setPassword($username, $password)) { |
||
| 57 | \OC::$server->getUserSession()->updateSessionTokenPassword($password); |
||
| 58 | |||
| 59 | self::sendNotificationMail($username); |
||
| 60 | |||
| 61 | \OC_JSON::success(); |
||
| 62 | } else { |
||
| 63 | \OC_JSON::error(); |
||
| 64 | } |
||
| 65 | } catch (\Exception $e) { |
||
| 66 | \OC_JSON::error(['data' => ['message' => $e->getMessage()]]); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 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 | |||
| 181 | private static function sendNotificationMail($username) { |
||
| 207 | } |
||
| 208 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.