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

Change subject: Consolidate duplicate code in TorBlockHooks
......................................................................


Consolidate duplicate code in TorBlockHooks

Change-Id: Ia9ac17200314536d63b4cf71bec8aeaf8370db42
---
M includes/TorBlockHooks.php
1 file changed, 32 insertions(+), 35 deletions(-)

Approvals:
  MarkTraceur: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/TorBlockHooks.php b/includes/TorBlockHooks.php
index 00c7187..6bca140 100644
--- a/includes/TorBlockHooks.php
+++ b/includes/TorBlockHooks.php
@@ -28,18 +28,21 @@
 
 class TorBlockHooks {
        /**
-        * Check if a user is a Tor node and not whitelisted or allowed
-        * to bypass tor blocks.
+        * Whether the given user is allowed to perform $action from its 
current IP
         *
-        * @param Title $title Title being acted upon
-        * @param User $user User performing the action
-        * @param string $action Action being performed
-        * @param array &$result Will be filled with block status if blocked
+        * @param User $user
+        * @param string|null $action
         * @return bool
         */
-       public static function onGetUserPermissionsErrorsExpensive( &$title, 
&$user, $action, &$result ) {
-               global $wgTorAllowedActions, $wgRequest;
-               if ( in_array( $action, $wgTorAllowedActions ) ) {
+       private static function checkUserCan( User $user, $action = null ) {
+               global $wgTorAllowedActions, $wgRequest, $wgUser;
+
+               // Just in case we're checking another user
+               if ( $user->getName() !== $wgUser->getName() ) {
+                       return true;
+               }
+
+               if ( $action !== null && in_array( $action, 
$wgTorAllowedActions ) ) {
                        return true;
                }
 
@@ -60,6 +63,24 @@
                                return true;
                        }
 
+                       return false;
+               }
+
+               return true;
+       }
+
+       /**
+        * Check if a user is a Tor node and not whitelisted or allowed
+        * to bypass tor blocks.
+        *
+        * @param Title $title Title being acted upon
+        * @param User $user User performing the action
+        * @param string $action Action being performed
+        * @param array &$result Will be filled with block status if blocked
+        * @return bool
+        */
+       public static function onGetUserPermissionsErrorsExpensive( &$title, 
&$user, $action, &$result ) {
+               if ( !self::checkUserCan( $user, $action ) ) {
                        wfDebugLog( 'torblock', "User detected as editing from 
Tor node. Adding Tor block to permissions errors." );
 
                        // Allow site customization of blocked message.
@@ -83,32 +104,8 @@
         * @return bool
         */
        public static function onEmailUserPermissionsErrors( $user, $editToken, 
&$hookError ) {
-               // wfDebugLog( 'torblock', "Checking Tor status" );
-
-               // Just in case we're checking another user
-               global $wgUser, $wgRequest;
-               if ( $user->getName() != $wgUser->getName() ) {
-                       return true;
-               }
-
-               if ( TorExitNodes::isExitNode() ) {
-                       wfDebugLog( 'torblock', "User detected as editing 
through tor." );
-
-                       global $wgTorBypassPermissions;
-                       foreach ( $wgTorBypassPermissions as $perm) {
-                               if ( $user->isAllowed( $perm ) ) {
-                                       wfDebugLog( 'torblock', "User has $perm 
permission. Exempting from Tor Blocks." );
-                                       return true;
-                               }
-                       }
-
-                       $ip = $wgRequest->getIP();
-                       if ( Block::isWhitelistedFromAutoblocks( $ip ) ) {
-                               wfDebugLog( 'torblock', "IP is in autoblock 
whitelist. Exempting from Tor blocks." );
-                               return true;
-                       }
-
-                       wfDebugLog( 'torblock', "User detected as editing from 
Tor node. Denying email." );
+               if ( !self::checkUserCan( $user ) ) {
+                       wfDebugLog( 'torblock', "User detected as trying to 
send an email from Tor node. Preventing." );
 
                        // Allow site customization of blocked message.
                        $blockedMsg = 'torblock-blocked';

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia9ac17200314536d63b4cf71bec8aeaf8370db42
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/TorBlock
Gerrit-Branch: master
Gerrit-Owner: Hoo man <h...@online.de>
Gerrit-Reviewer: Alex Monk <kren...@wikimedia.org>
Gerrit-Reviewer: Arlolra <abrea...@wikimedia.org>
Gerrit-Reviewer: CSteipp <cste...@wikimedia.org>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: MarkTraceur <mtrac...@member.fsf.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to