| Conditions | 8 |
| Paths | 10 |
| Total Lines | 122 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 58 | protected function validate(ErrorCollection $errors) |
||
| 59 | { |
||
| 60 | $request = $this->request; |
||
| 61 | $username = $request[User::USERNAME]; |
||
| 62 | $password = $request[User::PASSWORD]; |
||
| 63 | |||
| 64 | /* @var $uid int */ |
||
| 65 | |||
| 66 | $uid = $this->model |
||
| 67 | ->select('uid') |
||
| 68 | ->where('username = ? OR email = ?', $username, $username) |
||
| 69 | ->rc; |
||
| 70 | |||
| 71 | if (!$uid) |
||
| 72 | { |
||
| 73 | $errors |
||
| 74 | ->add(User::USERNAME) |
||
| 75 | ->add(User::PASSWORD) |
||
| 76 | ->add_generic("Unknown username/password combination."); |
||
| 77 | |||
| 78 | return false; |
||
| 79 | } |
||
| 80 | |||
| 81 | /* @var $user User */ |
||
| 82 | /* @var $metas MetaCollection */ |
||
| 83 | |||
| 84 | $user = $this->model[$uid]; |
||
| 85 | $metas = $user->metas; |
||
| 86 | |||
| 87 | $now = time(); |
||
| 88 | $login_unlock_time = $metas['login_unlock_time']; |
||
| 89 | |||
| 90 | if ($login_unlock_time) |
||
| 91 | { |
||
| 92 | if ($login_unlock_time > $now) |
||
| 93 | { |
||
| 94 | throw new \Exception |
||
| 95 | ( |
||
| 96 | \ICanBoogie\format("The user account has been locked after multiple failed login attempts. |
||
| 97 | An e-mail has been sent to unlock the account. Login attempts are locked until %time, |
||
| 98 | unless you unlock the account using the email sent.", [ |
||
| 99 | |||
| 100 | '%count' => $metas['failed_login_count'], |
||
| 101 | '%time' => I18n\format_date($login_unlock_time, 'HH:mm') |
||
| 102 | |||
| 103 | ]), |
||
| 104 | |||
| 105 | 403 |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | $metas['login_unlock_time'] = null; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (!$user->verify_password($password)) |
||
| 113 | { |
||
| 114 | $errors |
||
| 115 | ->add(User::USERNAME) |
||
| 116 | ->add(User::PASSWORD) |
||
| 117 | ->add_generic("Unknown username/password combination."); |
||
| 118 | |||
| 119 | $metas['failed_login_count'] += 1; |
||
| 120 | $metas['failed_login_time'] = $now; |
||
| 121 | |||
| 122 | if ($metas['failed_login_count'] >= 10) |
||
| 123 | { |
||
| 124 | $token = \ICanBoogie\generate_token(40, \ICanBoogie\TOKEN_ALPHA . \ICanBoogie\TOKEN_NUMERIC); |
||
| 125 | |||
| 126 | $metas['login_unlock_token'] = $token; |
||
| 127 | $metas['login_unlock_time'] = $now + 3600; |
||
| 128 | |||
| 129 | $until = I18n\format_date($now + 3600, 'HH:mm'); |
||
| 130 | |||
| 131 | $url = $this->app->site->url . '/api/users/unlock_login?' . http_build_query([ |
||
| 132 | |||
| 133 | 'username' => $username, |
||
| 134 | 'token' => $token, |
||
| 135 | 'continue' => $request->uri |
||
| 136 | |||
| 137 | ]); |
||
| 138 | |||
| 139 | $this->app->mail([ |
||
| 140 | |||
| 141 | 'destination' => $user->email, |
||
| 142 | 'from' => 'no-reply@' . $request->headers['Host'], |
||
| 143 | 'subject' => "Your account has been locked", |
||
| 144 | 'body' => <<<EOT |
||
| 145 | You receive this message because your account has been locked. |
||
| 146 | |||
| 147 | After multiple failed login attempts your account has been locked until $until. You can use the |
||
| 148 | following link to unlock your account and try to login again: |
||
| 149 | |||
| 150 | <$url> |
||
| 151 | |||
| 152 | If you forgot your password, you'll be able to request a new one. |
||
| 153 | |||
| 154 | If you didn't try to login neither forgot your password, this message might be the result of an |
||
| 155 | attack attempt on the website. If you think this is the case, please contact its admin. |
||
| 156 | |||
| 157 | The remote address of the request was: $request->ip. |
||
| 158 | EOT |
||
| 159 | ]); |
||
| 160 | |||
| 161 | unset($errors[User::PASSWORD]); |
||
| 162 | |||
| 163 | $errors->add_generic("Your account has been locked, a message has been sent to your e-mail address."); |
||
| 164 | } |
||
| 165 | |||
| 166 | return false; |
||
| 167 | } |
||
| 168 | |||
| 169 | if (!$user->is_admin && !$user->is_activated) |
||
| 170 | { |
||
| 171 | $errors->add_generic("User %username is not activated", [ '%username' => $username ]); |
||
| 172 | |||
| 173 | return false; |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->record = $user; |
||
| 177 | |||
| 178 | return true; |
||
| 179 | } |
||
| 180 | |||
| 213 |