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