Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / people / mail / PhabricatorPeopleEmailLoginMailEngine.php
blobc25b19c3c1066ace23a1f2b83eab7bf55c0fc9a4
1 <?php
3 final class PhabricatorPeopleEmailLoginMailEngine
4 extends PhabricatorPeopleMailEngine {
6 public function validateMail() {
7 $recipient = $this->getRecipient();
9 if ($recipient->getIsDisabled()) {
10 $this->throwValidationException(
11 pht('User is Disabled'),
12 pht(
13 'You can not send an email login link to this email address '.
14 'because the associated user account is disabled.'));
17 if (!$recipient->canEstablishWebSessions()) {
18 $this->throwValidationException(
19 pht('Not a Normal User'),
20 pht(
21 'You can not send an email login link to this email address '.
22 'because the associated user account is not a normal user account '.
23 'and can not log in to the web interface.'));
27 protected function newMail() {
28 $is_set_password = $this->isSetPasswordWorkflow();
30 if ($is_set_password) {
31 $subject = pht('[Phabricator] Account Password Link');
32 } else {
33 $subject = pht('[Phabricator] Account Login Link');
36 $recipient = $this->getRecipient();
38 PhabricatorSystemActionEngine::willTakeAction(
39 array($recipient->getPHID()),
40 new PhabricatorAuthEmailLoginAction(),
41 1);
43 $engine = new PhabricatorAuthSessionEngine();
44 $login_uri = $engine->getOneTimeLoginURI(
45 $recipient,
46 null,
47 PhabricatorAuthSessionEngine::ONETIME_RESET);
49 $is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
50 $have_passwords = $this->isPasswordAuthEnabled();
52 $body = array();
54 if ($is_set_password) {
55 $message_key = PhabricatorAuthEmailSetPasswordMessageType::MESSAGEKEY;
56 } else {
57 $message_key = PhabricatorAuthEmailLoginMessageType::MESSAGEKEY;
60 $message_body = PhabricatorAuthMessage::loadMessageText(
61 $recipient,
62 $message_key);
63 if (strlen($message_body)) {
64 $body[] = $this->newRemarkupText($message_body);
67 if ($have_passwords) {
68 if ($is_set_password) {
69 $body[] = pht(
70 'You can use this link to set a password on your account:'.
71 "\n\n %s\n",
72 $login_uri);
73 } else if ($is_serious) {
74 $body[] = pht(
75 "You can use this link to reset your Phabricator password:".
76 "\n\n %s\n",
77 $login_uri);
78 } else {
79 $body[] = pht(
80 "Condolences on forgetting your password. You can use this ".
81 "link to reset it:\n\n".
82 " %s\n\n".
83 "After you set a new password, consider writing it down on a ".
84 "sticky note and attaching it to your monitor so you don't ".
85 "forget again! Choosing a very short, easy-to-remember password ".
86 "like \"cat\" or \"1234\" might also help.\n\n".
87 "Best Wishes,\nPhabricator\n",
88 $login_uri);
91 } else {
92 $body[] = pht(
93 "You can use this login link to regain access to your Phabricator ".
94 "account:".
95 "\n\n".
96 " %s\n",
97 $login_uri);
100 $body = implode("\n\n", $body);
102 return id(new PhabricatorMetaMTAMail())
103 ->setSubject($subject)
104 ->setBody($body);
107 private function isPasswordAuthEnabled() {
108 return (bool)PhabricatorPasswordAuthProvider::getPasswordProvider();
111 private function isSetPasswordWorkflow() {
112 $sender = $this->getSender();
113 $recipient = $this->getRecipient();
115 // Users can hit the "login with an email link" workflow while trying to
116 // set a password on an account which does not yet have a password. We
117 // require they verify that they own the email address and send them
118 // through the email login flow. In this case, the messaging is slightly
119 // different.
121 if ($sender->getPHID()) {
122 if ($sender->getPHID() === $recipient->getPHID()) {
123 return true;
127 return false;