Changeset View
Changeset View
Standalone View
Standalone View
src/applications/auth/controller/PhabricatorEmailLoginController.php
<?php | <?php | ||||
final class PhabricatorEmailLoginController | final class PhabricatorEmailLoginController | ||||
extends PhabricatorAuthController { | extends PhabricatorAuthController { | ||||
public function shouldRequireLogin() { | public function shouldRequireLogin() { | ||||
return false; | return false; | ||||
} | } | ||||
public function handleRequest(AphrontRequest $request) { | public function handleRequest(AphrontRequest $request) { | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$is_logged_in = $viewer->isLoggedIn(); | |||||
$e_email = true; | $e_email = true; | ||||
$e_captcha = true; | $e_captcha = true; | ||||
$errors = array(); | $errors = array(); | ||||
if ($is_logged_in) { | |||||
if (!$this->isPasswordAuthEnabled()) { | |||||
return $this->newDialog() | |||||
->setTitle(pht('No Password Auth')) | |||||
->appendParagraph( | |||||
pht( | |||||
'Password authentication is not enabled and you are already '. | |||||
'logged in. There is nothing for you here.')) | |||||
->addCancelButton('/', pht('Continue')); | |||||
} | |||||
$v_email = $viewer->loadPrimaryEmailAddress(); | |||||
} else { | |||||
$v_email = $request->getStr('email'); | $v_email = $request->getStr('email'); | ||||
} | |||||
if ($request->isFormPost()) { | if ($request->isFormPost()) { | ||||
$e_email = null; | $e_email = null; | ||||
$e_captcha = pht('Again'); | $e_captcha = pht('Again'); | ||||
if (!$is_logged_in) { | |||||
$captcha_ok = AphrontFormRecaptchaControl::processCaptcha($request); | $captcha_ok = AphrontFormRecaptchaControl::processCaptcha($request); | ||||
if (!$captcha_ok) { | if (!$captcha_ok) { | ||||
$errors[] = pht('Captcha response is incorrect, try again.'); | $errors[] = pht('Captcha response is incorrect, try again.'); | ||||
$e_captcha = pht('Invalid'); | $e_captcha = pht('Invalid'); | ||||
} | } | ||||
} | |||||
if (!strlen($v_email)) { | if (!strlen($v_email)) { | ||||
$errors[] = pht('You must provide an email address.'); | $errors[] = pht('You must provide an email address.'); | ||||
$e_email = pht('Required'); | $e_email = pht('Required'); | ||||
} | } | ||||
if (!$errors) { | if (!$errors) { | ||||
// NOTE: Don't validate the email unless the captcha is good; this makes | // NOTE: Don't validate the email unless the captcha is good; this makes | ||||
Show All 36 Lines | if ($request->isFormPost()) { | ||||
'account has at least one verified address, you can only send '. | 'account has at least one verified address, you can only send '. | ||||
'password reset links to one of the verified addresses. Try '. | 'password reset links to one of the verified addresses. Try '. | ||||
'a verified address instead.'); | 'a verified address instead.'); | ||||
$e_email = pht('Unverified'); | $e_email = pht('Unverified'); | ||||
} | } | ||||
} | } | ||||
if (!$errors) { | if (!$errors) { | ||||
$body = $this->newAccountLoginMailBody($target_user); | $body = $this->newAccountLoginMailBody( | ||||
$target_user, | |||||
$is_logged_in); | |||||
if ($is_logged_in) { | |||||
$subject = pht('[Phabricator] Account Password Link'); | |||||
$instructions = pht( | |||||
'An email has been sent containing a link you can use to set '. | |||||
'a password for your account.'); | |||||
} else { | |||||
$subject = pht('[Phabricator] Account Login Link'); | |||||
$instructions = pht( | |||||
'An email has been sent containing a link you can use to log '. | |||||
'in to your account.'); | |||||
} | |||||
$mail = id(new PhabricatorMetaMTAMail()) | $mail = id(new PhabricatorMetaMTAMail()) | ||||
->setSubject(pht('[Phabricator] Account Login Link')) | ->setSubject($subject) | ||||
->setForceDelivery(true) | ->setForceDelivery(true) | ||||
->addRawTos(array($target_email->getAddress())) | ->addRawTos(array($target_email->getAddress())) | ||||
->setBody($body) | ->setBody($body) | ||||
->saveAndSend(); | ->saveAndSend(); | ||||
return $this->newDialog() | return $this->newDialog() | ||||
->setTitle(pht('Check Your Email')) | ->setTitle(pht('Check Your Email')) | ||||
->setShortTitle(pht('Email Sent')) | ->setShortTitle(pht('Email Sent')) | ||||
->appendParagraph( | ->appendParagraph($instructions) | ||||
pht('An email has been sent with a link you can use to log in.')) | |||||
->addCancelButton('/', pht('Done')); | ->addCancelButton('/', pht('Done')); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
$form = id(new AphrontFormView()) | $form = id(new AphrontFormView()) | ||||
->setViewer($viewer); | ->setViewer($viewer); | ||||
if ($this->isPasswordAuthEnabled()) { | if ($this->isPasswordAuthEnabled()) { | ||||
if ($is_logged_in) { | |||||
$title = pht('Set Password'); | |||||
$form->appendRemarkupInstructions( | |||||
pht( | |||||
'A password reset link will be sent to your primary email '. | |||||
'address. Follow the link to set an account password.')); | |||||
} else { | |||||
$title = pht('Password Reset'); | |||||
$form->appendRemarkupInstructions( | $form->appendRemarkupInstructions( | ||||
pht( | pht( | ||||
'To reset your password, provide your email address. An email '. | 'To reset your password, provide your email address. An email '. | ||||
'with a login link will be sent to you.')); | 'with a login link will be sent to you.')); | ||||
} | |||||
} else { | } else { | ||||
$title = pht('Email Login'); | |||||
$form->appendRemarkupInstructions( | $form->appendRemarkupInstructions( | ||||
pht( | pht( | ||||
'To access your account, provide your email address. An email '. | 'To access your account, provide your email address. An email '. | ||||
'with a login link will be sent to you.')); | 'with a login link will be sent to you.')); | ||||
} | } | ||||
$form | if ($is_logged_in) { | ||||
->appendControl( | $address_control = new AphrontFormStaticControl(); | ||||
id(new AphrontFormTextControl()) | } else { | ||||
->setLabel(pht('Email Address')) | $address_control = id(new AphrontFormTextControl()) | ||||
->setName('email') | ->setName('email') | ||||
->setValue($v_email) | ->setError($e_email); | ||||
->setError($e_email)) | } | ||||
->appendControl( | |||||
$address_control | |||||
->setLabel(pht('Email Address')) | |||||
->setValue($v_email); | |||||
$form | |||||
->appendControl($address_control); | |||||
if (!$is_logged_in) { | |||||
$form->appendControl( | |||||
id(new AphrontFormRecaptchaControl()) | id(new AphrontFormRecaptchaControl()) | ||||
->setLabel(pht('Captcha')) | ->setLabel(pht('Captcha')) | ||||
->setError($e_captcha)); | ->setError($e_captcha)); | ||||
if ($this->isPasswordAuthEnabled()) { | |||||
$title = pht('Password Reset'); | |||||
} else { | |||||
$title = pht('Email Login'); | |||||
} | } | ||||
return $this->newDialog() | return $this->newDialog() | ||||
->setTitle($title) | ->setTitle($title) | ||||
->setErrors($errors) | ->setErrors($errors) | ||||
->setWidth(AphrontDialogView::WIDTH_FORM) | ->setWidth(AphrontDialogView::WIDTH_FORM) | ||||
->appendForm($form) | ->appendForm($form) | ||||
->addCancelButton('/auth/start/') | ->addCancelButton('/auth/start/') | ||||
->addSubmitButton(pht('Send Email')); | ->addSubmitButton(pht('Send Email')); | ||||
} | } | ||||
private function newAccountLoginMailBody(PhabricatorUser $user) { | private function newAccountLoginMailBody( | ||||
PhabricatorUser $user, | |||||
$is_logged_in) { | |||||
$engine = new PhabricatorAuthSessionEngine(); | $engine = new PhabricatorAuthSessionEngine(); | ||||
$uri = $engine->getOneTimeLoginURI( | $uri = $engine->getOneTimeLoginURI( | ||||
$user, | $user, | ||||
null, | null, | ||||
PhabricatorAuthSessionEngine::ONETIME_RESET); | PhabricatorAuthSessionEngine::ONETIME_RESET); | ||||
$is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business'); | $is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business'); | ||||
$have_passwords = $this->isPasswordAuthEnabled(); | $have_passwords = $this->isPasswordAuthEnabled(); | ||||
if ($have_passwords) { | if ($have_passwords) { | ||||
if ($is_serious) { | if ($is_logged_in) { | ||||
$body = pht( | |||||
'You can use this link to set a password on your account:'. | |||||
"\n\n %s\n", | |||||
$uri); | |||||
} else if ($is_serious) { | |||||
$body = pht( | $body = pht( | ||||
"You can use this link to reset your Phabricator password:". | "You can use this link to reset your Phabricator password:". | ||||
"\n\n %s\n", | "\n\n %s\n", | ||||
$uri); | $uri); | ||||
} else { | } else { | ||||
$body = pht( | $body = pht( | ||||
"Condolences on forgetting your password. You can use this ". | "Condolences on forgetting your password. You can use this ". | ||||
"link to reset it:\n\n". | "link to reset it:\n\n". | ||||
Show All 25 Lines |