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 |
||
| 27 | class LDAPAuthenticator extends MemberAuthenticator |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $name = 'LDAP'; |
||
|
|
|||
| 33 | |||
| 34 | /** |
||
| 35 | * Set to 'yes' to indicate if this module should look up usernames in LDAP by matching the email addresses. |
||
| 36 | * |
||
| 37 | * CAVEAT #1: only set to 'yes' for systems that enforce email uniqueness. |
||
| 38 | * Otherwise only the first LDAP user with matching email will be accessible. |
||
| 39 | * |
||
| 40 | * CAVEAT #2: this is untested for systems that use LDAP with principal style usernames (i.e. [email protected]). |
||
| 41 | * The system will misunderstand emails for usernames with uncertain outcome. |
||
| 42 | * |
||
| 43 | * @var string 'no' or 'yes' |
||
| 44 | */ |
||
| 45 | private static $allow_email_login = 'no'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Set to 'yes' to fallback login attempts to {@link $fallback_authenticator}. |
||
| 49 | * This will occur if LDAP fails to authenticate the user. |
||
| 50 | * |
||
| 51 | * @var string 'no' or 'yes' |
||
| 52 | */ |
||
| 53 | private static $fallback_authenticator = 'no'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The class of {@link Authenticator} to use as the fallback authenticator. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private static $fallback_authenticator_class = MemberAuthenticator::class; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | public static function get_name() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param Controller $controller |
||
| 72 | * @return LDAPLoginForm |
||
| 73 | */ |
||
| 74 | public static function get_login_form(Controller $controller) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Performs the login, but will also create and sync the Member record on-the-fly, if not found. |
||
| 81 | * |
||
| 82 | * @param array $data |
||
| 83 | * @param HTTPRequest $request |
||
| 84 | * @param ValidationResult|null $result |
||
| 85 | * @return null|Member |
||
| 86 | */ |
||
| 87 | public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null) |
||
| 88 | { |
||
| 89 | $result = $result ?: ValidationResult::create(); |
||
| 90 | /** @var LDAPService $service */ |
||
| 91 | $service = Injector::inst()->get(LDAPService::class); |
||
| 92 | $login = trim($data['Login']); |
||
| 93 | if (Email::is_valid_address($login)) { |
||
| 94 | if (Config::inst()->get(self::class, 'allow_email_login') != 'yes') { |
||
| 95 | $result->addError( |
||
| 96 | _t( |
||
| 97 | 'LDAPAuthenticator.PLEASEUSEUSERNAME', |
||
| 98 | 'Please enter your username instead of your email to log in.' |
||
| 99 | ) |
||
| 100 | ); |
||
| 101 | return null; |
||
| 102 | } |
||
| 103 | $username = $service->getUsernameByEmail($login); |
||
| 104 | |||
| 105 | // No user found with this email. |
||
| 106 | View Code Duplication | if (!$username) { |
|
| 107 | if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { |
||
| 108 | if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { |
||
| 109 | { |
||
| 110 | return $fallbackMember; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $result->addError(_t('LDAPAuthenticator.INVALIDCREDENTIALS', 'Invalid credentials')); |
||
| 116 | return null; |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | $username = $login; |
||
| 120 | } |
||
| 121 | $serviceAuthenticationResult = $service->authenticate($username, $data['Password']); |
||
| 122 | $success = $serviceAuthenticationResult['success'] === true; |
||
| 123 | View Code Duplication | if (!$success) { |
|
| 124 | if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { |
||
| 125 | if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { |
||
| 126 | return $fallbackMember; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | $result->addError($serviceAuthenticationResult['message']); |
||
| 131 | |||
| 132 | return null; |
||
| 133 | } |
||
| 134 | $data = $service->getUserByUsername($serviceAuthenticationResult['identity']); |
||
| 135 | if (!$data) { |
||
| 136 | $result->addError( |
||
| 137 | _t( |
||
| 138 | 'LDAPAuthenticator.PROBLEMFINDINGDATA', |
||
| 139 | 'There was a problem retrieving your user data' |
||
| 140 | ) |
||
| 141 | ); |
||
| 142 | return null; |
||
| 143 | } |
||
| 144 | |||
| 145 | // LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields |
||
| 146 | $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); |
||
| 147 | View Code Duplication | if (!($member && $member->exists())) { |
|
| 148 | $member = new Member(); |
||
| 149 | $member->GUID = $data['objectguid']; |
||
| 150 | } |
||
| 151 | |||
| 152 | // Update the users from LDAP so we are sure that the email is correct. |
||
| 153 | // This will also write the Member record. |
||
| 154 | $service->updateMemberFromLDAP($member); |
||
| 155 | |||
| 156 | $request->getSession()->clear('BackURL'); |
||
| 157 | |||
| 158 | return $member; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Try to authenticate using the fallback authenticator. |
||
| 163 | * |
||
| 164 | * @param array $data |
||
| 165 | * @param HTTPRequest $request |
||
| 166 | * @return null|Member |
||
| 167 | */ |
||
| 168 | protected function fallbackAuthenticate($data, HTTPRequest $request) |
||
| 169 | { |
||
| 170 | if (array_key_exists('Login', $data) && !array_key_exists('Email', $data)) { |
||
| 171 | $data['Email'] = $data['Login']; |
||
| 172 | } |
||
| 173 | $authenticatorClass = Config::inst()->get(self::class, 'fallback_authenticator_class'); |
||
| 174 | if ($authenticator = Injector::inst()->get($authenticatorClass)) { |
||
| 175 | $result = call_user_func( |
||
| 176 | [ |
||
| 177 | $authenticator, |
||
| 178 | 'authenticate' |
||
| 179 | ], |
||
| 180 | $data, |
||
| 181 | $request |
||
| 182 | ); |
||
| 183 | return $result; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getLoginHandler($link) |
||
| 191 | |||
| 192 | public function supportedServices() |
||
| 193 | { |
||
| 201 | |||
| 202 | public function getLostPasswordHandler($link) |
||
| 206 | } |
||
| 207 |
This check marks private properties in classes that are never used. Those properties can be removed.