Conditions | 15 |
Paths | 18 |
Total Lines | 82 |
Code Lines | 43 |
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 |
||
83 | public function authenticate(array $data, HTTPRequest $request, ValidationResult &$result = null) |
||
84 | { |
||
85 | $result = $result ?: ValidationResult::create(); |
||
86 | /** @var LDAPService $service */ |
||
87 | $service = Injector::inst()->get(LDAPService::class); |
||
88 | $login = trim($data['Login']); |
||
89 | if (Email::is_valid_address($login)) { |
||
90 | if (Config::inst()->get(self::class, 'allow_email_login') != 'yes') { |
||
91 | $result->addError( |
||
92 | _t( |
||
93 | __CLASS__ . '.PLEASEUSEUSERNAME', |
||
94 | 'Please enter your username instead of your email to log in.' |
||
95 | ) |
||
96 | ); |
||
97 | return null; |
||
98 | } |
||
99 | $username = $service->getUsernameByEmail($login); |
||
100 | |||
101 | // No user found with this email. |
||
102 | if (!$username) { |
||
103 | if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { |
||
104 | if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { |
||
105 | { |
||
106 | return $fallbackMember; |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | $result->addError(_t(__CLASS__ . '.INVALIDCREDENTIALS', 'Invalid credentials')); |
||
112 | return null; |
||
113 | } |
||
114 | } else { |
||
115 | $username = $login; |
||
116 | } |
||
117 | |||
118 | $serviceAuthenticationResult = $service->authenticate($username, $data['Password']); |
||
119 | $success = $serviceAuthenticationResult['success'] === true; |
||
120 | |||
121 | if (!$success) { |
||
122 | /* |
||
123 | * Try the fallback method if admin or it failed for anything other than invalid credentials |
||
124 | * This is to avoid having an unhandled exception error thrown by PasswordEncryptor::create_for_algorithm() |
||
125 | */ |
||
126 | if (Config::inst()->get(self::class, 'fallback_authenticator') === 'yes') { |
||
127 | if (!in_array($serviceAuthenticationResult['code'], [Result::FAILURE_CREDENTIAL_INVALID]) |
||
128 | || $username === 'admin' |
||
129 | ) { |
||
130 | if ($fallbackMember = $this->fallbackAuthenticate($data, $request)) { |
||
131 | return $fallbackMember; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | $result->addError($serviceAuthenticationResult['message']); |
||
137 | |||
138 | return null; |
||
139 | } |
||
140 | $data = $service->getUserByUsername($serviceAuthenticationResult['identity']); |
||
141 | if (!$data) { |
||
|
|||
142 | $result->addError( |
||
143 | _t( |
||
144 | __CLASS__ . '.PROBLEMFINDINGDATA', |
||
145 | 'There was a problem retrieving your user data' |
||
146 | ) |
||
147 | ); |
||
148 | return null; |
||
149 | } |
||
150 | |||
151 | // LDAPMemberExtension::memberLoggedIn() will update any other AD attributes mapped to Member fields |
||
152 | $member = Member::get()->filter('GUID', $data['objectguid'])->limit(1)->first(); |
||
153 | if (!($member && $member->exists())) { |
||
154 | $member = new Member(); |
||
155 | $member->GUID = $data['objectguid']; |
||
156 | } |
||
157 | |||
158 | // Update the users from LDAP so we are sure that the email is correct. |
||
159 | // This will also write the Member record. |
||
160 | $service->updateMemberFromLDAP($member, $data); |
||
161 | |||
162 | $request->getSession()->clear('BackURL'); |
||
163 | |||
164 | return $member; |
||
165 | } |
||
243 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.