Conditions | 12 |
Paths | 24 |
Total Lines | 96 |
Code Lines | 48 |
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 |
||
201 | public function doChangePassword(array $data, $form) |
||
202 | { |
||
203 | $member = Security::getCurrentUser(); |
||
204 | // The user was logged in, check the current password |
||
205 | if ($member && ( |
||
206 | empty($data['OldPassword']) || |
||
207 | !$member->checkPassword($data['OldPassword'])->isValid() |
||
208 | ) |
||
209 | ) { |
||
210 | $form->sessionMessage( |
||
211 | _t( |
||
212 | 'SilverStripe\\Security\\Member.ERRORPASSWORDNOTMATCH', |
||
213 | 'Your current password does not match, please try again' |
||
214 | ), |
||
215 | 'bad' |
||
216 | ); |
||
217 | |||
218 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
219 | return $this->redirectBackToForm(); |
||
220 | } |
||
221 | |||
222 | if (!$member) { |
||
223 | if (Session::get('AutoLoginHash')) { |
||
224 | $member = Member::member_from_autologinhash(Session::get('AutoLoginHash')); |
||
225 | } |
||
226 | |||
227 | // The user is not logged in and no valid auto login hash is available |
||
228 | if (!$member) { |
||
229 | Session::clear('AutoLoginHash'); |
||
230 | |||
231 | return $this->redirect($this->addBackURLParam(Security::singleton()->Link('login'))); |
||
232 | } |
||
233 | } |
||
234 | |||
235 | // Check the new password |
||
236 | if (empty($data['NewPassword1'])) { |
||
237 | $form->sessionMessage( |
||
238 | _t( |
||
239 | 'SilverStripe\\Security\\Member.EMPTYNEWPASSWORD', |
||
240 | "The new password can't be empty, please try again" |
||
241 | ), |
||
242 | 'bad' |
||
243 | ); |
||
244 | |||
245 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
246 | return $this->redirectBackToForm(); |
||
247 | } |
||
248 | |||
249 | // Fail if passwords do not match |
||
250 | if ($data['NewPassword1'] !== $data['NewPassword2']) { |
||
251 | $form->sessionMessage( |
||
252 | _t( |
||
253 | 'SilverStripe\\Security\\Member.ERRORNEWPASSWORD', |
||
254 | 'You have entered your new password differently, try again' |
||
255 | ), |
||
256 | 'bad' |
||
257 | ); |
||
258 | |||
259 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
260 | return $this->redirectBackToForm(); |
||
261 | } |
||
262 | |||
263 | // Check if the new password is accepted |
||
264 | $validationResult = $member->changePassword($data['NewPassword1']); |
||
265 | if (!$validationResult->isValid()) { |
||
266 | $form->setSessionValidationResult($validationResult); |
||
267 | |||
268 | return $this->redirectBackToForm(); |
||
269 | } |
||
270 | |||
271 | // Clear locked out status |
||
272 | $member->LockedOutUntil = null; |
||
273 | $member->FailedLoginCount = null; |
||
274 | // Clear the members login hashes |
||
275 | $member->AutoLoginHash = null; |
||
276 | $member->AutoLoginExpired = DBDatetime::create()->now(); |
||
277 | $member->write(); |
||
278 | |||
279 | if ($member->canLogIn()->isValid()) { |
||
280 | Injector::inst()->get(IdentityStore::class)->logIn($member, false, $this->getRequest()); |
||
281 | } |
||
282 | |||
283 | // TODO Add confirmation message to login redirect |
||
284 | Session::clear('AutoLoginHash'); |
||
285 | |||
286 | // Redirect to backurl |
||
287 | $backURL = $this->getBackURL(); |
||
288 | if ($backURL) { |
||
289 | return $this->redirect($backURL); |
||
290 | } |
||
291 | |||
292 | // Redirect to default location - the login form saying "You are logged in as..." |
||
293 | $url = Security::singleton()->Link('login'); |
||
294 | |||
295 | return $this->redirect($url); |
||
296 | } |
||
297 | |||
311 |