1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-20 09:18:48 +02:00

Migrate the OAuthInfo table to the ExternalAccount table

Summary: Ref T1536. Migrates the OAuthInfo table to ExternalAccount, and makes `PhabricatorUserOAuthInfo` a wrapper for an ExternalAccount.

Test Plan: Logged in with OAuth, registered with OAuth, linked/unlinked OAuth accounts, checked OAuth status screen, deleted an account with related OAuth.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T1536

Differential Revision: https://secure.phabricator.com/D6172
This commit is contained in:
epriestley 2013-06-14 07:04:41 -07:00
parent bce4b7addf
commit 8111dc74bf
7 changed files with 205 additions and 39 deletions

View file

@ -0,0 +1,66 @@
<?php
// NOTE: We aren't using PhabricatorUserOAuthInfo anywhere here because it is
// getting nuked in a future diff.
$table = new PhabricatorUser();
$table_name = 'user_oauthinfo';
$conn_w = $table->establishConnection('w');
$xaccount = new PhabricatorExternalAccount();
echo "Migrating OAuth to ExternalAccount...\n";
$domain_map = array(
'disqus' => 'disqus.com',
'facebook' => 'facebook.com',
'github' => 'github.com',
'google' => 'google.com',
);
try {
$phabricator_oauth_uri = new PhutilURI(
PhabricatorEnv::getEnvConfig('phabricator.oauth-uri'));
$domain_map['phabricator'] = $phabricator_oauth_uri->getDomain();
} catch (Exception $ex) {
// Ignore; this likely indicates that we have removed `phabricator.oauth-uri`
// in some future diff.
}
$rows = queryfx_all(
$conn_w,
'SELECT * FROM user_oauthinfo');
foreach ($rows as $row) {
echo "Migrating row ID #".$row['id'].".\n";
$user = id(new PhabricatorUser())->loadOneWhere(
'id = %d',
$row['userID']);
if (!$user) {
echo "Bad user ID!\n";
continue;
}
$domain = idx($domain_map, $row['oauthProvider']);
if (empty($domain)) {
echo "Unknown OAuth provider!\n";
continue;
}
$xaccount = id(new PhabricatorExternalAccount())
->setUserPHID($user->getPHID())
->setAccountType($row['oauthProvider'])
->setAccountDomain($domain)
->setAccountID($row['oauthUID'])
->setAccountURI($row['accountURI'])
->setUsername($row['accountName'])
->setDateCreated($row['dateCreated']);
try {
$xaccount->save();
} catch (Exception $ex) {
phlog($ex);
}
}
echo "Done.\n";

View file

@ -3405,7 +3405,6 @@ phutil_register_library_map(array(
'PhabricatorUserEmail' => 'PhabricatorUserDAO', 'PhabricatorUserEmail' => 'PhabricatorUserDAO',
'PhabricatorUserLDAPInfo' => 'PhabricatorUserDAO', 'PhabricatorUserLDAPInfo' => 'PhabricatorUserDAO',
'PhabricatorUserLog' => 'PhabricatorUserDAO', 'PhabricatorUserLog' => 'PhabricatorUserDAO',
'PhabricatorUserOAuthInfo' => 'PhabricatorUserDAO',
'PhabricatorUserPreferences' => 'PhabricatorUserDAO', 'PhabricatorUserPreferences' => 'PhabricatorUserDAO',
'PhabricatorUserProfile' => 'PhabricatorUserDAO', 'PhabricatorUserProfile' => 'PhabricatorUserDAO',
'PhabricatorUserProfileEditor' => 'PhabricatorApplicationTransactionEditor', 'PhabricatorUserProfileEditor' => 'PhabricatorApplicationTransactionEditor',

View file

@ -120,7 +120,6 @@ final class PhabricatorOAuthLoginController
$provider_name))); $provider_name)));
$dialog->addHiddenInput('confirm_token', $provider->getAccessToken()); $dialog->addHiddenInput('confirm_token', $provider->getAccessToken());
$dialog->addHiddenInput('state', $this->oauthState); $dialog->addHiddenInput('state', $this->oauthState);
$dialog->addHiddenInput('scope', $oauth_info->getTokenScope());
$dialog->addSubmitButton('Link Accounts'); $dialog->addSubmitButton('Link Accounts');
$dialog->addCancelButton($provider->getSettingsPanelURI()); $dialog->addCancelButton($provider->getSettingsPanelURI());
@ -284,24 +283,16 @@ final class PhabricatorOAuthLoginController
$provider->getProviderKey(), $provider->getProviderKey(),
$provider->retrieveUserID()); $provider->retrieveUserID());
$scope = $this->getRequest()->getStr('scope');
if (!$oauth_info) { if (!$oauth_info) {
$oauth_info = new PhabricatorUserOAuthInfo(); $oauth_info = new PhabricatorUserOAuthInfo(
new PhabricatorExternalAccount());
$oauth_info->setOAuthProvider($provider->getProviderKey()); $oauth_info->setOAuthProvider($provider->getProviderKey());
$oauth_info->setOAuthUID($provider->retrieveUserID()); $oauth_info->setOAuthUID($provider->retrieveUserID());
// some providers don't tell you what scope you got, so default
// to the minimum Phabricator requires rather than assuming no scope
if (!$scope) {
$scope = $provider->getMinimumScope();
}
} }
$oauth_info->setAccountURI($provider->retrieveUserAccountURI()); $oauth_info->setAccountURI($provider->retrieveUserAccountURI());
$oauth_info->setAccountName($provider->retrieveUserAccountName()); $oauth_info->setAccountName($provider->retrieveUserAccountName());
$oauth_info->setToken($provider->getAccessToken()); $oauth_info->setToken($provider->getAccessToken());
$oauth_info->setTokenStatus('unused');
$oauth_info->setTokenScope($scope);
return $oauth_info; return $oauth_info;
} }

View file

@ -170,7 +170,6 @@ final class PhabricatorOAuthDefaultRegistrationController
$form = new AphrontFormView(); $form = new AphrontFormView();
$form $form
->addHiddenInput('confirm_token', $provider->getAccessToken()) ->addHiddenInput('confirm_token', $provider->getAccessToken())
->addHiddenInput('expires', $oauth_info->getTokenExpires())
->addHiddenInput('state', $this->getOAuthState()) ->addHiddenInput('state', $this->getOAuthState())
->setUser($request->getUser()) ->setUser($request->getUser())
->setAction($action_path) ->setAction($action_path)

View file

@ -310,13 +310,6 @@ final class PhabricatorUserEditor extends PhabricatorEditor {
$ldap->delete(); $ldap->delete();
} }
$oauths = id(new PhabricatorUserOAuthInfo())->loadAllWhere(
'userID = %d',
$user->getID());
foreach ($oauths as $oauth) {
$oauth->delete();
}
$externals = id(new PhabricatorExternalAccount())->loadAllWhere( $externals = id(new PhabricatorExternalAccount())->loadAllWhere(
'userPHID = %s', 'userPHID = %s',
$user->getPHID()); $user->getPHID());

View file

@ -1,46 +1,160 @@
<?php <?php
final class PhabricatorUserOAuthInfo extends PhabricatorUserDAO { final class PhabricatorUserOAuthInfo {
protected $userID; private $account;
protected $oauthProvider; private $token;
protected $oauthUID;
protected $accountURI; public function getID() {
protected $accountName; return $this->account->getID();
}
protected $token; public function setToken($token) {
protected $tokenExpires = 0; $this->token = $token;
protected $tokenScope = ''; return $this;
protected $tokenStatus = 'unused'; }
public function getToken() {
return $this->token;
}
public function __construct(PhabricatorExternalAccount $account) {
$this->account = $account;
}
public function setAccountURI($value) {
$this->account->setAccountURI($value);
return $this;
}
public function getAccountURI() {
return $this->account->getAccountURI();
}
public function setAccountName($account_name) {
$this->account->setUsername($account_name);
return $this;
}
public function getAccountName() {
return $this->account->getUsername();
}
public function setUserID($user_id) {
$user = id(new PhabricatorUser())->loadOneWhere('id = %d', $user_id);
if (!$user) {
throw new Exception("No such user with given ID!");
}
$this->account->setUserPHID($user->getPHID());
return $this;
}
public function getUserID() {
$phid = $this->account->getUserPHID();
if (!$phid) {
return null;
}
$user = id(new PhabricatorUser())->loadOneWhere('phid = %s', $phid);
if (!$user) {
throw new Exception("No such user with given PHID!");
}
return $user->getID();
}
public function setOAuthUID($oauth_uid) {
$this->account->setAccountID($oauth_uid);
return $this;
}
public function getOAuthUID() {
return $this->account->getAccountID();
}
public function setOAuthProvider($oauth_provider) {
$domain = self::getDomainForProvider($oauth_provider);
$this->account->setAccountType($oauth_provider);
$this->account->setAccountDomain($domain);
return $this;
}
public function getOAuthProvider() {
return $this->account->getAccountType();
}
public static function loadOneByUserAndProviderKey( public static function loadOneByUserAndProviderKey(
PhabricatorUser $user, PhabricatorUser $user,
$provider_key) { $provider_key) {
return id(new PhabricatorUserOAuthInfo())->loadOneWhere( $account = id(new PhabricatorExternalAccount())->loadOneWhere(
'userID = %d AND oauthProvider = %s', 'userPHID = %s AND accountType = %s AND accountDomain = %s',
$user->getID(), $user->getPHID(),
$provider_key); $provider_key,
self::getDomainForProvider($provider_key));
if (!$account) {
return null;
}
return new PhabricatorUserOAuthInfo($account);
} }
public static function loadAllOAuthProvidersByUser( public static function loadAllOAuthProvidersByUser(
PhabricatorUser $user) { PhabricatorUser $user) {
return id(new PhabricatorUserOAuthInfo())->loadAllWhere( $accounts = id(new PhabricatorExternalAccount())->loadAllWhere(
'userID = %d', 'userPHID = %s',
$user->getID()); $user->getPHID());
$results = array();
foreach ($accounts as $account) {
$results[] = new PhabricatorUserOAuthInfo($account);
}
return $results;
} }
public static function loadOneByProviderKeyAndAccountID( public static function loadOneByProviderKeyAndAccountID(
$provider_key, $provider_key,
$account_id) { $account_id) {
return id(new PhabricatorUserOAuthInfo())->loadOneWhere( $account = id(new PhabricatorExternalAccount())->loadOneWhere(
'oauthProvider = %s and oauthUID = %s', 'accountType = %s AND accountDomain = %s AND accountID = %s',
$provider_key, $provider_key,
self::getDomainForProvider($provider_key),
$account_id); $account_id);
if (!$account) {
return null;
}
return new PhabricatorUserOAuthInfo($account);
} }
public function save() {
$this->account->save();
return $this;
}
private static function getDomainForProvider($provider_key) {
$domain_map = array(
'disqus' => 'disqus.com',
'facebook' => 'facebook.com',
'github' => 'github.com',
'google' => 'google.com',
);
try {
$phabricator_oauth_uri = new PhutilURI(
PhabricatorEnv::getEnvConfig('phabricator.oauth-uri'));
$domain_map['phabricator'] = $phabricator_oauth_uri->getDomain();
} catch (Exception $ex) {
// Ignore.
}
return idx($domain_map, $provider_key);
}
} }

View file

@ -1358,6 +1358,10 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList {
'type' => 'sql', 'type' => 'sql',
'name' => $this->getPatchPath('20130607.xaccount.sql'), 'name' => $this->getPatchPath('20130607.xaccount.sql'),
), ),
'20130611.migrateoauth.php' => array(
'type' => 'php',
'name' => $this->getPatchPath('20130611.migrateoauth.php'),
),
); );
} }
} }