Skip to content

Commit 2789055

Browse files
author
epriestley
committedJun 17, 2013
Add very basic bin/auth tool
Summary: Ref T1536. This script basically exists to restore access if/when users shoot themselves in the foot by disabling all auth providers and can no longer log in. Test Plan: {F46411} Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T1536 Differential Revision: https://secure.phabricator.com/D6205
1 parent fc2973c commit 2789055

6 files changed

+130
-1
lines changed
 

‎bin/auth

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../scripts/setup/manage_auth.php

‎scripts/setup/manage_auth.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$root = dirname(dirname(dirname(__FILE__)));
5+
require_once $root.'/scripts/__init_script__.php';
6+
7+
$args = new PhutilArgumentParser($argv);
8+
$args->setTagline('manage authentication');
9+
$args->setSynopsis(<<<EOSYNOPSIS
10+
**auth** __command__ [__options__]
11+
Manage Phabricator authentication configuration.
12+
13+
EOSYNOPSIS
14+
);
15+
$args->parseStandardArguments();
16+
17+
$workflows = array(
18+
new PhabricatorAuthManagementListWorkflow(),
19+
new PhutilHelpArgumentWorkflow(),
20+
);
21+
22+
$args->parseWorkflows($workflows);

‎src/__phutil_library_map__.php

+4
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,8 @@
823823
'PhabricatorAuthLinkController' => 'applications/auth/controller/PhabricatorAuthLinkController.php',
824824
'PhabricatorAuthListController' => 'applications/auth/controller/config/PhabricatorAuthListController.php',
825825
'PhabricatorAuthLoginController' => 'applications/auth/controller/PhabricatorAuthLoginController.php',
826+
'PhabricatorAuthManagementRecoverWorkflow' => 'applications/auth/management/PhabricatorAuthManagementRecoverWorkflow.php',
827+
'PhabricatorAuthManagementWorkflow' => 'applications/auth/management/PhabricatorAuthManagementWorkflow.php',
826828
'PhabricatorAuthNewController' => 'applications/auth/controller/config/PhabricatorAuthNewController.php',
827829
'PhabricatorAuthProvider' => 'applications/auth/provider/PhabricatorAuthProvider.php',
828830
'PhabricatorAuthProviderConfig' => 'applications/auth/storage/PhabricatorAuthProviderConfig.php',
@@ -2704,6 +2706,8 @@
27042706
1 => 'PhabricatorApplicationSearchResultsControllerInterface',
27052707
),
27062708
'PhabricatorAuthLoginController' => 'PhabricatorAuthController',
2709+
'PhabricatorAuthManagementRecoverWorkflow' => 'PhabricatorAuthManagementWorkflow',
2710+
'PhabricatorAuthManagementWorkflow' => 'PhutilArgumentWorkflow',
27072711
'PhabricatorAuthNewController' => 'PhabricatorAuthProviderConfigController',
27082712
'PhabricatorAuthProviderConfig' =>
27092713
array(

‎src/applications/auth/controller/PhabricatorAuthStartController.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public function processRequest() {
4747
return $this->renderError(
4848
pht(
4949
"This Phabricator install is not configured with any enabled ".
50-
"authentication providers which can be used to log in."));
50+
"authentication providers which can be used to log in. If you ".
51+
"have accidentally locked yourself out by disabling all providers, ".
52+
"you can use `phabricator/bin/auth recover <username>` to ".
53+
"recover access to an administrative account."));
5154
}
5255

5356
$next_uri = $request->getStr('next');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
final class PhabricatorAuthManagementRecoverWorkflow
4+
extends PhabricatorAuthManagementWorkflow {
5+
6+
protected function didConstruct() {
7+
$this
8+
->setName('recover')
9+
->setExamples('**recover** __username__')
10+
->setSynopsis(
11+
'Recover access to an administrative account if you have locked '.
12+
'yourself out of Phabricator.')
13+
->setArguments(
14+
array(
15+
'username' => array(
16+
'name' => 'username',
17+
'wildcard' => true,
18+
),
19+
));
20+
}
21+
22+
public function execute(PhutilArgumentParser $args) {
23+
24+
$can_recover = id(new PhabricatorPeopleQuery())
25+
->setViewer(PhabricatorUser::getOmnipotentUser())
26+
->withIsAdmin(true)
27+
->execute();
28+
if (!$can_recover) {
29+
throw new PhutilArgumentUsageException(
30+
pht(
31+
'This Phabricator installation has no recoverable administrator '.
32+
'accounts. You can use `bin/accountadmin` to create a new '.
33+
'administrator account or make an existing user an administrator.'));
34+
}
35+
$can_recover = mpull($can_recover, 'getUsername');
36+
sort($can_recover);
37+
$can_recover = implode(', ', $can_recover);
38+
39+
$usernames = $args->getArg('username');
40+
if (!$usernames) {
41+
throw new PhutilArgumentUsageException(
42+
pht('You must specify the username of the account to recover.'));
43+
} else if (count($usernames) > 1) {
44+
throw new PhutilArgumentUsageException(
45+
pht('You can only recover the username for one account.'));
46+
}
47+
48+
$username = head($usernames);
49+
50+
$user = id(new PhabricatorPeopleQuery())
51+
->setViewer(PhabricatorUser::getOmnipotentUser())
52+
->withUsernames(array($username))
53+
->executeOne();
54+
55+
if (!$user) {
56+
throw new PhutilArgumentUsageException(
57+
pht(
58+
'No such user "%s". Recoverable administrator accounts are: %s.',
59+
$username,
60+
$can_recover));
61+
}
62+
63+
if (!$user->getIsAdmin()) {
64+
throw new PhutilArgumentUsageException(
65+
pht(
66+
'You can only recover administrator accounts, but %s is not an '.
67+
'administrator. Recoverable administrator accounts are: %s.',
68+
$username,
69+
$can_recover));
70+
}
71+
72+
$console = PhutilConsole::getConsole();
73+
$console->writeOut(
74+
pht(
75+
'Use this link to recover access to the "%s" account:',
76+
$username));
77+
$console->writeOut("\n\n");
78+
$console->writeOut(" %s", $user->getEmailLoginURI());
79+
$console->writeOut("\n\n");
80+
$console->writeOut(
81+
pht(
82+
'After logging in, you can use the "Auth" application to add or '.
83+
'restore authentication providers and allow normal logins to '.
84+
'succeed.')."\n");
85+
86+
return 0;
87+
}
88+
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
abstract class PhabricatorAuthManagementWorkflow
4+
extends PhutilArgumentWorkflow {
5+
6+
final public function isExecutable() {
7+
return true;
8+
}
9+
10+
}

0 commit comments

Comments
 (0)
Failed to load comments.