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