| Conditions | 13 |
| Paths | 54 |
| Total Lines | 119 |
| Code Lines | 70 |
| Lines | 25 |
| Ratio | 21.01 % |
| 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 |
||
| 44 | public function doChangePassword(array $data, $form) |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * @var LDAPService $service |
||
| 48 | */ |
||
| 49 | $service = Injector::inst()->get(LDAPService::class); |
||
| 50 | $member = Security::getCurrentUser(); |
||
| 51 | if ($member) { |
||
| 52 | try { |
||
| 53 | $userData = $service->getUserByGUID($member->GUID); |
||
| 54 | } catch (Exception $e) { |
||
| 55 | Injector::inst()->get('Logger')->error($e->getMessage()); |
||
| 56 | |||
| 57 | $form->clearMessage(); |
||
| 58 | $form->sessionMessage( |
||
| 59 | _t( |
||
| 60 | __CLASS__ . '.NOUSER', |
||
| 61 | 'Your account hasn\'t been setup properly, please contact an administrator.' |
||
| 62 | ), |
||
| 63 | 'bad' |
||
| 64 | ); |
||
| 65 | return $form->getController()->redirect($form->getController()->Link('changepassword')); |
||
| 66 | } |
||
| 67 | $loginResult = $service->authenticate($userData['samaccountname'], $data['OldPassword']); |
||
| 68 | View Code Duplication | if (!$loginResult['success']) { |
|
| 69 | $form->clearMessage(); |
||
| 70 | $form->sessionMessage( |
||
| 71 | _t( |
||
| 72 | 'SilverStripe\\Security\\Member.ERRORPASSWORDNOTMATCH', |
||
| 73 | 'Your current password does not match, please try again' |
||
| 74 | ), |
||
| 75 | 'bad' |
||
| 76 | ); |
||
| 77 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 78 | return $form->getController()->redirect($form->getController()->Link('changepassword')); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if (!$member) { |
||
| 83 | if ($this->getRequest()->getSession()->get('AutoLoginHash')) { |
||
| 84 | $member = Member::member_from_autologinhash($this->getRequest()->getSession()->get('AutoLoginHash')); |
||
| 85 | } |
||
| 86 | |||
| 87 | // The user is not logged in and no valid auto login hash is available |
||
| 88 | if (!$member) { |
||
| 89 | $this->getRequest()->getSession()->clear('AutoLoginHash'); |
||
| 90 | return $form->getController()->redirect($form->getController()->Link('login')); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // Check the new password |
||
| 95 | if (empty($data['NewPassword1'])) { |
||
| 96 | $form->clearMessage(); |
||
| 97 | $form->sessionMessage( |
||
| 98 | _t( |
||
| 99 | 'SilverStripe\\Security\\Member.EMPTYNEWPASSWORD', |
||
| 100 | "The new password can't be empty, please try again" |
||
| 101 | ), |
||
| 102 | 'bad' |
||
| 103 | ); |
||
| 104 | |||
| 105 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 106 | return $form->getController()->redirect($form->getController()->Link('changepassword')); |
||
| 107 | } elseif ($data['NewPassword1'] == $data['NewPassword2']) { |
||
| 108 | // Providing OldPassword to perform password _change_ operation. This will respect the |
||
| 109 | // password history policy. Unfortunately we cannot support password history policy on password _reset_ |
||
| 110 | // at the moment, which means it will not be enforced on SilverStripe-driven email password reset. |
||
| 111 | $oldPassword = !empty($data['OldPassword']) ? $data['OldPassword']: null; |
||
| 112 | |||
| 113 | /** @var ValidationResult $validationResult */ |
||
| 114 | $validationResult = $service->setPassword($member, $data['NewPassword1'], $oldPassword); |
||
| 115 | |||
| 116 | // try to catch connection and other errors that the ldap service can through |
||
| 117 | if ($validationResult->isValid()) { |
||
| 118 | Security::setCurrentUser($member); |
||
| 119 | |||
| 120 | $this->getRequest()->getSession()->clear('AutoLoginHash'); |
||
| 121 | |||
| 122 | // Clear locked out status |
||
| 123 | $member->LockedOutUntil = null; |
||
| 124 | $member->FailedLoginCount = null; |
||
| 125 | $member->write(); |
||
| 126 | |||
| 127 | if (!empty($_REQUEST['BackURL']) |
||
| 128 | // absolute redirection URLs may cause spoofing |
||
| 129 | && Director::is_site_url($_REQUEST['BackURL']) |
||
| 130 | ) { |
||
| 131 | $url = Director::absoluteURL($_REQUEST['BackURL']); |
||
| 132 | return $form->getController()->redirect($url); |
||
| 133 | } else { |
||
| 134 | // Redirect to default location - the login form saying "You are logged in as..." |
||
| 135 | $redirectURL = HTTP::setGetVar( |
||
| 136 | 'BackURL', |
||
| 137 | Director::absoluteBaseURL(), |
||
| 138 | $form->getController()->Link('login') |
||
| 139 | ); |
||
| 140 | return $form->getController()->redirect($redirectURL); |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | $form->clearMessage(); |
||
| 144 | $messages = implode('. ', array_column($validationResult->getMessages(), 'message')); |
||
| 145 | $form->sessionMessage($messages, 'bad'); |
||
| 146 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 147 | return $form->getController()->redirect($form->getController()->Link('changepassword')); |
||
| 148 | } |
||
| 149 | View Code Duplication | } else { |
|
| 150 | $form->clearMessage(); |
||
| 151 | $form->sessionMessage( |
||
| 152 | _t( |
||
| 153 | 'SilverStripe\\Security\\Member.ERRORNEWPASSWORD', |
||
| 154 | 'You have entered your new password differently, try again' |
||
| 155 | ), |
||
| 156 | 'bad' |
||
| 157 | ); |
||
| 158 | |||
| 159 | // redirect back to the form, instead of using redirectBack() which could send the user elsewhere. |
||
| 160 | return $form->getController()->redirect($form->getController()->Link('changepassword')); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 |