Page MenuHomePhabricator
Paste P1948

SwitchUserWorkflow.php
ActivePublic

Authored by michaeljs1990 on Feb 26 2016, 12:02 AM.
Tags
None
Referenced Files
F1130465: SwitchUserWorkflow.php
Feb 26 2016, 12:02 AM
Subscribers
None
<?php
class SwitchUserWorkflow extends IntegratorWorkflow {
protected function didConstruct() {
$this
->setName('migrate-external-account')
->setExamples('**migrate-external-account** --username-old __olduser__ '.
'--username-new __newuser__')
->setSynopsis(pht(
'Associate a new user account with an old one so history is not lost. '.
'Only for external accounts.'))
->setArguments([
[
'name' => 'username-old',
'param' => 'string',
'help' => pht('Old username that is no longer active.'),
],
[
'name' => 'username-new',
'param' => 'string',
'help' => pht('New username for the user to use.'),
],
]);
}
public function execute(PhutilArgumentParser $args) {
$old_user = $this->requireArgument($args, 'username-old');
$new_user = $this->requireArgument($args, 'username-new');
$old_user_acct = id(new PhabricatorExternalAccount())
->loadOneWhere('accountId = %s', $old_user);
$new_user_acct = id(new PhabricatorExternalAccount())
->loadOneWhere('accountID = %s', $new_user);
$console = PhutilConsole::getConsole();
if (!$old_user_acct) {
throw $this->argumentUsageException('%s does not exist.', $old_user);
}
if (!$new_user_acct) {
throw $this->argumentUsageException('%s does not exist.', $new_user);
}
// set the new user account to point to the old user account id
$old_user_phid = $old_user_acct->getUserPHID();
$new_user_acct
->setUserPHID($old_user_phid)
->save();
$console->writeOut(pht(
"%s has been migrated to %s\n",
$old_user,
$new_user));
}
}