Move logic for checking block behaviour to Block class
authorThalia <thalia.e.chan@googlemail.com>
Wed, 13 Mar 2019 16:08:15 +0000 (16:08 +0000)
committerThalia <thalia.e.chan@googlemail.com>
Wed, 27 Mar 2019 12:54:55 +0000 (12:54 +0000)
User::trackBlockWithCookie and PasswordReset::isBlocked make decisions
about block behaviour based on the block parameters. This should be
done in the Block class.

Bug: T218905
Change-Id: Ia3f46abacdaf70e720b715b39dc60aed53be2d0a

includes/Block.php
includes/user/PasswordReset.php
includes/user/User.php

index 060eebd..700e551 100644 (file)
@@ -2118,4 +2118,46 @@ class Block {
 
                return null;
        }
+
+       /**
+        * Check if the block should be tracked with a cookie.
+        *
+        * @since 1.33
+        * @param bool $isIpUser The user is logged out
+        * @return bool The block should be tracked with a cookie
+        */
+       public function shouldTrackWithCookie( $isIpUser ) {
+               $config = RequestContext::getMain()->getConfig();
+               switch ( $this->getType() ) {
+                       case self::TYPE_IP:
+                       case self::TYPE_RANGE:
+                               return $isIpUser && $config->get( 'CookieSetOnIpBlock' );
+                       case self::TYPE_USER:
+                               return !$isIpUser && $config->get( 'CookieSetOnAutoblock' ) && $this->isAutoblocking();
+                       default:
+                               return false;
+               }
+       }
+
+       /**
+        * Check if the block prevents a user from resetting their password
+        *
+        * @since 1.33
+        * @return bool|null The block blocks password reset
+        */
+       public function appliesToPasswordReset() {
+               switch ( $this->getSystemBlockType() ) {
+                       case null:
+                       case 'global-block':
+                               return $this->isCreateAccountBlocked();
+                       case 'proxy':
+                               return true;
+                       case 'dnsbl':
+                       case 'wgSoftBlockRanges':
+                               return false;
+                       default:
+                               return false;
+               }
+       }
+
 }
index ef104cc..b3a8884 100644 (file)
@@ -265,23 +265,7 @@ class PasswordReset implements LoggerAwareInterface {
                if ( !$block ) {
                        return false;
                }
-               $type = $block->getSystemBlockType();
-               if ( in_array( $type, [ null, 'global-block' ], true ) ) {
-                       // Normal block. Maybe it was meant for someone else and the user just needs to log in;
-                       // or maybe it was issued specifically to prevent some IP from messing with password
-                       // reset? Go out on a limb and use the registration allowed flag to decide.
-                       return $block->isCreateAccountBlocked();
-               } elseif ( $type === 'proxy' ) {
-                       // we disallow actions through proxy even if the user is logged in
-                       // so it makes sense to disallow password resets as well
-                       return true;
-               } elseif ( in_array( $type, [ 'dnsbl', 'wgSoftBlockRanges' ], true ) ) {
-                       // these are just meant to force login so let's not prevent that
-                       return false;
-               } else {
-                       // some extension - we'll have to guess
-                       return true;
-               }
+               return $block->appliesToPasswordReset();
        }
 
        /**
index 44df557..3fcba46 100644 (file)
@@ -1403,23 +1403,10 @@ class User implements IDBAccessObject, UserIdentity {
         */
        public function trackBlockWithCookie() {
                $block = $this->getBlock();
-               if ( $block && $this->getRequest()->getCookie( 'BlockID' ) === null ) {
-                       $config = RequestContext::getMain()->getConfig();
-                       $shouldSetCookie = false;
 
-                       if ( $this->isAnon() && $config->get( 'CookieSetOnIpBlock' ) ) {
-                               // If user is logged-out, set a cookie to track the Block
-                               $shouldSetCookie = in_array( $block->getType(), [
-                                       Block::TYPE_IP, Block::TYPE_RANGE
-                               ] );
-                               if ( $shouldSetCookie ) {
-                                       $block->setCookie( $this->getRequest()->response() );
-                               }
-                       } elseif ( $this->isLoggedIn() && $config->get( 'CookieSetOnAutoblock' ) ) {
-                               $shouldSetCookie = $block->getType() === Block::TYPE_USER && $block->isAutoblocking();
-                               if ( $shouldSetCookie ) {
-                                       $block->setCookie( $this->getRequest()->response() );
-                               }
+               if ( $block && $this->getRequest()->getCookie( 'BlockID' ) === null ) {
+                       if ( $block->shouldTrackWithCookie( $this->isAnon() ) ) {
+                               $block->setCookie( $this->getRequest()->response() );
                        }
                }
        }