Differential D18901 Diff 45357 src/applications/auth/management/PhabricatorAuthManagementRecoverWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/auth/management/PhabricatorAuthManagementRecoverWorkflow.php
| <?php | <?php | ||||
| final class PhabricatorAuthManagementRecoverWorkflow | final class PhabricatorAuthManagementRecoverWorkflow | ||||
| extends PhabricatorAuthManagementWorkflow { | extends PhabricatorAuthManagementWorkflow { | ||||
| protected function didConstruct() { | protected function didConstruct() { | ||||
| $this | $this | ||||
| ->setName('recover') | ->setName('recover') | ||||
| ->setExamples('**recover** __username__') | ->setExamples('**recover** __username__') | ||||
| ->setSynopsis( | ->setSynopsis( | ||||
| pht( | pht( | ||||
| 'Recover access to an administrative account if you have locked '. | 'Recover access to an account if you have locked yourself out '. | ||||
| 'yourself out of Phabricator.')) | 'of Phabricator.')) | ||||
| ->setArguments( | ->setArguments( | ||||
| array( | array( | ||||
| 'username' => array( | 'username' => array( | ||||
| 'name' => 'username', | 'name' => 'username', | ||||
| 'wildcard' => true, | 'wildcard' => true, | ||||
| ), | ), | ||||
| )); | )); | ||||
| } | } | ||||
| public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
| $can_recover = id(new PhabricatorPeopleQuery()) | |||||
| ->setViewer($this->getViewer()) | |||||
| ->withIsAdmin(true) | |||||
| ->execute(); | |||||
| if (!$can_recover) { | |||||
| throw new PhutilArgumentUsageException( | |||||
| pht( | |||||
| 'This Phabricator installation has no recoverable administrator '. | |||||
| 'accounts. You can use `%s` to create a new administrator '. | |||||
| 'account or make an existing user an administrator.', | |||||
| 'bin/accountadmin')); | |||||
| } | |||||
| $can_recover = mpull($can_recover, 'getUsername'); | |||||
| sort($can_recover); | |||||
| $can_recover = implode(', ', $can_recover); | |||||
| $usernames = $args->getArg('username'); | $usernames = $args->getArg('username'); | ||||
| if (!$usernames) { | if (!$usernames) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht('You must specify the username of the account to recover.')); | pht('You must specify the username of the account to recover.')); | ||||
| } else if (count($usernames) > 1) { | } else if (count($usernames) > 1) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht('You can only recover the username for one account.')); | pht('You can only recover the username for one account.')); | ||||
| } | } | ||||
| $username = head($usernames); | $username = head($usernames); | ||||
| $user = id(new PhabricatorPeopleQuery()) | $user = id(new PhabricatorPeopleQuery()) | ||||
| ->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
| ->withUsernames(array($username)) | ->withUsernames(array($username)) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if (!$user) { | if (!$user) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'No such user "%s". Recoverable administrator accounts are: %s.', | 'No such user "%s" to recover.', | ||||
| $username, | $username)); | ||||
| $can_recover)); | |||||
| } | |||||
| if (!$user->getIsAdmin()) { | |||||
| throw new PhutilArgumentUsageException( | |||||
| pht( | |||||
| 'You can only recover administrator accounts, but %s is not an '. | |||||
| 'administrator. Recoverable administrator accounts are: %s.', | |||||
| $username, | |||||
| $can_recover)); | |||||
| } | } | ||||
| if (!$user->canEstablishWebSessions()) { | if (!$user->canEstablishWebSessions()) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'This account ("%s") can not establish web sessions, so it is '. | 'This account ("%s") can not establish web sessions, so it is '. | ||||
| 'not possible to generate a functional recovery link. Special '. | 'not possible to generate a functional recovery link. Special '. | ||||
| 'accounts like daemons and mailing lists can not log in via the '. | 'accounts like daemons and mailing lists can not log in via the '. | ||||
| Show All 30 Lines | |||||