Index: trunk/extensions/CentralAuth/CentralAuthHooks.php |
— | — | @@ -183,30 +183,51 @@ |
184 | 184 | return true; |
185 | 185 | } |
186 | 186 | |
| 187 | + // Clean up username |
| 188 | + $title = Title::makeTitleSafe( NS_USER, $userName ); |
| 189 | + if ( !$title ) { |
| 190 | + wfDebug( __METHOD__.": invalid username\n" ); |
| 191 | + } |
| 192 | + $userName = $title->getText(); |
| 193 | + |
| 194 | + // Try the central user |
187 | 195 | $centralUser = new CentralAuthUser( $userName ); |
188 | | - $localId = User::idFromName( $userName ); |
189 | | - |
190 | 196 | if ( $centralUser->authenticateWithToken( $token ) != 'ok' ) { |
191 | 197 | wfDebug( __METHOD__.": token mismatch\n" ); |
192 | | - } elseif ( !$centralUser->isAttached() && $localId ) { |
| 198 | + return true; |
| 199 | + } |
| 200 | + |
| 201 | + // Try the local user from the slave DB |
| 202 | + $localId = User::idFromName( $userName ); |
| 203 | + |
| 204 | + // Fetch the user ID from the master, so that we don't try to create the user |
| 205 | + // when they already exist, due to replication lag |
| 206 | + if ( !$localId && wfGetLB()->getReaderIndex() != 0 ) { |
| 207 | + $dbw = wfGetDB( DB_MASTER ); |
| 208 | + $localId = $dbw->selectField( 'user', 'user_id', |
| 209 | + array( 'user_name' => $userName ), __METHOD__ ); |
| 210 | + } |
| 211 | + |
| 212 | + if ( !$centralUser->isAttached() && $localId ) { |
193 | 213 | wfDebug( __METHOD__.": exists, and not attached\n" ); |
| 214 | + return true; |
| 215 | + } |
| 216 | + |
| 217 | + if ( !$localId ) { |
| 218 | + // User does not exist locally, attempt to create it |
| 219 | + if ( !self::attemptAddUser( $user, $userName ) ) { |
| 220 | + // Can't create user, give up now |
| 221 | + return true; |
| 222 | + } |
194 | 223 | } else { |
195 | | - if ( !$localId ) { |
196 | | - // User does not exist locally, attempt to create it |
197 | | - if ( !self::attemptAddUser( $user, $userName ) ) { |
198 | | - // Can't create user, give up now |
199 | | - return true; |
200 | | - } |
201 | | - } else { |
202 | | - $user->setID( $localId ); |
203 | | - $user->loadFromId(); |
204 | | - } |
205 | | - // Auth OK. |
206 | | - wfDebug( __METHOD__.": logged in from session\n" ); |
207 | | - self::initSession( $user, $token ); |
208 | | - $user->centralAuthObj = $centralUser; |
209 | | - $result = true; |
| 224 | + $user->setID( $localId ); |
| 225 | + $user->loadFromId(); |
210 | 226 | } |
| 227 | + // Auth OK. |
| 228 | + wfDebug( __METHOD__.": logged in from session\n" ); |
| 229 | + self::initSession( $user, $token ); |
| 230 | + $user->centralAuthObj = $centralUser; |
| 231 | + $result = true; |
211 | 232 | |
212 | 233 | return true; |
213 | 234 | } |