followup r91985 — remove duplication of code per ^demon's suggestion.
[lhc/web/wiklou.git] / includes / specials / SpecialPasswordReset.php
index 47bb83e..191832b 100644 (file)
@@ -35,24 +35,13 @@ class SpecialPasswordReset extends FormSpecialPage {
        public function userCanExecute( User $user ) {
                global $wgPasswordResetRoutes, $wgAuth;
 
-               // Maybe password resets are disabled, or there are no allowable routes
-               if ( !is_array( $wgPasswordResetRoutes )
-                       || !in_array( true, array_values( $wgPasswordResetRoutes ) ) )
-               {
-                       throw new ErrorPageError( 'internalerror', 'passwordreset-disabled' );
-               }
-
-               // Maybe the external auth plugin won't allow local password changes
-               if ( !$wgAuth->allowPasswordChange() ) {
+               $error = $this->canChangePassword( $user );
+               if ( is_string( $error ) ) {
+                       throw new ErrorPageError( 'internalerror', $error );
+               } else if ( !$error ) {
                        throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
                }
 
-               // 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() ) {
-                       throw new ErrorPageError( 'internalerror', 'blocked-mailpassword' );
-               }
-
                return parent::userCanExecute( $user );
        }
 
@@ -145,7 +134,7 @@ class SpecialPasswordReset extends FormSpecialPage {
                }
 
                $firstUser = $users[0];
-               
+
                if ( !$firstUser instanceof User || !$firstUser->getID() ) {
                        return array( array( 'nosuchuser', $data['Username'] ) );
                }
@@ -224,4 +213,42 @@ class SpecialPasswordReset extends FormSpecialPage {
                $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
                $this->getOutput()->returnToMain();
        }
-}
+
+       function canChangePassword(User $user) {
+               global $wgPasswordResetRoutes, $wgAuth;
+
+               // Maybe password resets are disabled, or there are no allowable routes
+               if ( !is_array( $wgPasswordResetRoutes ) ||
+                        !in_array( true, array_values( $wgPasswordResetRoutes ) ) ) {
+                       return 'passwordreset-disabled';
+               }
+
+               // Maybe the external auth plugin won't allow local password changes
+               if ( !$wgAuth->allowPasswordChange() ) {
+                       return 'resetpass_forbidden';
+               }
+
+               // 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() ) {
+                       return 'blocked-mailpassword';
+               }
+
+               return true;
+       }
+
+
+       /**
+        * Hide the password reset page if resets are disabled.
+        * @return Bool
+        */
+       function isListed() {
+               global $wgPasswordResetRoutes, $wgAuth, $wgUser;
+
+               if ( $this->canChangePassword( $wgUser ) === true ) {
+                       return parent::isListed();
+               }
+
+               return false;
+       }
+}
\ No newline at end of file