From eb6df43f2e7f75b4107c6ff45b3497056c731153 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Mon, 2 Apr 2012 17:57:00 +0200 Subject: [PATCH] Moved Preferences::trySetUserEmail() to User::setEmailWithConfirmation() * 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 | 32 ++++------------------ includes/User.php | 35 ++++++++++++++++++++++++ includes/specials/SpecialChangeEmail.php | 20 ++++++++------ languages/messages/MessagesEn.php | 1 + maintenance/language/messages.inc | 1 + 5 files changed, 54 insertions(+), 35 deletions(-) diff --git a/includes/Preferences.php b/includes/Preferences.php index bc8e47f826..1904f2d667 100644 --- a/includes/Preferences.php +++ b/includes/Preferences.php @@ -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 ); } /** diff --git a/includes/User.php b/includes/User.php index 105e011fab..f94b30cf57 100644 --- a/includes/User.php +++ b/includes/User.php @@ -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 diff --git a/includes/specials/SpecialChangeEmail.php b/includes/specials/SpecialChangeEmail.php index 0f85f516b8..bd5a443ccf 100644 --- a/includes/specials/SpecialChangeEmail.php +++ b/includes/specials/SpecialChangeEmail.php @@ -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( - '

' . - $this->getOutput()->parseInline( $status->getWikiText( $info ) ) . - '

' ); - } + $oldaddr = $user->getEmail(); + $status = $user->setEmailWithConfirmation( $newaddr ); + if ( !$status->isGood() ) { + $this->getOutput()->addHTML( + '

' . + $this->getOutput()->parseInline( $status->getWikiText( $info ) ) . + '

' ); return false; } + wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) ); + $user->saveSettings(); - return $info ? $info : true; + + return $status->value; } } diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 3cacacfe52..7c42e5a339 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -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}}', diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index d88cc3fe0a..230f397f4e 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -485,6 +485,7 @@ $wgMessageStructure = array( 'emailconfirmlink', 'invalidemailaddress', 'cannotchangeemail', + 'emaildisabled', 'accountcreated', 'accountcreatedtext', 'createaccount-title', -- 2.20.1