r56541 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r56540‎ | r56541 | r56542 >
Date:17:24, 17 September 2009
Author:happy-melon
Status:deferred
Tags:
Comment:
More nice things with Login and related
Modified paths:
  • /branches/happy-melon/phase3/includes/ExternalUser.php (modified) (history)
  • /branches/happy-melon/phase3/includes/Login.php (modified) (history)
  • /branches/happy-melon/phase3/includes/specials/SpecialCreateAccount.php (modified) (history)
  • /branches/happy-melon/phase3/includes/specials/SpecialUserlogin.php (modified) (history)

Diff [purge]

Index: branches/happy-melon/phase3/includes/ExternalUser.php
@@ -285,4 +285,22 @@
286286 'eu_external_id' => $this->getId() ),
287287 __METHOD__ );
288288 }
 289+
 290+ /**
 291+ * Check whether this external user id is already linked with
 292+ * a local user.
 293+ * @return Mixed User if the account is linked, Null otherwise.
 294+ */
 295+ public final function getLocalUser(){
 296+ $dbr = wfGetDb( DB_SLAVE );
 297+ $row = $dbr->selectRow(
 298+ 'external_user',
 299+ '*',
 300+ array( 'eu_external_id' => $this->getId() )
 301+ );
 302+ return $row
 303+ ? User::newFromId( $row->eu_wiki_id )
 304+ : null;
 305+ }
 306+
289307 }
Index: branches/happy-melon/phase3/includes/Login.php
@@ -147,6 +147,7 @@
148148 return self::NOT_EXISTS;
149149 }
150150 if( !$this->mExtUser->authenticate( $this->mPassword ) ) {
 151+ $this->mLoginResult = 'wrongpassword';
151152 return self::WRONG_PLUGIN_PASS;
152153 }
153154 } else {
@@ -160,6 +161,7 @@
161162 }
162163 if( !$wgAuth->authenticate( $this->mUser->getName(), $this->mPassword ) ) {
163164 wfDebug( __METHOD__.": \$wgAuth->authenticate() returned false, aborting\n" );
 165+ $this->mLoginResult = 'wrongpassword';
164166 return self::WRONG_PLUGIN_PASS;
165167 }
166168 }
@@ -178,6 +180,7 @@
179181 global $wgUser, $wgAuth;
180182
181183 if ( '' == $this->mName ) {
 184+ $this->mLoginResult = 'noname';
182185 return self::NO_NAME;
183186 }
184187
@@ -195,6 +198,7 @@
196199 } else if ( $throttleCount < $count ) {
197200 $wgMemc->incr($throttleKey);
198201 } else if ( $throttleCount >= $count ) {
 202+ $this->mLoginResult = 'login-throttled';
199203 return self::THROTTLED;
200204 }
201205 }
@@ -211,6 +215,16 @@
212216 }
213217
214218 $this->mExtUser = ExternalUser::newFromName( $this->mName );
 219+
 220+ # If the given username produces a valid ExternalUser, which is
 221+ # linked to an existing local user, use that, regardless of
 222+ # whether the username matches up.
 223+ if( $this->mExtUser ){
 224+ $user = $this->mExtUser->getLocalUser();
 225+ if( $user instanceof User ){
 226+ $this->mUser = $user;
 227+ }
 228+ }
215229
216230 # TODO: Allow some magic here for invalid external names, e.g., let the
217231 # user choose a different wiki name.
@@ -219,12 +233,15 @@
220234 }
221235
222236 # If the user doesn't exist in the local database, our only chance
223 - # is for an external auth plugin to autocreate the local user.
 237+ # is for an external auth plugin to autocreate the local user first.
224238 if ( $this->mUser->getID() == 0 ) {
225239 if ( $this->canAutoCreate() == self::SUCCESS ) {
226240 $isAutoCreated = true;
227241 wfDebug( __METHOD__.": creating account\n" );
228 - $this->initUser( true );
 242+ $result = $this->initUser( true );
 243+ if( $result !== self::SUCCESS ){
 244+ return $result;
 245+ };
229246 } else {
230247 return $this->canAutoCreate();
231248 }
@@ -234,9 +251,8 @@
235252 }
236253
237254 # Give general extensions, such as a captcha, a chance to abort logins
238 - $abort = self::ABORTED;
239 - if( !wfRunHooks( 'AbortLogin', array( $this->mUser, $this->mPassword, &$abort ) ) ) {
240 - return $abort;
 255+ if( !wfRunHooks( 'AbortLogin', array( $this->mUser, $this->mPassword, &$this->mLoginResult ) ) ) {
 256+ return self::ABORTED;
241257 }
242258
243259 if( !$this->mUser->checkPassword( $this->mPassword ) ) {
@@ -266,7 +282,13 @@
267283 # etc will probably just fail cleanly here.
268284 $retval = self::RESET_PASS;
269285 } else {
270 - $retval = ( $this->mPassword === '' ) ? self::EMPTY_PASS : self::WRONG_PASS;
 286+ if( $this->mPassword === '' ){
 287+ $retval = self::EMPTY_PASS;
 288+ $this->mLoginResult = 'wrongpasswordempty';
 289+ } else {
 290+ $retval = self::WRONG_PASS;
 291+ $this->mLoginResult = 'wrongpassword';
 292+ }
271293 }
272294 } else {
273295 $wgAuth->updateUser( $this->mUser );
@@ -296,15 +318,20 @@
297319 * authentication database?
298320 * @param $byEmail Bool is this request going to be handled by sending
299321 * the password by email?
300 - * @return Bool whether creation was successful (should only fail for
301 - * Db errors etc).
 322+ * @return Class constant status code.
302323 */
303324 protected function initUser( $autocreate=false, $byEmail=false ) {
304325 global $wgAuth;
 326+
 327+ if( !wfRunHooks( 'AbortNewAccount', array( $this->mUser, &$this->mCreateResult, $autocreate, $byEmail ) ) ) {
 328+ # Hook point to add extra creation throttles and blocks
 329+ wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
 330+ return self::ABORTED;
 331+ }
305332
306333 $fields = array(
307334 'name' => $this->mName,
308 - 'password' => $byEmail ? null : $this->mPassword,
 335+ 'password' => $byEmail ? null : User::crypt( $this->mPassword ),
309336 'email' => $this->mEmail,
310337 'options' => array(
311338 'rememberpassword' => $this->mRemember ? 1 : 0,
@@ -314,7 +341,7 @@
315342 $this->mUser = User::createNew( $this->mName, $fields );
316343
317344 if( $this->mUser === null ){
318 - return null;
 345+ return self::FAILED;
319346 }
320347
321348 # Let old AuthPlugins play with the user
@@ -338,9 +365,10 @@
339366 $this->mUser->addNewUserLogEntry( $byEmail );
340367
341368 # Run hooks
342 - wfRunHooks( 'AddNewAccount', array( $this->mUser ) );
 369+ wfRunHooks( 'AddNewAccount', array( $this->mUser, $autocreate, $byEmail ) );
343370
344 - return true;
 371+ $this->mUser->saveSettings();
 372+ return self::SUCCESS;
345373 }
346374
347375 /**
@@ -431,12 +459,6 @@
432460 $this->mUser->setEmail( $this->mEmail );
433461 $this->mUser->setRealName( $this->mRealName );
434462
435 - if( !wfRunHooks( 'AbortNewAccount', array( $this->mUser, &$this->mCreateResult ) ) ) {
436 - # Hook point to add extra creation throttles and blocks
437 - wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
438 - return self::ABORTED;
439 - }
440 -
441463 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
442464 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
443465 $value = $wgMemc->get( $key );
@@ -457,11 +479,8 @@
458480 }
459481
460482 $result = $this->initUser( false, $byEmail );
461 - if( $result === null )
462 - # It's unlikely we'd get here without some exception
463 - # being thrown, but it's probably possible...
464 - return self::FAILED;
465 -
 483+ if( $result !== self::SUCCESS )
 484+ return $result;
466485
467486 # Send out an email message if needed
468487 if( $byEmail ){
Index: branches/happy-melon/phase3/includes/specials/SpecialUserlogin.php
@@ -451,11 +451,13 @@
452452
453453 case Login::NO_NAME:
454454 case Login::ILLEGAL:
455 - $this->mainLoginForm( wfMsg( 'noname' ) );
456 - break;
457455 case Login::WRONG_PLUGIN_PASS:
458 - $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
 456+ case Login::WRONG_PASS:
 457+ case Login::EMPTY_PASS:
 458+ case Login::THROTTLED:
 459+ $this->mainLoginForm( wfMsg( $this->mLogin->mLoginResult ) );
459460 break;
 461+
460462 case Login::NOT_EXISTS:
461463 if( $wgUser->isAllowed( 'createaccount' ) ){
462464 $this->mainLoginForm( wfMsgWikiHtml( 'nosuchuser', htmlspecialchars( $this->mName ) ) );
@@ -463,12 +465,7 @@
464466 $this->mainLoginForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->mName ) ) );
465467 }
466468 break;
467 - case Login::WRONG_PASS:
468 - $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
469 - break;
470 - case Login::EMPTY_PASS:
471 - $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
472 - break;
 469+
473470 case Login::RESET_PASS:
474471 # 'Shell out' to Special:ResetPass to get the user to
475472 # set a new permanent password from a temporary one.
@@ -477,14 +474,18 @@
478475 $reset->mHeaderMsgType = 'success';
479476 $reset->execute( null );
480477 break;
 478+
481479 case Login::CREATE_BLOCKED:
482480 $this->userBlockedMessage();
483481 break;
484 - case Login::THROTTLED:
485 - $this->mainLoginForm( wfMsg( 'login-throttled' ) );
 482+
 483+ case Login::ABORTED:
 484+ $msg = $this->mLogin->mLoginResult ? $this->mLogin->mLoginResult : $this->mLogin->mCreateResult;
 485+ $this->mainLoginForm( wfMsg( $msg ) );
486486 break;
 487+
487488 default:
488 - throw new MWException( "Unhandled case value" );
 489+ throw new MWException( "Unhandled case value: $result" );
489490 }
490491 }
491492
Index: branches/happy-melon/phase3/includes/specials/SpecialCreateAccount.php
@@ -183,10 +183,10 @@
184184 case Login::CREATE_BADNAME:
185185 case Login::WRONG_PLUGIN_PASS:
186186 case Login::ABORTED:
187 - return $this->showMainForm( wfMsg( $this->mLogin->mCreateResult ) );
 187+ return $this->showMainForm( wfMsgExt( $this->mLogin->mCreateResult, array('parseinline') ) );
188188
189189 case Login::CREATE_SORBS:
190 - return $this->showMainForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . wfGetIP() . ')' );
 190+ return $this->showMainForm( wfMsgExt( 'sorbs_create_account_reason' ) . ' (' . wfGetIP() . ')', array('parseinline') );
191191
192192 case Login::CREATE_BLOCKED:
193193 return $this->userBlockedMessage();

Status & tagging log