| Conditions | 12 |
| Paths | 23 |
| Total Lines | 84 |
| Code Lines | 52 |
| Lines | 4 |
| Ratio | 4.76 % |
| 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 |
||
| 63 | public function forgotPassword($data, $form) |
||
| 64 | { |
||
| 65 | /** @var Controller $controller */ |
||
| 66 | $controller = $form->getController(); |
||
| 67 | |||
| 68 | // No need to protect against injections, LDAPService will ensure that this is safe |
||
| 69 | $login = trim($data['Login']); |
||
| 70 | |||
| 71 | $service = Injector::inst()->get(LDAPService::class); |
||
| 72 | if (Email::is_valid_address($login)) { |
||
| 73 | if (Config::inst()->get(LDAPAuthenticator::class, 'allow_email_login') != 'yes') { |
||
| 74 | $form->sessionMessage( |
||
| 75 | _t( |
||
| 76 | 'LDAPLoginForm.USERNAMEINSTEADOFEMAIL', |
||
| 77 | 'Please enter your username instead of your email to get a password reset link.' |
||
| 78 | ), |
||
| 79 | 'bad' |
||
| 80 | ); |
||
| 81 | return $controller->redirect($controller->Link('lostpassword')); |
||
| 82 | } |
||
| 83 | $userData = $service->getUserByEmail($login); |
||
| 84 | } else { |
||
| 85 | $userData = $service->getUserByUsername($login); |
||
| 86 | } |
||
| 87 | // Avoid information disclosure by displaying the same status, |
||
| 88 | // regardless whether the email address actually exists |
||
| 89 | if (!isset($userData['objectguid'])) { |
||
| 90 | return $controller->redirect($controller->Link('passwordsent/') |
||
| 91 | . urlencode($data['Login'])); |
||
| 92 | } |
||
| 93 | |||
| 94 | $member = Member::get()->filter('GUID', $userData['objectguid'])->limit(1)->first(); |
||
| 95 | // User haven't been imported yet so do that now |
||
| 96 | View Code Duplication | if (!($member && $member->exists())) { |
|
| 97 | $member = new Member(); |
||
| 98 | $member->GUID = $userData['objectguid']; |
||
| 99 | } |
||
| 100 | |||
| 101 | // Update the users from LDAP so we are sure that the email is correct. |
||
| 102 | // This will also write the Member record. |
||
| 103 | $service->updateMemberFromLDAP($member); |
||
| 104 | |||
| 105 | // Allow vetoing forgot password requests |
||
| 106 | $results = $this->extend('forgotPassword', $member); |
||
| 107 | if ($results && is_array($results) && in_array(false, $results, true)) { |
||
| 108 | return $controller->redirect('lostpassword'); |
||
| 109 | } |
||
| 110 | |||
| 111 | if ($member) { |
||
| 112 | /** @see MemberLoginForm::forgotPassword */ |
||
| 113 | $token = $member->generateAutologinTokenAndStoreHash(); |
||
| 114 | $e = Email::create() |
||
| 115 | ->setSubject(_t('Member.SUBJECTPASSWORDRESET', 'Your password reset link', 'Email subject')) |
||
| 116 | ->setHTMLTemplate('SilverStripe\\Control\\Email\\ForgotPasswordEmail') |
||
| 117 | ->setData($member) |
||
| 118 | ->setData(['PasswordResetLink' => Security::getPasswordResetLink($member, $token)]); |
||
| 119 | $e->setTo($member->Email); |
||
| 120 | $e->send(); |
||
| 121 | return $controller->redirect($controller->Link('passwordsent/') . urlencode($data['Login'])); |
||
| 122 | } elseif ($data['Login']) { |
||
| 123 | // Avoid information disclosure by displaying the same status, |
||
| 124 | // regardless whether the email address actually exists |
||
| 125 | return $controller->redirect($controller->Link('passwordsent/') . urlencode($data['Login'])); |
||
| 126 | } else { |
||
| 127 | if (Config::inst()->get(LDAPAuthenticator::class, 'allow_email_login') === 'yes') { |
||
| 128 | $form->sessionMessage( |
||
| 129 | _t( |
||
| 130 | 'LDAPLoginForm.ENTERUSERNAMEOREMAIL', |
||
| 131 | 'Please enter your username or your email address to get a password reset link.' |
||
| 132 | ), |
||
| 133 | 'bad' |
||
| 134 | ); |
||
| 135 | } else { |
||
| 136 | $form->sessionMessage( |
||
| 137 | _t( |
||
| 138 | 'LDAPLoginForm.ENTERUSERNAME', |
||
| 139 | 'Please enter your username to get a password reset link.' |
||
| 140 | ), |
||
| 141 | 'bad' |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | return $controller->redirect($controller->Link('lostpassword')); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 219 |