1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-28 16:30: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',
'PhabricatorUserEditor' => 'applications/people/editor/PhabricatorUserEditor.php',
'PhabricatorUserEmail' => 'applications/people/storage/PhabricatorUserEmail.php',
'PhabricatorUserLDAPInfo' => 'applications/people/storage/PhabricatorUserLDAPInfo.php',
'PhabricatorUserLog' => 'applications/people/storage/PhabricatorUserLog.php',
'PhabricatorUserOAuthInfo' => 'applications/people/storage/PhabricatorUserOAuthInfo.php',
'PhabricatorUserPreferences' => 'applications/settings/storage/PhabricatorUserPreferences.php',
@ -3403,7 +3402,6 @@ phutil_register_library_map(array(
'PhabricatorUserDAO' => 'PhabricatorLiskDAO',
'PhabricatorUserEditor' => 'PhabricatorEditor',
'PhabricatorUserEmail' => 'PhabricatorUserDAO',
'PhabricatorUserLDAPInfo' => 'PhabricatorUserDAO',
'PhabricatorUserLog' => 'PhabricatorUserDAO',
'PhabricatorUserPreferences' => 'PhabricatorUserDAO',
'PhabricatorUserProfile' => 'PhabricatorUserDAO',

View file

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

View file

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

View file

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

View file

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

View file

@ -303,13 +303,6 @@ final class PhabricatorUserEditor extends PhabricatorEditor {
}
$user->openTransaction();
$ldaps = id(new PhabricatorUserLDAPInfo())->loadAllWhere(
'userID = %d',
$user->getID());
foreach ($ldaps as $ldap) {
$ldap->delete();
}
$externals = id(new PhabricatorExternalAccount())->loadAllWhere(
'userPHID = %s',
$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) {
$user = $request->getUser();
$ldap_info = id(new PhabricatorUserLDAPInfo())->loadOneWhere(
'userID = %d',
$user->getID());
$ldap_account = id(new PhabricatorExternalAccount())->loadOneWhere(
'userPHID = %s AND accountType = %s AND accountDomain = %s',
$user->getPHID(),
'ldap',
'self');
$forms = array();
if (!$ldap_info) {
if (!$ldap_account) {
$unlink = pht('Link LDAP Account');
$unlink_form = new AphrontFormView();
$unlink_form

View file

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