From 9990ba163eb596adb867355ffc6a1dba16bfdd9b Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Sat, 27 Mar 2010 15:05:56 +0000 Subject: [PATCH] Per r64228 CR: make the check a static method in IPBlockForm to reduce duplication. --- includes/api/ApiBlock.php | 16 +++------ includes/api/ApiUnblock.php | 16 +++------ includes/specials/SpecialBlockip.php | 44 +++++++++++++++++------- includes/specials/SpecialIpblocklist.php | 16 +++------ 4 files changed, 45 insertions(+), 47 deletions(-) diff --git a/includes/api/ApiBlock.php b/includes/api/ApiBlock.php index 6af2a21e29..aa36f87a91 100644 --- a/includes/api/ApiBlock.php +++ b/includes/api/ApiBlock.php @@ -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' ) ); diff --git a/includes/api/ApiUnblock.php b/includes/api/ApiUnblock.php index 7857c5e23e..ce49fe3761 100644 --- a/includes/api/ApiUnblock.php +++ b/includes/api/ApiUnblock.php @@ -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']; diff --git a/includes/specials/SpecialBlockip.php b/includes/specials/SpecialBlockip.php index 85182dca25..31edbe2032 100644 --- a/includes/specials/SpecialBlockip.php +++ b/includes/specials/SpecialBlockip.php @@ -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. diff --git a/includes/specials/SpecialIpblocklist.php b/includes/specials/SpecialIpblocklist.php index e1cbdbc14f..1f8795d08c 100644 --- a/includes/specials/SpecialIpblocklist.php +++ b/includes/specials/SpecialIpblocklist.php @@ -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( '' ); -- 2.20.1