Merge "Slight improvements to FormSpecialPage behavior."
[lhc/web/wiklou.git] / includes / specials / SpecialPasswordReset.php
index 491fadc..d1f8136 100644 (file)
@@ -33,6 +33,11 @@ class SpecialPasswordReset extends FormSpecialPage {
         */
        private $email;
 
+       /**
+        * @var User
+        */
+       private $firstUser;
+
        /**
         * @var Status
         */
@@ -65,7 +70,7 @@ class SpecialPasswordReset extends FormSpecialPage {
                                'type' => 'text',
                                'label-message' => 'passwordreset-username',
                        );
-                       if( $this->getUser()->isLoggedIn() ) {
+                       if ( $this->getUser()->isLoggedIn() ) {
                                $a['Username']['default'] = $this->getUser()->getName();
                        }
                }
@@ -86,7 +91,7 @@ class SpecialPasswordReset extends FormSpecialPage {
                        );
                }
 
-               if( $this->getUser()->isAllowed( 'passwordreset' ) ) {
+               if ( $this->getUser()->isAllowed( 'passwordreset' ) ) {
                        $a['Capture'] = array(
                                'type' => 'check',
                                'label-message' => 'passwordreset-capture',
@@ -136,7 +141,7 @@ class SpecialPasswordReset extends FormSpecialPage {
                        }
                }
 
-               if( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ) {
+               if ( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ) {
                        // The user knows they don't have the passwordreset permission, but they tried to spoof the form.  That's naughty
                        throw new PermissionsError( 'passwordreset' );
                }
@@ -162,7 +167,7 @@ class SpecialPasswordReset extends FormSpecialPage {
                        );
                        if ( $res ) {
                                $users = array();
-                               foreach( $res as $row ) {
+                               foreach ( $res as $row ) {
                                        $users[] = User::newFromRow( $row );
                                }
                        } else {
@@ -180,8 +185,8 @@ class SpecialPasswordReset extends FormSpecialPage {
                        return array( $error );
                }
 
-               if( count( $users ) == 0 ) {
-                       if( $method == 'email' ) {
+               if ( count( $users ) == 0 ) {
+                       if ( $method == 'email' ) {
                                // Don't reveal whether or not an email address is in use
                                return true;
                        } else {
@@ -257,14 +262,17 @@ class SpecialPasswordReset extends FormSpecialPage {
 
                $this->result = $firstUser->sendMail( $title->escaped(), $this->email->text() );
 
-               // Blank the email if the user is not supposed to see it
-               if( !isset( $data['Capture'] ) || !$data['Capture'] ) {
+               if ( isset( $data['Capture'] ) && $data['Capture'] ) {
+                       // Save the user, will be used if an error occurs when sending the email
+                       $this->firstUser = $firstUser;
+               } else {
+                       // Blank the email if the user is not supposed to see it
                        $this->email = null;
                }
 
                if ( $this->result->isGood() ) {
                        return true;
-               } elseif( isset( $data['Capture'] ) && $data['Capture'] ) {
+               } elseif ( isset( $data['Capture'] ) && $data['Capture'] ) {
                        // The email didn't send, but maybe they knew that and that's why they captured it
                        return true;
                } else {
@@ -275,13 +283,14 @@ class SpecialPasswordReset extends FormSpecialPage {
        }
 
        public function onSuccess() {
-               if( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ) {
-                       // @todo: Logging
+               if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ) {
+                       // @todo Logging
 
-                       if( $this->result->isGood() ) {
+                       if ( $this->result->isGood() ) {
                                $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
                        } else {
-                               $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture', $this->result->getMessage() );
+                               $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture',
+                                       $this->result->getMessage(), $this->firstUser->getName() );
                        }
 
                        $this->getOutput()->addHTML( Html::rawElement( 'pre', array(), $this->email->escaped() ) );
@@ -292,7 +301,7 @@ class SpecialPasswordReset extends FormSpecialPage {
        }
 
        protected function canChangePassword( User $user ) {
-               global $wgPasswordResetRoutes, $wgAuth;
+               global $wgPasswordResetRoutes, $wgEnableEmail, $wgAuth;
 
                // Maybe password resets are disabled, or there are no allowable routes
                if ( !is_array( $wgPasswordResetRoutes ) ||
@@ -306,6 +315,11 @@ class SpecialPasswordReset extends FormSpecialPage {
                        return 'resetpass_forbidden';
                }
 
+               // Maybe email features have been disabled
+               if ( !$wgEnableEmail ) {
+                       return 'passwordreset-emaildisabled';
+               }
+
                // Maybe the user is blocked (check this here rather than relying on the parent
                // method as we have a more specific error message to use here
                if ( $user->isBlocked() ) {