From 79706a8c93853caeb44d5025b35ce99db0d1570f Mon Sep 17 00:00:00 2001 From: Thalia Date: Wed, 13 Mar 2019 16:08:15 +0000 Subject: [PATCH] Move logic for checking block behaviour to Block class 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 | 42 +++++++++++++++++++++++++++++++++ includes/user/PasswordReset.php | 18 +------------- includes/user/User.php | 19 +++------------ 3 files changed, 46 insertions(+), 33 deletions(-) diff --git a/includes/Block.php b/includes/Block.php index 060eebd703..700e551eb9 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -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; + } + } + } diff --git a/includes/user/PasswordReset.php b/includes/user/PasswordReset.php index ef104cce5c..b3a8884f0a 100644 --- a/includes/user/PasswordReset.php +++ b/includes/user/PasswordReset.php @@ -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(); } /** diff --git a/includes/user/User.php b/includes/user/User.php index 44df557a4d..3fcba4698d 100644 --- a/includes/user/User.php +++ b/includes/user/User.php @@ -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() ); } } } -- 2.20.1