Skip to content

Commit 58f2cb2

Browse files
author
epriestley
committed
Provide a script for batch creating user accounts
Summary: Make it a little easier to create a bunch of accounts if your company has more than like 5 employees. Test Plan: Ran "add_user.php" to create new users. Created new users from the web console. Reviewers: btrahan, jungejason, rguerin Reviewed By: btrahan CC: aran, btrahan, rguerin Differential Revision: https://secure.phabricator.com/D1336
1 parent d16454d commit 58f2cb2

File tree

6 files changed

+130
-42
lines changed

6 files changed

+130
-42
lines changed

scripts/user/add_user.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/*
5+
* Copyright 2012 Facebook, Inc.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
$root = dirname(dirname(dirname(__FILE__)));
21+
require_once $root.'/scripts/__init_script__.php';
22+
23+
phutil_require_module('phutil', 'console');
24+
phutil_require_module('phutil', 'future/exec');
25+
26+
if ($argc !== 5) {
27+
echo "usage: add_user.php <username> <email> <realname> <admin_user>\n";
28+
exit(1);
29+
}
30+
31+
$username = $argv[1];
32+
$email = $argv[2];
33+
$realname = $argv[3];
34+
$admin = $argv[4];
35+
36+
$admin = id(new PhabricatorUser())->loadOneWhere(
37+
'username = %s',
38+
$argv[4]);
39+
if (!$admin) {
40+
throw new Exception(
41+
"Admin user must be the username of a valid Phabricator account, used ".
42+
"to send the new user a welcome email.");
43+
}
44+
45+
$existing_user = id(new PhabricatorUser())->loadOneWhere(
46+
'username = %s',
47+
$username);
48+
if ($existing_user) {
49+
throw new Exception(
50+
"There is already a user with the username '{$username}'!");
51+
}
52+
53+
$existing_user = id(new PhabricatorUser())->loadOneWhere(
54+
'email = %s',
55+
$email);
56+
if ($existing_user) {
57+
throw new Exception(
58+
"There is already a user with the email '{$email}'!");
59+
}
60+
61+
$user = new PhabricatorUser();
62+
$user->setUsername($username);
63+
$user->setEmail($email);
64+
$user->setRealname($realname);
65+
$user->save();
66+
67+
$user->sendWelcomeEmail($admin);
68+
69+
echo "Created user '{$username}' (realname='{$realname}', email='{$email}').\n";

src/applications/people/controller/edit/PhabricatorPeopleEditController.php

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/*
4-
* Copyright 2011 Facebook, Inc.
4+
* Copyright 2012 Facebook, Inc.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -121,7 +121,6 @@ private function processBasicRequest(PhabricatorUser $user) {
121121
$errors = array();
122122

123123
$welcome_checked = true;
124-
$is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
125124

126125
$request = $this->getRequest();
127126
if ($request->isFormPost()) {
@@ -175,44 +174,7 @@ private function processBasicRequest(PhabricatorUser $user) {
175174
$log->save();
176175

177176
if ($welcome_checked) {
178-
$admin_username = $admin->getUserName();
179-
$admin_realname = $admin->getRealName();
180-
$user_username = $user->getUserName();
181-
182-
$base_uri = PhabricatorEnv::getProductionURI('/');
183-
184-
$uri = $user->getEmailLoginURI();
185-
$body = <<<EOBODY
186-
Welcome to Phabricator!
187-
188-
{$admin_username} ({$admin_realname}) has created an account for you.
189-
190-
Username: {$user_username}
191-
192-
To login to Phabricator, follow this link and set a password:
193-
194-
{$uri}
195-
196-
After you have set a password, you can login in the future by going here:
197-
198-
{$base_uri}
199-
200-
EOBODY;
201-
202-
if (!$is_serious) {
203-
$body .= <<<EOBODY
204-
Love,
205-
Phabricator
206-
207-
EOBODY;
208-
}
209-
210-
$mail = id(new PhabricatorMetaMTAMail())
211-
->addTos(array($user->getPHID()))
212-
->setSubject('[Phabricator] Welcome to Phabricator')
213-
->setBody($body)
214-
->setFrom($admin->getPHID())
215-
->saveAndSend();
177+
$user->sendWelcomeEmail($admin);
216178
}
217179
}
218180

src/applications/people/controller/edit/__init__.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88

99
phutil_require_module('phabricator', 'aphront/response/404');
1010
phutil_require_module('phabricator', 'aphront/response/redirect');
11-
phutil_require_module('phabricator', 'applications/metamta/storage/mail');
1211
phutil_require_module('phabricator', 'applications/people/controller/base');
1312
phutil_require_module('phabricator', 'applications/people/storage/log');
1413
phutil_require_module('phabricator', 'applications/people/storage/user');
15-
phutil_require_module('phabricator', 'infrastructure/env');
1614
phutil_require_module('phabricator', 'view/form/base');
1715
phutil_require_module('phabricator', 'view/form/control/checkbox');
1816
phutil_require_module('phabricator', 'view/form/control/select');

src/applications/people/storage/user/PhabricatorUser.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,47 @@ public function updateNameTokens() {
457457
}
458458
}
459459

460+
public function sendWelcomeEmail(PhabricatorUser $admin) {
461+
$admin_username = $admin->getUserName();
462+
$admin_realname = $admin->getRealName();
463+
$user_username = $this->getUserName();
464+
$is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
465+
466+
$base_uri = PhabricatorEnv::getProductionURI('/');
467+
468+
$uri = $this->getEmailLoginURI();
469+
$body = <<<EOBODY
470+
Welcome to Phabricator!
471+
472+
{$admin_username} ({$admin_realname}) has created an account for you.
473+
474+
Username: {$user_username}
475+
476+
To login to Phabricator, follow this link and set a password:
477+
478+
{$uri}
479+
480+
After you have set a password, you can login in the future by going here:
481+
482+
{$base_uri}
483+
484+
EOBODY;
485+
486+
if (!$is_serious) {
487+
$body .= <<<EOBODY
488+
489+
Love,
490+
Phabricator
491+
492+
EOBODY;
493+
}
494+
495+
$mail = id(new PhabricatorMetaMTAMail())
496+
->addTos(array($this->getPHID()))
497+
->setSubject('[Phabricator] Welcome to Phabricator')
498+
->setBody($body)
499+
->setFrom($admin->getPHID())
500+
->saveAndSend();
501+
}
502+
460503
}

src/applications/people/storage/user/__init__.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
phutil_require_module('phabricator', 'aphront/writeguard');
10+
phutil_require_module('phabricator', 'applications/metamta/storage/mail');
1011
phutil_require_module('phabricator', 'applications/people/storage/base');
1112
phutil_require_module('phabricator', 'applications/people/storage/log');
1213
phutil_require_module('phabricator', 'applications/people/storage/preferences');

src/docs/configuration/configuring_accounts_and_registration.diviner

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ To manage accounts from the web, login as an administrator account and go to
3838
##/people/## or click "People" on the homepage. Provided you're an admin,
3939
you'll see options to create or edit accounts.
4040

41+
= Managing Accounts from the Command Line =
42+
43+
You can use ##scripts/user/add_user.php## to batch create accounts. Run it
44+
like:
45+
46+
$ ./add_user.php <username> <email> <realname> <admin>
47+
48+
For example:
49+
50+
$ ./add_user.php alincoln alincoln@logcabin.com 'Abraham Lincoln' tjefferson
51+
52+
This will create a new ##alincoln## user and send them a "Welcome to
53+
Phabricator" email from ##tjefferson## with instructions on how to log in and
54+
set a password.
55+
4156
= Configuring Facebook OAuth =
4257

4358
You can configure Facebook OAuth to allow login, login and registration, or

0 commit comments

Comments
 (0)