Special:Preferences Remove red box in email and changed confirm error to warning
[lhc/web/wiklou.git] / includes / specials / SpecialChangePassword.php
index 6a4347d..91ac4e0 100644 (file)
@@ -190,20 +190,16 @@ class SpecialChangePassword extends FormSpecialPage {
                        return true;
                }
 
-               try {
-                       $this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
-                       $this->mDomain = $wgAuth->getDomain();
+               $this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
+               $this->mDomain = $wgAuth->getDomain();
 
-                       if ( !$wgAuth->allowPasswordChange() ) {
-                               throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
-                       }
+               if ( !$wgAuth->allowPasswordChange() ) {
+                       throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
+               }
 
-                       $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
+               $status = $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
 
-                       return true;
-               } catch ( PasswordError $e ) {
-                       return $e->getMessage();
-               }
+               return $status;
        }
 
        public function onSuccess() {
@@ -231,10 +227,14 @@ class SpecialChangePassword extends FormSpecialPage {
        }
 
        /**
-        * @param string $oldpass
-        * @param string $newpass
-        * @param string $retype
-        * @throws PasswordError When cannot set the new password because requirements not met.
+        * Checks the new password if it meets the requirements for passwords and set
+        * it as a current password, otherwise set the passed Status object to fatal
+        * and doesn't change anything
+        *
+        * @param string $oldpass The current (temporary) password.
+        * @param string $newpass The password to set.
+        * @param string $retype The string of the retype password field to check with newpass
+        * @return Status
         */
        protected function attemptReset( $oldpass, $newpass, $retype ) {
                $isSelf = ( $this->mUserName === $this->getUser()->getName() );
@@ -245,33 +245,32 @@ class SpecialChangePassword extends FormSpecialPage {
                }
 
                if ( !$user || $user->isAnon() ) {
-                       throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
+                       return Status::newFatal( $this->msg( 'nosuchusershort', $this->mUserName ) );
                }
 
                if ( $newpass !== $retype ) {
                        Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
-                       throw new PasswordError( $this->msg( 'badretype' )->text() );
+                       return Status::newFatal( $this->msg( 'badretype' ) );
                }
 
                $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
                if ( $throttleCount === true ) {
                        $lang = $this->getLanguage();
                        $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
-                       throw new PasswordError( $this->msg( 'changepassword-throttled' )
+                       return Status::newFatal( $this->msg( 'changepassword-throttled' )
                                ->params( $lang->formatDuration( $throttleInfo['seconds'] ) )
-                               ->text()
                        );
                }
 
                // @todo Make these separate messages, since the message is written for both cases
                if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
                        Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
-                       throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
+                       return Status::newFatal( $this->msg( 'resetpass-wrong-oldpass' ) );
                }
 
                // User is resetting their password to their old password
                if ( $oldpass === $newpass ) {
-                       throw new PasswordError( $this->msg( 'resetpass-recycled' )->text() );
+                       return Status::newFatal( $this->msg( 'resetpass-recycled' ) );
                }
 
                // Do AbortChangePassword after checking mOldpass, so we don't leak information
@@ -279,7 +278,7 @@ class SpecialChangePassword extends FormSpecialPage {
                $abortMsg = 'resetpass-abort-generic';
                if ( !Hooks::run( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
                        Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
-                       throw new PasswordError( $this->msg( $abortMsg )->text() );
+                       return Status::newFatal( $this->msg( $abortMsg ) );
                }
 
                // Please reset throttle for successful logins, thanks!
@@ -292,7 +291,7 @@ class SpecialChangePassword extends FormSpecialPage {
                        Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
                } catch ( PasswordError $e ) {
                        Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
-                       throw new PasswordError( $e->getMessage() );
+                       return Status::newFatal( new RawMessage( $e->getMessage() ) );
                }
 
                if ( $isSelf ) {
@@ -301,8 +300,9 @@ class SpecialChangePassword extends FormSpecialPage {
                        $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
                        $user->setCookies( null, null, $remember );
                }
-               $user->resetPasswordExpiration();
                $user->saveSettings();
+               $this->resetPasswordExpiration( $user );
+               return Status::newGood();
        }
 
        public function requiresUnblock() {
@@ -312,4 +312,32 @@ class SpecialChangePassword extends FormSpecialPage {
        protected function getGroupName() {
                return 'users';
        }
+
+       /**
+        * For resetting user password expiration, until AuthManager comes along
+        * @param User $user
+        */
+       private function resetPasswordExpiration( User $user ) {
+               global $wgPasswordExpirationDays;
+               $newExpire = null;
+               if ( $wgPasswordExpirationDays ) {
+                       $newExpire = wfTimestamp(
+                               TS_MW,
+                               time() + ( $wgPasswordExpirationDays * 24 * 3600 )
+                       );
+               }
+               // Give extensions a chance to force an expiration
+               Hooks::run( 'ResetPasswordExpiration', array( $this, &$newExpire ) );
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->update(
+                       'user',
+                       array( 'user_password_expires' => $dbw->timestampOrNull( $newExpire ) ),
+                       array( 'user_id' => $user->getID() ),
+                       __METHOD__
+               );
+       }
+
+       protected function getDisplayFormat() {
+               return 'ooui';
+       }
 }