Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/people/mail/PhabricatorPeopleEmailLoginMailEngine.php
13459 views
1
<?php
2
3
final class PhabricatorPeopleEmailLoginMailEngine
4
extends PhabricatorPeopleMailEngine {
5
6
public function validateMail() {
7
$recipient = $this->getRecipient();
8
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.'));
15
}
16
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.'));
24
}
25
}
26
27
protected function newMail() {
28
$is_set_password = $this->isSetPasswordWorkflow();
29
30
if ($is_set_password) {
31
$subject = pht(
32
'[%s] Account Password Link',
33
PlatformSymbols::getPlatformServerName());
34
} else {
35
$subject = pht(
36
'[%s] Account Login Link',
37
PlatformSymbols::getPlatformServerName());
38
}
39
40
$recipient = $this->getRecipient();
41
42
PhabricatorSystemActionEngine::willTakeAction(
43
array($recipient->getPHID()),
44
new PhabricatorAuthEmailLoginAction(),
45
1);
46
47
$engine = new PhabricatorAuthSessionEngine();
48
$login_uri = $engine->getOneTimeLoginURI(
49
$recipient,
50
null,
51
PhabricatorAuthSessionEngine::ONETIME_RESET);
52
53
$is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
54
$have_passwords = $this->isPasswordAuthEnabled();
55
56
$body = array();
57
58
if ($is_set_password) {
59
$message_key = PhabricatorAuthEmailSetPasswordMessageType::MESSAGEKEY;
60
} else {
61
$message_key = PhabricatorAuthEmailLoginMessageType::MESSAGEKEY;
62
}
63
64
$message_body = PhabricatorAuthMessage::loadMessageText(
65
$recipient,
66
$message_key);
67
if ($message_body !== null && strlen($message_body)) {
68
$body[] = $this->newRemarkupText($message_body);
69
}
70
71
if ($have_passwords) {
72
if ($is_set_password) {
73
$body[] = pht(
74
'You can use this link to set a password on your account:'.
75
"\n\n %s\n",
76
$login_uri);
77
} else if ($is_serious) {
78
$body[] = pht(
79
"You can use this link to reset your password:".
80
"\n\n %s\n",
81
$login_uri);
82
} else {
83
$body[] = pht(
84
"Condolences on forgetting your password. You can use this ".
85
"link to reset it:\n\n".
86
" %s\n\n".
87
"After you set a new password, consider writing it down on a ".
88
"sticky note and attaching it to your monitor so you don't ".
89
"forget again! Choosing a very short, easy-to-remember password ".
90
"like \"cat\" or \"1234\" might also help.\n\n".
91
"Best Wishes,\nPhabricator\n",
92
$login_uri);
93
94
}
95
} else {
96
$body[] = pht(
97
"You can use this login link to regain access to your account:".
98
"\n\n".
99
" %s\n",
100
$login_uri);
101
}
102
103
$body = implode("\n\n", $body);
104
105
return id(new PhabricatorMetaMTAMail())
106
->setSubject($subject)
107
->setBody($body);
108
}
109
110
private function isPasswordAuthEnabled() {
111
return (bool)PhabricatorPasswordAuthProvider::getPasswordProvider();
112
}
113
114
private function isSetPasswordWorkflow() {
115
$sender = $this->getSender();
116
$recipient = $this->getRecipient();
117
118
// Users can hit the "login with an email link" workflow while trying to
119
// set a password on an account which does not yet have a password. We
120
// require they verify that they own the email address and send them
121
// through the email login flow. In this case, the messaging is slightly
122
// different.
123
124
if ($sender->getPHID()) {
125
if ($sender->getPHID() === $recipient->getPHID()) {
126
return true;
127
}
128
}
129
130
return false;
131
}
132
133
}
134
135