Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/people/controller/PhabricatorPeopleNewController.php
12262 views
1
<?php
2
3
final class PhabricatorPeopleNewController
4
extends PhabricatorPeopleController {
5
6
public function handleRequest(AphrontRequest $request) {
7
$type = $request->getURIData('type');
8
$admin = $request->getUser();
9
10
id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession(
11
$admin,
12
$request,
13
$this->getApplicationURI());
14
15
$is_bot = false;
16
$is_list = false;
17
switch ($type) {
18
case 'standard':
19
$this->requireApplicationCapability(
20
PeopleCreateUsersCapability::CAPABILITY);
21
break;
22
case 'bot':
23
$is_bot = true;
24
break;
25
case 'list':
26
$is_list = true;
27
break;
28
default:
29
return new Aphront404Response();
30
}
31
32
$user = new PhabricatorUser();
33
$require_real_name = PhabricatorEnv::getEnvConfig('user.require-real-name');
34
35
$e_username = true;
36
$e_realname = $require_real_name ? true : null;
37
$e_email = true;
38
$errors = array();
39
40
$welcome_checked = true;
41
42
$new_email = null;
43
44
if ($request->isFormPost()) {
45
$welcome_checked = $request->getInt('welcome');
46
47
$user->setUsername($request->getStr('username'));
48
49
$new_email = $request->getStr('email');
50
if (!strlen($new_email)) {
51
$errors[] = pht('Email is required.');
52
$e_email = pht('Required');
53
} else if (!PhabricatorUserEmail::isValidAddress($new_email)) {
54
$errors[] = PhabricatorUserEmail::describeValidAddresses();
55
$e_email = pht('Invalid');
56
} else if (!PhabricatorUserEmail::isAllowedAddress($new_email)) {
57
$errors[] = PhabricatorUserEmail::describeAllowedAddresses();
58
$e_email = pht('Not Allowed');
59
} else {
60
$e_email = null;
61
}
62
63
$user->setRealName($request->getStr('realname'));
64
65
if (!strlen($user->getUsername())) {
66
$errors[] = pht('Username is required.');
67
$e_username = pht('Required');
68
} else if (!PhabricatorUser::validateUsername($user->getUsername())) {
69
$errors[] = PhabricatorUser::describeValidUsername();
70
$e_username = pht('Invalid');
71
} else {
72
$e_username = null;
73
}
74
75
if (!strlen($user->getRealName()) && $require_real_name) {
76
$errors[] = pht('Real name is required.');
77
$e_realname = pht('Required');
78
} else {
79
$e_realname = null;
80
}
81
82
if (!$errors) {
83
try {
84
85
$email = id(new PhabricatorUserEmail())
86
->setAddress($new_email)
87
->setIsVerified(0);
88
89
// Automatically approve the user, since an admin is creating them.
90
$user->setIsApproved(1);
91
92
// If the user is a bot or list, approve their email too.
93
if ($is_bot || $is_list) {
94
$email->setIsVerified(1);
95
}
96
97
id(new PhabricatorUserEditor())
98
->setActor($admin)
99
->createNewUser($user, $email);
100
101
if ($is_bot) {
102
id(new PhabricatorUserEditor())
103
->setActor($admin)
104
->makeSystemAgentUser($user, true);
105
}
106
107
if ($is_list) {
108
id(new PhabricatorUserEditor())
109
->setActor($admin)
110
->makeMailingListUser($user, true);
111
}
112
113
if ($welcome_checked) {
114
$welcome_engine = id(new PhabricatorPeopleWelcomeMailEngine())
115
->setSender($admin)
116
->setRecipient($user);
117
if ($welcome_engine->canSendMail()) {
118
$welcome_engine->sendMail();
119
}
120
}
121
122
$response = id(new AphrontRedirectResponse())
123
->setURI('/p/'.$user->getUsername().'/');
124
return $response;
125
} catch (AphrontDuplicateKeyQueryException $ex) {
126
$errors[] = pht('Username and email must be unique.');
127
128
$same_username = id(new PhabricatorUser())
129
->loadOneWhere('username = %s', $user->getUsername());
130
$same_email = id(new PhabricatorUserEmail())
131
->loadOneWhere('address = %s', $new_email);
132
133
if ($same_username) {
134
$e_username = pht('Duplicate');
135
}
136
137
if ($same_email) {
138
$e_email = pht('Duplicate');
139
}
140
}
141
}
142
}
143
144
$form = id(new AphrontFormView())
145
->setUser($admin);
146
147
if ($is_bot) {
148
$title = pht('Create New Bot');
149
$form->appendRemarkupInstructions(
150
pht('You are creating a new **bot** user account.'));
151
} else if ($is_list) {
152
$title = pht('Create New Mailing List');
153
$form->appendRemarkupInstructions(
154
pht('You are creating a new **mailing list** user account.'));
155
} else {
156
$title = pht('Create New User');
157
$form->appendRemarkupInstructions(
158
pht('You are creating a new **standard** user account.'));
159
}
160
161
$form
162
->appendChild(
163
id(new AphrontFormTextControl())
164
->setLabel(pht('Username'))
165
->setName('username')
166
->setValue($user->getUsername())
167
->setError($e_username))
168
->appendChild(
169
id(new AphrontFormTextControl())
170
->setLabel(pht('Real Name'))
171
->setName('realname')
172
->setValue($user->getRealName())
173
->setError($e_realname))
174
->appendChild(
175
id(new AphrontFormTextControl())
176
->setLabel(pht('Email'))
177
->setName('email')
178
->setValue($new_email)
179
->setCaption(PhabricatorUserEmail::describeAllowedAddresses())
180
->setError($e_email));
181
182
if (!$is_bot && !$is_list) {
183
$form->appendChild(
184
id(new AphrontFormCheckboxControl())
185
->addCheckbox(
186
'welcome',
187
1,
188
pht(
189
'Send "Welcome to %s" email with login instructions.',
190
PlatformSymbols::getPlatformServerName()),
191
$welcome_checked));
192
}
193
194
$form
195
->appendChild(
196
id(new AphrontFormSubmitControl())
197
->addCancelButton($this->getApplicationURI())
198
->setValue(pht('Create User')));
199
200
if ($is_bot) {
201
$form
202
->appendChild(id(new AphrontFormDividerControl()))
203
->appendRemarkupInstructions(
204
pht(
205
'**Why do bot accounts need an email address?**'.
206
"\n\n".
207
'Although bots do not normally receive email, they can interact '.
208
'with other systems which require an email address. Examples '.
209
'include:'.
210
"\n\n".
211
" - If the account takes actions which //send// email, we need ".
212
" an address to use in the //From// header.\n".
213
" - If the account creates commits, Git and Mercurial require ".
214
" an email address for authorship.\n".
215
" - If you send email //to// this server on behalf of the ".
216
" account, the address can identify the sender.\n".
217
" - Some internal authentication functions depend on accounts ".
218
" having an email address.\n".
219
"\n\n".
220
"The address will automatically be verified, so you do not need ".
221
"to be able to receive mail at this address, and can enter some ".
222
"invalid or nonexistent (but correctly formatted) address like ".
223
"`[email protected]` if you prefer."));
224
}
225
226
$box = id(new PHUIObjectBoxView())
227
->setHeaderText($title)
228
->setFormErrors($errors)
229
->setBackground(PHUIObjectBoxView::WHITE_CONFIG)
230
->setForm($form);
231
232
$crumbs = $this->buildApplicationCrumbs();
233
$crumbs->addTextCrumb($title);
234
$crumbs->setBorder(true);
235
236
$view = id(new PHUITwoColumnView())
237
->setFooter($box);
238
239
return $this->newPage()
240
->setTitle($title)
241
->setCrumbs($crumbs)
242
->appendChild($view);
243
}
244
245
}
246
247