Moved Preferences::trySetUserEmail() to User::setEmailWithConfirmation()
authorAlexandre Emsenhuber <ialex.wiki@gmail.com>
Mon, 2 Apr 2012 15:57:00 +0000 (17:57 +0200)
committerAlexandre Emsenhuber <ialex.wiki@gmail.com>
Tue, 3 Apr 2012 08:50:34 +0000 (10:50 +0200)
* Much more easier to find it in the User class than in Preferences and it's general enough to be in that class.
* Rewrote the function for better readbility
* It now always return a Status object so that it's easier to interpret its result.
* Update the only caller in core (in Special:ChangeEmail) and moved the PrefsEmailAdit hook there

Change-Id: I55939bb5295e73594c3fdf7287dddbc16a233ce4

includes/Preferences.php
includes/User.php
includes/specials/SpecialChangeEmail.php
languages/messages/MessagesEn.php
maintenance/language/messages.inc

index bc8e47f..1904f2d 100644 (file)
@@ -1437,34 +1437,14 @@ class Preferences {
         * @return Array (true on success or Status on failure, info string)
         */
        public static function trySetUserEmail( User $user, $newaddr ) {
-               global $wgEnableEmail, $wgEmailAuthentication;
-               $info = ''; // none
+               wfDeprecated( __METHOD__, '1.20' );
 
-               if ( $wgEnableEmail ) {
-                       $oldaddr = $user->getEmail();
-                       if ( ( $newaddr != '' ) && ( $newaddr != $oldaddr ) ) {
-                               # The user has supplied a new email address on the login page
-                               # new behaviour: set this new emailaddr from login-page into user database record
-                               $user->setEmail( $newaddr );
-                               if ( $wgEmailAuthentication ) {
-                                       # Mail a temporary password to the dirty address.
-                                       # User can come back through the confirmation URL to re-enable email.
-                                       $type = $oldaddr != '' ? 'changed' : 'set';
-                                       $result = $user->sendConfirmationMail( $type );
-                                       if ( !$result->isGood() ) {
-                                               return array( $result, 'mailerror' );
-                                       }
-                                       $info = 'eauth';
-                               }
-                       } elseif ( $newaddr != $oldaddr ) { // if the address is the same, don't change it
-                               $user->setEmail( $newaddr );
-                       }
-                       if ( $oldaddr != $newaddr ) {
-                               wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
-                       }
+               $result = $user->setEmailWithConfirmation( $newaddr );
+               if ( $result->isGood() ) {
+                       return array( true, $result->value );
+               } else {
+                       return array( $result, 'mailerror' );
                }
-
-               return array( true, $info );
        }
 
        /**
index 105e011..f94b30c 100644 (file)
@@ -2112,6 +2112,41 @@ class User {
                wfRunHooks( 'UserSetEmail', array( $this, &$this->mEmail ) );
        }
 
+       /**
+        * Set the user's e-mail address and a confirmation mail if needed.
+        *
+        * @param $str String New e-mail address
+        * @return Status
+        */
+       public function setEmailWithConfirmation( $str ) {
+               global $wgEnableEmail, $wgEmailAuthentication;
+
+               if ( !$wgEnableEmail ) {
+                       return Status::newFatal( 'emaildisabled' );
+               }
+
+               $oldaddr = $this->getEmail();
+               if ( $str === $oldaddr ) {
+                       return Status::newGood( true );
+               }
+
+               $this->setEmail( $str );
+
+               if ( $str !== '' && $wgEmailAuthentication ) {
+                       # Send a confirmation request to the new address if needed
+                       $type = $oldaddr != '' ? 'changed' : 'set';
+                       $result = $this->sendConfirmationMail( $type );
+                       if ( $result->isGood() ) {
+                               # Say the the caller that a confirmation mail has been sent
+                               $status->value = 'eauth';
+                       }
+               } else {
+                       $result = Status::newGood( true );
+               }
+
+               return $result;
+       }
+
        /**
         * Get the user's real name
         * @return String User's real name
index 0f85f51..bd5a443 100644 (file)
@@ -196,18 +196,20 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
                        LoginForm::clearLoginThrottle( $user->getName() );
                }
 
-               list( $status, $info ) = Preferences::trySetUserEmail( $user, $newaddr );
-               if ( $status !== true ) {
-                       if ( $status instanceof Status ) {
-                               $this->getOutput()->addHTML(
-                                       '<p class="error">' .
-                                       $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
-                                       '</p>' );
-                       }
+               $oldaddr = $user->getEmail();
+               $status = $user->setEmailWithConfirmation( $newaddr );
+               if ( !$status->isGood() ) {
+                       $this->getOutput()->addHTML(
+                               '<p class="error">' .
+                               $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
+                               '</p>' );
                        return false;
                }
 
+               wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
+
                $user->saveSettings();
-               return $info ? $info : true;
+
+               return $status->value;
        }
 }
index 3cacacf..7c42e5a 100644 (file)
@@ -1147,6 +1147,7 @@ No e-mail will be sent for any of the following features.',
 'invalidemailaddress'        => 'The e-mail address cannot be accepted as it appears to have an invalid format.
 Please enter a well-formatted address or empty that field.',
 'cannotchangeemail'          => 'Account e-mail addresses cannot be changed on this wiki.',
+'emaildisabled'              => 'E-mails are disabled on this site.',
 'accountcreated'             => 'Account created',
 'accountcreatedtext'         => 'The user account for $1 has been created.',
 'createaccount-title'        => 'Account creation for {{SITENAME}}',
index d88cc3f..230f397 100644 (file)
@@ -485,6 +485,7 @@ $wgMessageStructure = array(
                'emailconfirmlink',
                'invalidemailaddress',
                'cannotchangeemail',
+               'emaildisabled',
                'accountcreated',
                'accountcreatedtext',
                'createaccount-title',