r34949 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34948‎ | r34949 | r34950 >
Date:02:49, 17 May 2008
Author:tstarling
Status:old
Tags:
Comment:
* In Special:UserLogin, respect local blocks when automatically creating a user account based on plugin authentication
* Log such account creations with the new autocreate type
* Fixed incorrect output in account creation block message when the reason is blank
Modified paths:
  • /trunk/extensions/CentralAuth/CentralAuthHooks.php (modified) (history)
  • /trunk/extensions/CentralAuth/CentralAuthUser.php (modified) (history)
  • /trunk/phase3/includes/SpecialUserlogin.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialUserlogin.php
@@ -32,6 +32,7 @@
3333 const EMPTY_PASS = 6;
3434 const RESET_PASS = 7;
3535 const ABORTED = 8;
 36+ const CREATE_BLOCKED = 9;
3637
3738 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
3839 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
@@ -370,25 +371,28 @@
371372 if ( '' == $this->mName ) {
372373 return self::NO_NAME;
373374 }
 375+
 376+ // Load $wgUser now, and check to see if we're logging in as the same name.
 377+ // This is necessary because loading $wgUser (say by calling getName()) calls
 378+ // the UserLoadFromSession hook, which potentially creates the user in the
 379+ // database. Until we load $wgUser, checking for user existence using
 380+ // User::newFromName($name)->getId() below will effectively be using stale data.
 381+ if ( $wgUser->getName() === $this->mName ) {
 382+ wfDebug( __METHOD__.": already logged in as {$this->mName}\n" );
 383+ return self::SUCCESS;
 384+ }
374385 $u = User::newFromName( $this->mName );
375386 if( is_null( $u ) || !User::isUsableName( $u->getName() ) ) {
376387 return self::ILLEGAL;
377388 }
 389+
 390+ $isAutoCreated = false;
378391 if ( 0 == $u->getID() ) {
379 - global $wgAuth;
380 - /**
381 - * If the external authentication plugin allows it,
382 - * automatically create a new account for users that
383 - * are externally defined but have not yet logged in.
384 - */
385 - if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
386 - if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
387 - $u = $this->initUser( $u, true );
388 - } else {
389 - return self::WRONG_PLUGIN_PASS;
390 - }
 392+ $status = $this->attemptAutoCreate( $u );
 393+ if ( $status !== self::SUCCESS ) {
 394+ return $status;
391395 } else {
392 - return self::NOT_EXISTS;
 396+ $isAutoCreated = true;
393397 }
394398 } else {
395399 $u->load();
@@ -438,12 +442,50 @@
439443 $wgAuth->updateUser( $u );
440444 $wgUser = $u;
441445
 446+ if ( $isAutoCreated ) {
 447+ // Must be run after $wgUser is set, for correct new user log
 448+ wfRunHooks( 'AuthPluginAutoCreate', array( $wgUser ) );
 449+ }
 450+
442451 $retval = self::SUCCESS;
443452 }
444453 wfRunHooks( 'LoginAuthenticateAudit', array( $u, $this->mPassword, $retval ) );
445454 return $retval;
446455 }
447456
 457+ /**
 458+ * Attempt to automatically create a user on login.
 459+ * Only succeeds if there is an external authentication method which allows it.
 460+ * @return integer Status code
 461+ */
 462+ function attemptAutoCreate( $user ) {
 463+ global $wgAuth, $wgUser;
 464+ /**
 465+ * If the external authentication plugin allows it,
 466+ * automatically create a new account for users that
 467+ * are externally defined but have not yet logged in.
 468+ */
 469+ if ( !$wgAuth->autoCreate() ) {
 470+ return self::NOT_EXISTS;
 471+ }
 472+ if ( !$wgAuth->userExists( $user->getName() ) ) {
 473+ wfDebug( __METHOD__.": user does not exist\n" );
 474+ return self::NOT_EXISTS;
 475+ }
 476+ if ( !$wgAuth->authenticate( $user->getName(), $this->mPassword ) ) {
 477+ wfDebug( __METHOD__.": \$wgAuth->authenticate() returned false, aborting\n" );
 478+ return self::WRONG_PLUGIN_PASS;
 479+ }
 480+ if ( $wgUser->isBlockedFromCreateAccount() ) {
 481+ wfDebug( __METHOD__.": user is blocked from account creation\n" );
 482+ return self::CREATE_BLOCKED;
 483+ }
 484+
 485+ wfDebug( __METHOD__.": creating account\n" );
 486+ $user = $this->initUser( $user, true );
 487+ return self::SUCCESS;
 488+ }
 489+
448490 function processLogin() {
449491 global $wgUser, $wgAuth;
450492
@@ -495,6 +537,9 @@
496538 case self::RESET_PASS:
497539 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
498540 break;
 541+ case self::CREATE_BLOCKED:
 542+ $this->userBlockedMessage();
 543+ break;
499544 default:
500545 throw new MWException( "Unhandled case value" );
501546 }
@@ -652,7 +697,11 @@
653698 $blocker = User::whoIs( $wgUser->mBlock->mBy );
654699 $block_reason = $wgUser->mBlock->mReason;
655700
656 - $wgOut->addWikiMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker );
 701+ if ( strval( $block_reason ) === '' ) {
 702+ $wgOut->addWikiMsg( 'cantcreateaccount-no-reason', $ip, $blocker );
 703+ } else {
 704+ $wgOut->addWikiMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker );
 705+ }
657706 $wgOut->returnToMain( false );
658707 }
659708
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -1154,6 +1154,7 @@
11551155 'cantcreateaccount-text' => "Account creation from this IP address ('''$1''') has been blocked by [[User:$3|$3]].
11561156
11571157 The reason given by $3 is ''$2''",
 1158+'cantcreateaccount-no-reason' => "Account creation from this IP address ('''$1''') has been blocked by [[User:$2|$2]]. No reason was given.",
11581159
11591160 # History pages
11601161 'viewpagelogs' => 'View logs for this page',
Index: trunk/extensions/CentralAuth/CentralAuthUser.php
@@ -1729,6 +1729,7 @@
17301730 return;
17311731 }
17321732 $id = $_COOKIE[$wgCentralAuthCookiePrefix . 'Session'];
 1733+ wfDebug( __METHOD__.": Deleting session $id\n" );
17331734 $key = self::memcKey( 'session', $id );
17341735 $wgMemc->delete( $key );
17351736 }
Index: trunk/extensions/CentralAuth/CentralAuthHooks.php
@@ -320,7 +320,8 @@
321321 }
322322
323323 // Is the user blocked?
324 - if ( !$user->isAllowedToCreateAccount() ) {
 324+ $anon = new User;
 325+ if ( !$anon->isAllowedToCreateAccount() ) {
325326 // Blacklist the user to avoid repeated DB queries subsequently
326327 // First load the session again in case it changed while the above DB query was in progress
327328 wfDebug( __METHOD__.": user is blocked from this wiki, blacklisting\n" );

Status & tagging log