3 final class PhabricatorEmailLoginController
4 extends PhabricatorAuthController
{
6 public function shouldRequireLogin() {
10 public function handleRequest(AphrontRequest
$request) {
11 $viewer = $this->getViewer();
12 $is_logged_in = $viewer->isLoggedIn();
19 if (!$this->isPasswordAuthEnabled()) {
20 return $this->newDialog()
21 ->setTitle(pht('No Password Auth'))
24 'Password authentication is not enabled and you are already '.
25 'logged in. There is nothing for you here.'))
26 ->addCancelButton('/', pht('Continue'));
29 $v_email = $viewer->loadPrimaryEmailAddress();
31 $v_email = $request->getStr('email');
34 if ($request->isFormPost()) {
36 $e_captcha = pht('Again');
39 $captcha_ok = AphrontFormRecaptchaControl
::processCaptcha($request);
41 $errors[] = pht('Captcha response is incorrect, try again.');
42 $e_captcha = pht('Invalid');
46 if (!strlen($v_email)) {
47 $errors[] = pht('You must provide an email address.');
48 $e_email = pht('Required');
52 // NOTE: Don't validate the email unless the captcha is good; this makes
53 // it expensive to fish for valid email addresses while giving the user
54 // a better error if they goof their email.
56 $action_actor = PhabricatorSystemActionEngine
::newActorFromRequest(
59 PhabricatorSystemActionEngine
::willTakeAction(
61 new PhabricatorAuthTryEmailLoginAction(),
64 $target_email = id(new PhabricatorUserEmail())->loadOneWhere(
70 $target_user = id(new PhabricatorUser())->loadOneWhere(
72 $target_email->getUserPHID());
77 pht('There is no account associated with that email address.');
78 $e_email = pht('Invalid');
81 // If this address is unverified, only send a reset link to it if
82 // the account has no verified addresses. This prevents an opportunistic
83 // attacker from compromising an account if a user adds an email
84 // address but mistypes it and doesn't notice.
86 // (For a newly created account, all the addresses may be unverified,
87 // which is why we'll send to an unverified address in that case.)
89 if ($target_email && !$target_email->getIsVerified()) {
90 $verified_addresses = id(new PhabricatorUserEmail())->loadAllWhere(
91 'userPHID = %s AND isVerified = 1',
92 $target_email->getUserPHID());
93 if ($verified_addresses) {
95 'That email address is not verified, but the account it is '.
96 'connected to has at least one other verified address. When an '.
97 'account has at least one verified address, you can only send '.
98 'password reset links to one of the verified addresses. Try '.
99 'a verified address instead.');
100 $e_email = pht('Unverified');
105 $target_address = new PhutilEmailAddress($target_email->getAddress());
107 $user_log = PhabricatorUserLog
::initializeNewLog(
109 $target_user->getPHID(),
110 PhabricatorEmailLoginUserLogType
::LOGTYPE
);
112 $mail_engine = id(new PhabricatorPeopleEmailLoginMailEngine())
114 ->setRecipient($target_user)
115 ->setRecipientAddress($target_address)
116 ->setActivityLog($user_log);
119 $mail_engine->validateMail();
120 } catch (PhabricatorPeopleMailEngineException
$ex) {
121 return $this->newDialog()
122 ->setTitle($ex->getTitle())
123 ->appendParagraph($ex->getBody())
124 ->addCancelButton('/auth/start/', pht('Done'));
127 $mail_engine->sendMail();
131 'An email has been sent containing a link you can use to set '.
132 'a password for your account.');
135 'An email has been sent containing a link you can use to log '.
136 'in to your account.');
139 return $this->newDialog()
140 ->setTitle(pht('Check Your Email'))
141 ->setShortTitle(pht('Email Sent'))
142 ->appendParagraph($instructions)
143 ->addCancelButton('/', pht('Done'));
148 $form = id(new AphrontFormView())
149 ->setViewer($viewer);
151 if ($this->isPasswordAuthEnabled()) {
153 $title = pht('Set Password');
154 $form->appendRemarkupInstructions(
156 'A password reset link will be sent to your primary email '.
157 'address. Follow the link to set an account password.'));
159 $title = pht('Password Reset');
160 $form->appendRemarkupInstructions(
162 'To reset your password, provide your email address. An email '.
163 'with a login link will be sent to you.'));
166 $title = pht('Email Login');
167 $form->appendRemarkupInstructions(
169 'To access your account, provide your email address. An email '.
170 'with a login link will be sent to you.'));
174 $address_control = new AphrontFormStaticControl();
176 $address_control = id(new AphrontFormTextControl())
178 ->setError($e_email);
182 ->setLabel(pht('Email Address'))
183 ->setValue($v_email);
186 ->appendControl($address_control);
188 if (!$is_logged_in) {
189 $form->appendControl(
190 id(new AphrontFormRecaptchaControl())
191 ->setLabel(pht('Captcha'))
192 ->setError($e_captcha));
195 return $this->newDialog()
198 ->setWidth(AphrontDialogView
::WIDTH_FORM
)
200 ->addCancelButton('/auth/start/')
201 ->addSubmitButton(pht('Send Email'));
204 private function isPasswordAuthEnabled() {
205 return (bool)PhabricatorPasswordAuthProvider
::getPasswordProvider();