| Conditions | 11 |
| Paths | 17 |
| Total Lines | 65 |
| 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 |
||
| 112 | public function authenticate() |
||
| 113 | { |
||
| 114 | if (!in_array($this->getApplicationKey(), $this->getApplicationKeys())) { |
||
| 115 | return new Result(Result::FAILURE, $this->getIdentity(), array('Invalid application key')); |
||
| 116 | } |
||
| 117 | |||
| 118 | $identity = $this->getIdentity(); |
||
| 119 | $applicationId = '@' . $this->getApplicationIdentifier(); |
||
| 120 | $applicationIdIndex = strrpos($identity, $applicationId); |
||
| 121 | //$login = (0 < $applicationIdIndex && strlen($identity) - strlen($applicationId) == $applicationIdIndex)?substr($identity, 0, $applicationIdIndex):$identity; |
||
| 122 | $login = $identity; |
||
| 123 | $users = $this->getRepository(); |
||
| 124 | /* @var \Auth\Entity\User $user */ |
||
| 125 | $user = $users->findByLogin($login, ['allowDeactivated' => true]); |
||
| 126 | $filter = new CredentialFilter(); |
||
| 127 | $credential = $this->getCredential(); |
||
| 128 | |||
| 129 | $loginSuccess = false; |
||
| 130 | $loginResult = array(); |
||
| 131 | |||
| 132 | if (0 < $applicationIdIndex && strlen($identity) - strlen($applicationId) == $applicationIdIndex) { |
||
| 133 | $this->serviceManager->get('Core/Log')->debug('User ' . $login . ', login with correct suffix: '); |
||
| 134 | // the login ends with the applicationID, therefore use the secret key |
||
| 135 | // the external login must be the form 'xxxxx@yyyy' where yyyy is the matching suffix to the external application key |
||
| 136 | if (isset($user)) { |
||
| 137 | if ($user->getSecret() == $filter->filter($credential)) { |
||
| 138 | $loginSuccess = true; |
||
| 139 | } else { |
||
| 140 | $loginSuccess = false; |
||
| 141 | $this->serviceManager->get('Core/Log')->info('User ' . $login . ', secret: ' . $user->getSecret() . ' != loginPassword: ' . $filter->filter($credential) . ' (' . $credential . ')'); |
||
| 142 | } |
||
| 143 | } else { |
||
| 144 | $user = $users->create( |
||
| 145 | array( |
||
| 146 | 'login' => $login, |
||
| 147 | 'password' => $credential, |
||
| 148 | 'secret' => $filter->filter($credential), |
||
| 149 | 'role' => 'recruiter' |
||
| 150 | ) |
||
| 151 | ); |
||
| 152 | $users->store($user); |
||
| 153 | $loginSuccess = true; |
||
| 154 | $loginResult = array('firstLogin' => true); |
||
| 155 | } |
||
| 156 | } elseif (isset($user)) { |
||
| 157 | $this->serviceManager->get('Core/Log')->debug('User ' . $login . ', login with incorrect suffix: '); |
||
| 158 | if ($user->getCredential() == $filter->filter($credential)) { |
||
| 159 | $this->serviceManager->get('Core/Log')->debug('User ' . $login . ', credentials are equal'); |
||
| 160 | $loginSuccess = true; |
||
| 161 | } elseif (!empty($applicationId)) { |
||
| 162 | $this->serviceManager->get('Core/Log')->debug('User ' . $login . ', credentials are not equal'); |
||
| 163 | // TODO: remove this code as soon as the secret key has been fully established |
||
| 164 | // basically this does allow an external login with an applicationIndex match against the User-Password |
||
| 165 | // the way it had been used in the start |
||
| 166 | if ($user->getCredential() == $filter->filter($credential)) { |
||
| 167 | $this->serviceManager->get('Core/Log')->debug('User ' . $login . ', credentials2 test'); |
||
| 168 | $loginSuccess = true; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | if (!$loginSuccess) { |
||
| 174 | return new Result(Result::FAILURE_CREDENTIAL_INVALID, $identity, array('User not known or invalid credential')); |
||
| 175 | } |
||
| 176 | return new Result(Result::SUCCESS, $user->getId(), $loginResult); |
||
| 177 | } |
||
| 179 |