jenkins-bot has submitted this change and it was merged.

Change subject: Instead of only exposing error details, also return a Block 
object
......................................................................


Instead of only exposing error details, also return a Block object

This'll make it a lot easier to integrate with globally blocked users.
Block already supports foreign blockers anyway. We just need to be able
to change the error output.
With a Block object, globalblock handling could be similar to local
block handling. Stuff like UserBlockedError can then be used.

Change-Id: I2893b9e24173d21fe518c360f6c6add363cc7b23
---
M GlobalBlocking.php
A includes/GlobalBlock.php
M includes/GlobalBlocking.class.php
M includes/GlobalBlockingHooks.php
4 files changed, 85 insertions(+), 16 deletions(-)

Approvals:
  Bartosz Dziewoński: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/GlobalBlocking.php b/GlobalBlocking.php
index f7e8473..25ac8d0 100644
--- a/GlobalBlocking.php
+++ b/GlobalBlocking.php
@@ -52,6 +52,7 @@
 $wgAutoloadClasses['SpecialRemoveGlobalBlock'] = __DIR__ . 
'/includes/specials/SpecialRemoveGlobalBlock.php';
 $wgAutoloadClasses['ApiQueryGlobalBlocks'] = __DIR__ . 
'/includes/api/ApiQueryGlobalBlocks.php';
 $wgAutoloadClasses['ApiGlobalBlock'] = __DIR__ . 
'/includes/api/ApiGlobalBlock.php';
+$wgAutoloadClasses['GlobalBlock'] = __DIR__ . '/includes/GlobalBlock.php';
 $wgAutoloadClasses['GlobalBlocking'] = __DIR__ . 
'/includes/GlobalBlocking.class.php';
 $wgAutoloadClasses['GlobalBlockingHooks'] = __DIR__ . 
'/includes/GlobalBlockingHooks.php';
 
diff --git a/includes/GlobalBlock.php b/includes/GlobalBlock.php
new file mode 100644
index 0000000..8c9b46b
--- /dev/null
+++ b/includes/GlobalBlock.php
@@ -0,0 +1,26 @@
+<?php
+
+class GlobalBlock extends Block {
+    /**
+     * @var array
+     */
+    protected $error;
+
+    /**
+     * @param stdClass $block
+     * @param array $error
+     */
+    public function __construct( stdClass $block, array $error ) {
+        parent::__construct();
+
+        $this->error = $error;
+        $this->setBlocker( $block->gb_by );
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getPermissionsError( IContextSource $context ) {
+        return $this->error;
+    }
+}
diff --git a/includes/GlobalBlocking.class.php 
b/includes/GlobalBlocking.class.php
index 0dc4ae7..b598062 100644
--- a/includes/GlobalBlocking.class.php
+++ b/includes/GlobalBlocking.class.php
@@ -6,14 +6,41 @@
  * @license GNU GPL v2+
  */
 class GlobalBlocking {
+       /**
+        * @param $user User
+        * @param $ip string
+        * @return Block|null
+        * @throws MWException
+        */
+       static function getUserBlock( $user, $ip ) {
+               $details = static::getUserBlockDetails( $user, $ip );
+               if ( !empty( $details['error'] ) ) {
+                       $block = new GlobalBlock( $details['block'], 
$details['error'] );
+                       $block->setTarget( $ip );
+                       return $block;
+               }
+
+               return null;
+       }
 
        /**
         * @param $user User
         * @param $ip string
-        * @return array: empty or a message key with parameters
+        * @return array empty or a message key with parameters
         * @throws MWException
         */
        static function getUserBlockErrors( $user, $ip ) {
+               $details = static::getUserBlockDetails( $user, $ip );
+               return $details['error'];
+       }
+
+       /**
+        * @param $user User
+        * @param $ip string
+        * @return array ['block' => DB row, 'error' => empty or a message key 
with parameters]
+        * @throws MWException
+        */
+       static function getUserBlockDetails( $user, $ip ) {
                global $wgLang, $wgRequest, $wgGlobalBlockingBlockXFF;
                static $result = null;
 
@@ -24,7 +51,7 @@
 
                if ( $user->isAllowed( 'ipblock-exempt' ) || $user->isAllowed( 
'globalblock-exempt' ) ) {
                        // User is exempt from IP blocks.
-                       return $result = array();
+                       return $result = array( 'block' => null, 'error' => 
array() );
                }
 
                $block = self::getGlobalBlockingBlock( $ip, $user->isAnon() );
@@ -32,7 +59,7 @@
                        // Check for local whitelisting
                        if ( GlobalBlocking::getWhitelistInfo( $block->gb_id ) 
) {
                                // Block has been whitelisted.
-                               return $result = array();
+                               return $result = array( 'block' => null, 
'error' => array() );
                        }
 
                        $blockTimestamp = $wgLang->timeanddate( wfTimestamp( 
TS_MW, $block->gb_timestamp ), true );
@@ -57,8 +84,19 @@
                                'code' => $errorMsg,
                                'info' => $apiErrorInfo,
                        );
-                       return $result = array( $errorMsg, $blockingUser, 
$display_wiki, $block->gb_reason,
-                               $blockTimestamp, $blockExpiry, $ip, 
$block->gb_address );
+                       return $result = array(
+                               'block' => $block,
+                               'error' => array(
+                                       $errorMsg,
+                                       $blockingUser,
+                                       $display_wiki,
+                                       $block->gb_reason,
+                                       $blockTimestamp,
+                                       $blockExpiry,
+                                       $ip,
+                                       $block->gb_address
+                               ),
+                       );
                }
 
                if ( $wgGlobalBlockingBlockXFF ) {
@@ -83,19 +121,22 @@
                                                'info' => 'One or more proxy 
servers used by your request has been globally blocked',
                                        );
                                        return $result = array(
-                                               $blockedIpXffMsg,
-                                               $blockingUser,
-                                               $display_wiki,
-                                               $block->gb_reason,
-                                               $blockTimestamp,
-                                               $blockExpiry,
-                                               $blockIP
+                                               'block' => $block,
+                                               'error' => array(
+                                                       $blockedIpXffMsg,
+                                                       $blockingUser,
+                                                       $display_wiki,
+                                                       $block->gb_reason,
+                                                       $blockTimestamp,
+                                                       $blockExpiry,
+                                                       $blockIP
+                                               ),
                                        );
                                }
                        }
                }
 
-               return $result = array();
+               return $result = array( 'block' => null, 'error' => array() );
        }
 
        /**
diff --git a/includes/GlobalBlockingHooks.php b/includes/GlobalBlockingHooks.php
index 59ba099..5b80934 100644
--- a/includes/GlobalBlockingHooks.php
+++ b/includes/GlobalBlockingHooks.php
@@ -59,12 +59,13 @@
         * @param User $user
         * @param string $ip
         * @param bool $blocked
+        * @param Block|null $block
         *
         * @return bool
         */
-       static public function onUserIsBlockedGlobally( User &$user, $ip, 
&$blocked ) {
-               $blockError = GlobalBlocking::getUserBlockErrors( $user, $ip );
-               if ( $blockError ) {
+       static public function onUserIsBlockedGlobally( User &$user, $ip, 
&$blocked, &$block ) {
+               $block = GlobalBlocking::getUserBlock( $user, $ip );
+               if ( $block !== null ) {
                        $blocked = true;
                        return false;
                }

-- 
To view, visit https://gerrit.wikimedia.org/r/283442
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2893b9e24173d21fe518c360f6c6add363cc7b23
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/GlobalBlocking
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>
Gerrit-Reviewer: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to