Kaldari has submitted this change and it was merged. Change subject: Populate lu_local_id and lu_global_id on new user attach ......................................................................
Populate lu_local_id and lu_global_id on new user attach Bug: T142507 Change-Id: I36d6fb4ee95b97dada7fc80dcbe7126f42e97249 --- M includes/CentralAuthPrimaryAuthenticationProvider.php M includes/CentralAuthUser.php M includes/GlobalRename/GlobalUserMerge.php M includes/GlobalRename/GlobalUserMergeDatabaseUpdates.php 4 files changed, 66 insertions(+), 11 deletions(-) Approvals: Kaldari: Looks good to me, approved Gergő Tisza: Looks good to me, but someone else must approve jenkins-bot: Verified diff --git a/includes/CentralAuthPrimaryAuthenticationProvider.php b/includes/CentralAuthPrimaryAuthenticationProvider.php index 802cae2..1f5d158 100644 --- a/includes/CentralAuthPrimaryAuthenticationProvider.php +++ b/includes/CentralAuthPrimaryAuthenticationProvider.php @@ -440,17 +440,23 @@ // Wha? return AuthenticationResponse::newFail( wfMessage( 'userexists' ) ); } - $centralUser->attach( wfWikiID(), 'new' ); - CentralAuthUtils::getCentralDB()->onTransactionIdle( function () use ( $centralUser ) { - CentralAuthUtils::scheduleCreationJobs( $centralUser ); - } ); return AuthenticationResponse::newPass( $user->getName() ); } } - return AuthenticationResponse::newAbstain(); } + public function finishAccountCreation( $user, $creator, AuthenticationResponse $response ) { + $centralUser = CentralAuthUser::getMasterInstance( $user ); + // Do the attach in finishAccountCreation instead of begin because now the user has been added + // to database and local ID exists (which is needed in attach) + $centralUser->attach( wfWikiID(), 'new' ); + CentralAuthUtils::getCentralDB()->onTransactionIdle( function () use ( $centralUser ) { + CentralAuthUtils::scheduleCreationJobs( $centralUser ); + } ); + return null; + } + public function autoCreatedAccount( $user, $source ) { $centralUser = CentralAuthUser::getMasterInstance( $user ); if ( !$centralUser->exists() ) { diff --git a/includes/CentralAuthUser.php b/includes/CentralAuthUser.php index 3d0d179..7a7b836 100644 --- a/includes/CentralAuthUser.php +++ b/includes/CentralAuthUser.php @@ -534,6 +534,28 @@ } /** + * Return the local user account ID of the user with the same name on given wiki, + * irrespective of whether it is attached or not + * @param string $wikiId ID for the local database to connect to + * @return int|null Local user ID for given $wikiID. Null if $wikiID is invalid or local user doesn't exist + */ + public function getLocalId( $wikiId ) { + // Make sure the wiki ID is valid. (This prevents DBConnectionError in unit tests) + $validWikis = self::getWikiList(); + if ( !in_array( $wikiId, $validWikis ) ) { + return null; + } + // Retrieve the local user ID from the specified database. + $db = $this->getLocalDB( $wikiId ); + $id = $db->selectField( 'user', 'user_id', array( 'user_name' => $this->mName ), __METHOD__ ); + // If user doesn't exist, return null instead of false + if ( $id === false ) { + return null; + } + return $id; + } + + /** * Generate a valid memcached key for caching the object's data. * @return String */ @@ -1773,7 +1795,9 @@ 'lu_wiki' => $wikiID, 'lu_name' => $this->mName, 'lu_attached_timestamp' => $dbw->timestamp( $ts ), - 'lu_attached_method' => $method ), + 'lu_attached_method' => $method, + 'lu_local_id' => $this->getLocalId( $wikiID ), + 'lu_global_id' => $this->getId() ), __METHOD__, array( 'IGNORE' ) ); diff --git a/includes/GlobalRename/GlobalUserMerge.php b/includes/GlobalRename/GlobalUserMerge.php index 49a99c4..10495bd 100644 --- a/includes/GlobalRename/GlobalUserMerge.php +++ b/includes/GlobalRename/GlobalUserMerge.php @@ -126,7 +126,7 @@ 'attached' => $oldCAUser->listAttached(), ) ); - $this->databaseUpdates->merge( $oldName, $newName ); + $this->databaseUpdates->merge( $oldName, $newName, $newId ); $this->databaseUpdates->mergeGlobalUserGroups( $oldId, $newId ); $this->databaseUpdates->mergeRenameUserQueue( $oldId, $newId ); $oldCAUser->removeAntiSpoof(); diff --git a/includes/GlobalRename/GlobalUserMergeDatabaseUpdates.php b/includes/GlobalRename/GlobalUserMergeDatabaseUpdates.php index 11f0622..a6d8a68 100644 --- a/includes/GlobalRename/GlobalUserMergeDatabaseUpdates.php +++ b/includes/GlobalRename/GlobalUserMergeDatabaseUpdates.php @@ -19,10 +19,11 @@ * Merge a global user's rows into * another global user's ones. * - * @param string $oldname - * @param string $newname + * @param string $oldname Old global username + * @param string $newname New global username + * @param int $newId New global user ID */ - public function merge( $oldname, $newname ) { + public function merge( $oldname, $newname, $newId = null ) { $dbw = $this->getDB(); $dbw->startAtomic( __METHOD__ ); @@ -40,12 +41,36 @@ // that wiki yet. $dbw->update( 'localuser', - array( 'lu_name' => $newname ), + array( + 'lu_name' => $newname, + 'lu_global_id' => $newId + ), array( 'lu_name' => $oldname ), __METHOD__, array( 'IGNORE' ) ); + // Get the list of wikis with local accounts attached to the global account + $attachedWikis = $dbw->selectFieldValues( + 'localuser', + 'lu_wiki', + array( 'lu_name' => $newname ) + ); + // For each attached account, update the lu_local_id field + $user = CentralAuthUser::newFromId( $newId ); + foreach ( $attachedWikis as $wiki ) { + $localId = $user->getLocalId( $wiki ); + // Note that $localId will be null in case there is no local account with new name on that wiki yet + $dbw->update( + 'localuser', + array( 'lu_local_id' => $localId ), + array( + 'lu_name' => $newname, + 'lu_wiki' => $wiki + ) + ); + } + // Delete the ones that are duplicates, // we'll use the existing rows $dbw->delete( -- To view, visit https://gerrit.wikimedia.org/r/309553 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I36d6fb4ee95b97dada7fc80dcbe7126f42e97249 Gerrit-PatchSet: 20 Gerrit-Project: mediawiki/extensions/CentralAuth Gerrit-Branch: master Gerrit-Owner: Niharika29 <nihar...@wikimedia.org> Gerrit-Reviewer: Anomie <bjor...@wikimedia.org> Gerrit-Reviewer: BryanDavis <bda...@wikimedia.org> Gerrit-Reviewer: Gergő Tisza <gti...@wikimedia.org> Gerrit-Reviewer: Kaldari <rkald...@wikimedia.org> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: Niharika29 <nihar...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits