Per r64228 CR: make the check a static method in IPBlockForm to reduce duplication.
authorHappy-melon <happy-melon@users.mediawiki.org>
Sat, 27 Mar 2010 15:05:56 +0000 (15:05 +0000)
committerHappy-melon <happy-melon@users.mediawiki.org>
Sat, 27 Mar 2010 15:05:56 +0000 (15:05 +0000)
includes/api/ApiBlock.php
includes/api/ApiUnblock.php
includes/specials/SpecialBlockip.php
includes/specials/SpecialIpblocklist.php

index 6af2a21..aa36f87 100644 (file)
@@ -66,18 +66,10 @@ class ApiBlock extends ApiBase {
                }
                # bug 15810: blocked admins should have limited access here
                if( $wgUser->isBlocked() ){
-                       $user = User::newFromName( $params['user'] );
-                       if( $user instanceof User
-                               && $user->getId() == $wgUser->getId() )
-                       {
-                               # User is trying to unblock themselves
-                               if( !$wgUser->isAllowed( 'unblockself' ) ){
-                                       $this->dieUsageMsg( array( 'ipbnounblockself' ) );
-                               }
-                       } else {
-                               # User is trying to block/unblock someone else
-                               $this->dieUsageMsg( array( 'ipbblocked' ) );
-                       }
+                       $status = IPBlockForm::checkUnblockSelf( $params['user'] );
+                       if( $status !== true ){
+                               $this->dieUsageMsg( array( $status ) );
+                       } 
                }
                if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) ) {
                        $this->dieUsageMsg( array( 'canthide' ) );
index 7857c5e..ce49fe3 100644 (file)
@@ -64,18 +64,10 @@ class ApiUnblock extends ApiBase {
                }
                # bug 15810: blocked admins should have limited access here
                if( $wgUser->isBlocked() ){
-                       $user = User::newFromName( $params['user'] );
-                       if( $user instanceof User
-                               && $user->getId() == $wgUser->getId() )
-                       {
-                               # User is trying to unblock themselves
-                               if( !$wgUser->isAllowed( 'unblockself' ) ){
-                                       $this->dieUsageMsg( array( 'ipbnounblockself' ) );
-                               }
-                       } else {
-                               # User is trying to block/unblock someone else
-                               $this->dieUsageMsg( array( 'ipbblocked' ) );
-                       }
+                       $status = IPBlockForm::checkUnblockSelf( $params['user'] );
+                       if( $status !== true ){
+                               $this->dieUsageMsg( array( $status ) );
+                       } 
                }
 
                $id = $params['id'];
index 85182dc..31edbe2 100644 (file)
@@ -27,18 +27,10 @@ function wfSpecialBlockip( $par ) {
        
        # bug 15810: blocked admins should have limited access here
        if( $wgUser->isBlocked() ){
-               $user = User::newFromName( $ipb->BlockAddress );
-               if( $user instanceof User
-                       && $user->getId() == $wgUser->getId() )
-               {
-                       # User is trying to unblock themselves
-                       if( !$wgUser->isAllowed( 'unblockself' ) ){
-                               throw new ErrorPageError( 'badaccess', 'ipbnounblockself' );
-                       }
-               } else {
-                       # User is trying to block/unblock someone else
-                       throw new ErrorPageError( 'badaccess', 'ipbblocked' );
-               }
+               $status = IPBlockForm::checkUnblockSelf( $ipb->BlockAddress );
+               if( $status !== true ){
+                       throw new ErrorPageError( 'badaccess', $status );
+               } 
        }
 
        $action = $wgRequest->getVal( 'action' );
@@ -376,6 +368,34 @@ class IPBlockForm {
                global $wgEnableUserEmail, $wgSysopEmailBans;
                return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
        }
+       
+       /**
+        * bug 15810: blocked admins should not be able to block/unblock
+        * others, and probably shouldn't be able to unblock themselves
+        * either.
+        * @param $user User, Int or String
+        */
+       public static function checkUnblockSelf( $user ){
+               global $wgUser;
+               if( is_int( $user ) ){
+                       $user = User::newFromId( $user );
+               } elseif ( is_string( $user ) ){
+                       $user = User::newFromName( $user );
+               }
+               if( $user instanceof User
+                       && $user->getId() == $wgUser->getId() )
+               {
+                       # User is trying to unblock themselves
+                       if( $wgUser->isAllowed( 'unblockself' ) ){
+                               return true;
+                       } else {
+                               return 'ipbnounblockself';
+                       }
+               } else {
+                       # User is trying to block/unblock someone else
+                       return 'ipbblocked';
+               }
+       }
 
        /**
         * Backend block code.
index e1cbdbc..1f8795d 100644 (file)
@@ -41,18 +41,12 @@ function wfSpecialIpblocklist( $ip = '' ) {
                        } else {
                                $user = User::newFromName( $ip );
                        }
-                       if( $user instanceof User
-                               && $user->getId() == $wgUser->getId() )
-                       {
-                               # User is trying to unblock themselves
-                               if( !$wgUser->isAllowed( 'unblockself' ) ){
-                                       throw new ErrorPageError( 'badaccess', 'ipbnounblockself' );
-                               }
-                       } else {
-                               # User is trying to block/unblock someone else
-                               throw new ErrorPageError( 'badaccess', 'ipbblocked' );
-                       }
+                       $status = IPBlockForm::checkUnblockSelf( $user );
+                       if( $status !== true ){
+                               throw new ErrorPageError( 'badaccess', $status );
+                       } 
                }
+               
                if( $action == 'unblock' ){
                        # Show unblock form
                        $ipu->showForm( '' );