1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-01 02:10:59 +01:00

Migrate PhabricatorUserLDAPInfo to PhabricatorExternalAccount

Summary: Ref T1536. This is similar to D6172 but much simpler: we don't need to retain external interfaces here and can do a straight migration.

Test Plan: TBA

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T1536

Differential Revision: https://secure.phabricator.com/D6173
This commit is contained in:
epriestley 2013-06-16 09:55:55 -07:00
parent 9327e51446
commit 8744cdb699
10 changed files with 84 additions and 41 deletions

View file

@ -0,0 +1,41 @@
<?php
// NOTE: We aren't using PhabricatorUserLDAPInfo anywhere here because it is
// being nuked by this change
$table = new PhabricatorUser();
$table_name = 'user_ldapinfo';
$conn_w = $table->establishConnection('w');
$xaccount = new PhabricatorExternalAccount();
echo "Migrating LDAP to ExternalAccount...\n";
$rows = queryfx_all($conn_w, 'SELECT * FROM %T', $table_name);
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;
}
$xaccount = id(new PhabricatorExternalAccount())
->setUserPHID($user->getPHID())
->setAccountType('ldap')
->setAccountDomain('self')
->setAccountID($row['ldapUsername'])
->setUsername($row['ldapUsername'])
->setDateCreated($row['dateCreated']);
try {
$xaccount->save();
} catch (Exception $ex) {
phlog($ex);
}
}
echo "Done.\n";

View file

@ -1554,7 +1554,6 @@ phutil_register_library_map(array(
'PhabricatorUserDAO' => 'applications/people/storage/PhabricatorUserDAO.php', 'PhabricatorUserDAO' => 'applications/people/storage/PhabricatorUserDAO.php',
'PhabricatorUserEditor' => 'applications/people/editor/PhabricatorUserEditor.php', 'PhabricatorUserEditor' => 'applications/people/editor/PhabricatorUserEditor.php',
'PhabricatorUserEmail' => 'applications/people/storage/PhabricatorUserEmail.php', 'PhabricatorUserEmail' => 'applications/people/storage/PhabricatorUserEmail.php',
'PhabricatorUserLDAPInfo' => 'applications/people/storage/PhabricatorUserLDAPInfo.php',
'PhabricatorUserLog' => 'applications/people/storage/PhabricatorUserLog.php', 'PhabricatorUserLog' => 'applications/people/storage/PhabricatorUserLog.php',
'PhabricatorUserOAuthInfo' => 'applications/people/storage/PhabricatorUserOAuthInfo.php', 'PhabricatorUserOAuthInfo' => 'applications/people/storage/PhabricatorUserOAuthInfo.php',
'PhabricatorUserPreferences' => 'applications/settings/storage/PhabricatorUserPreferences.php', 'PhabricatorUserPreferences' => 'applications/settings/storage/PhabricatorUserPreferences.php',
@ -3403,7 +3402,6 @@ phutil_register_library_map(array(
'PhabricatorUserDAO' => 'PhabricatorLiskDAO', 'PhabricatorUserDAO' => 'PhabricatorLiskDAO',
'PhabricatorUserEditor' => 'PhabricatorEditor', 'PhabricatorUserEditor' => 'PhabricatorEditor',
'PhabricatorUserEmail' => 'PhabricatorUserDAO', 'PhabricatorUserEmail' => 'PhabricatorUserDAO',
'PhabricatorUserLDAPInfo' => 'PhabricatorUserDAO',
'PhabricatorUserLog' => 'PhabricatorUserDAO', 'PhabricatorUserLog' => 'PhabricatorUserDAO',
'PhabricatorUserPreferences' => 'PhabricatorUserDAO', 'PhabricatorUserPreferences' => 'PhabricatorUserDAO',
'PhabricatorUserProfile' => 'PhabricatorUserDAO', 'PhabricatorUserProfile' => 'PhabricatorUserDAO',

View file

@ -34,11 +34,13 @@ final class PhabricatorLDAPLoginController extends PhabricatorAuthController {
if ($current_user->getPHID()) { if ($current_user->getPHID()) {
if ($ldap_info->getID()) { if ($ldap_info->getID()) {
$existing_ldap = id(new PhabricatorUserLDAPInfo())->loadOneWhere( $existing_ldap = id(new PhabricatorExternalAccount())->loadOneWhere(
'userID = %d', 'accountType = %s AND accountDomain = %s AND userPHID = %s',
$current_user->getID()); 'ldap',
'self',
$current_user->getPHID());
if ($ldap_info->getUserID() != $current_user->getID() || if ($ldap_info->getUserPHID() != $current_user->getPHID() ||
$existing_ldap) { $existing_ldap) {
$dialog = new AphrontDialogView(); $dialog = new AphrontDialogView();
$dialog->setUser($current_user); $dialog->setUser($current_user);
@ -71,7 +73,7 @@ final class PhabricatorLDAPLoginController extends PhabricatorAuthController {
return id(new AphrontDialogResponse())->setDialog($dialog); return id(new AphrontDialogResponse())->setDialog($dialog);
} }
$ldap_info->setUserID($current_user->getID()); $ldap_info->setUserPHID($current_user->getPHID());
$this->saveLDAPInfo($ldap_info); $this->saveLDAPInfo($ldap_info);
@ -82,8 +84,9 @@ final class PhabricatorLDAPLoginController extends PhabricatorAuthController {
if ($ldap_info->getID()) { if ($ldap_info->getID()) {
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
$known_user = id(new PhabricatorUser())->load( $known_user = id(new PhabricatorUser())->loadOneWhere(
$ldap_info->getUserID()); 'phid = %s',
$ldap_info->getUserPHID());
$session_key = $known_user->establishSession('web'); $session_key = $known_user->establishSession('web');
@ -152,19 +155,23 @@ final class PhabricatorLDAPLoginController extends PhabricatorAuthController {
} }
private function retrieveLDAPInfo(PhabricatorLDAPProvider $provider) { private function retrieveLDAPInfo(PhabricatorLDAPProvider $provider) {
$ldap_info = id(new PhabricatorUserLDAPInfo())->loadOneWhere( $ldap_info = id(new PhabricatorExternalAccount())->loadOneWhere(
'ldapUsername = %s', 'accountType = %s AND accountDomain = %s AND accountID = %s',
'ldap',
'self',
$provider->retrieveUsername()); $provider->retrieveUsername());
if (!$ldap_info) { if (!$ldap_info) {
$ldap_info = new PhabricatorUserLDAPInfo(); $ldap_info = id(new PhabricatorExternalAccount())
$ldap_info->setLDAPUsername($provider->retrieveUsername()); ->setAccountType('ldap')
->setAccountDomain('self')
->setAccountID($provider->retrieveUsername());
} }
return $ldap_info; return $ldap_info;
} }
private function saveLDAPInfo(PhabricatorUserLDAPInfo $info) { private function saveLDAPInfo(PhabricatorExternalAccount $info) {
// UNGUARDED WRITES: Logging-in users don't have their CSRF set up yet. // UNGUARDED WRITES: Logging-in users don't have their CSRF set up yet.
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
$info->save(); $info->save();

View file

@ -116,7 +116,7 @@ extends PhabricatorAuthController {
->setActor($user) ->setActor($user)
->createNewUser($user, $email_obj); ->createNewUser($user, $email_obj);
$ldap_info->setUserID($user->getID()); $ldap_info->setUserPHID($user->getPHID());
$ldap_info->save(); $ldap_info->save();
$session_key = $user->establishSession('web'); $session_key = $user->establishSession('web');

View file

@ -6,11 +6,13 @@ final class PhabricatorLDAPUnlinkController extends PhabricatorAuthController {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$ldap_info = id(new PhabricatorUserLDAPInfo())->loadOneWhere( $ldap_account = id(new PhabricatorExternalAccount())->loadOneWhere(
'userID = %d', 'userPHID = %s AND accountType = %s AND accountDomain = %s',
$user->getID()); $user->getPHID(),
'ldap',
'self');
if (!$ldap_info) { if (!$ldap_account) {
return new Aphront400Response(); return new Aphront400Response();
} }
@ -27,7 +29,7 @@ final class PhabricatorLDAPUnlinkController extends PhabricatorAuthController {
return id(new AphrontDialogResponse())->setDialog($dialog); return id(new AphrontDialogResponse())->setDialog($dialog);
} }
$ldap_info->delete(); $ldap_account->delete();
return id(new AphrontRedirectResponse()) return id(new AphrontRedirectResponse())
->setURI('/settings/panel/ldap/'); ->setURI('/settings/panel/ldap/');

View file

@ -96,10 +96,12 @@ final class PhabricatorPeopleLdapController
->setActor($admin) ->setActor($admin)
->createNewUser($user, $email_obj); ->createNewUser($user, $email_obj);
$ldap_info = new PhabricatorUserLDAPInfo(); id(new PhabricatorExternalAccount())
$ldap_info->setLDAPUsername($username); ->setUserPHID($user->getPHID())
$ldap_info->setUserID($user->getID()); ->setAccountType('ldap')
$ldap_info->save(); ->setAccountDomain('self')
->setAccountID($username)
->save();
$header = pht('Successfully added %s', $username); $header = pht('Successfully added %s', $username);
$attribute = null; $attribute = null;

View file

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

View file

@ -1,6 +0,0 @@
<?php
final class PhabricatorUserLDAPInfo extends PhabricatorUserDAO {
protected $userID;
protected $ldapUsername;
}

View file

@ -23,13 +23,15 @@ final class PhabricatorSettingsPanelLDAP
public function processRequest(AphrontRequest $request) { public function processRequest(AphrontRequest $request) {
$user = $request->getUser(); $user = $request->getUser();
$ldap_info = id(new PhabricatorUserLDAPInfo())->loadOneWhere( $ldap_account = id(new PhabricatorExternalAccount())->loadOneWhere(
'userID = %d', 'userPHID = %s AND accountType = %s AND accountDomain = %s',
$user->getID()); $user->getPHID(),
'ldap',
'self');
$forms = array(); $forms = array();
if (!$ldap_info) { if (!$ldap_account) {
$unlink = pht('Link LDAP Account'); $unlink = pht('Link LDAP Account');
$unlink_form = new AphrontFormView(); $unlink_form = new AphrontFormView();
$unlink_form $unlink_form

View file

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