Rearrange code in User::getBlockedStatus to avoid isAllowed calls
[lhc/web/wiklou.git] / includes / user / User.php
index ea72bda..fe15034 100644 (file)
@@ -1414,10 +1414,6 @@ class User implements IDBAccessObject, UserIdentity {
                                ] );
                                if ( $shouldSetCookie ) {
                                        $block->setCookie( $this->getRequest()->response() );
-
-                                       // temporary measure the use of cookies on ip blocks
-                                       $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
-                                       $stats->increment( 'block.ipblock.setCookie.success' );
                                }
                        } elseif ( $this->isLoggedIn() && $config->get( 'CookieSetOnAutoblock' ) ) {
                                $shouldSetCookie = $block->getType() === Block::TYPE_USER && $block->isAutoblocking();
@@ -1829,7 +1825,7 @@ class User implements IDBAccessObject, UserIdentity {
         *   Check when actually saving should be done against master.
         */
        private function getBlockedStatus( $bFromReplica = true ) {
-               global $wgProxyWhitelist, $wgUser, $wgApplyIpBlocksToXff, $wgSoftBlockRanges;
+               global $wgProxyWhitelist, $wgApplyIpBlocksToXff, $wgSoftBlockRanges;
 
                if ( $this->mBlockedby != -1 ) {
                        return;
@@ -1848,15 +1844,14 @@ class User implements IDBAccessObject, UserIdentity {
                # user is not immune to autoblocks/hardblocks, and they are the current user so we
                # know which IP address they're actually coming from
                $ip = null;
-               if ( !$this->isAllowed( 'ipblock-exempt' ) ) {
-                       // $wgUser->getName() only works after the end of Setup.php. Until
-                       // then, assume it's a logged-out user.
-                       $globalUserName = $wgUser->isSafeToLoad()
-                               ? $wgUser->getName()
-                               : IP::sanitizeIP( $wgUser->getRequest()->getIP() );
-                       if ( $this->getName() === $globalUserName ) {
-                               $ip = $this->getRequest()->getIP();
-                       }
+               $sessionUser = RequestContext::getMain()->getUser();
+               // the session user is set up towards the end of Setup.php. Until then,
+               // assume it's a logged-out user.
+               $globalUserName = $sessionUser->isSafeToLoad()
+                       ? $sessionUser->getName()
+                       : IP::sanitizeIP( $sessionUser->getRequest()->getIP() );
+               if ( $this->getName() === $globalUserName && !$this->isAllowed( 'ipblock-exempt' ) ) {
+                       $ip = $this->getRequest()->getIP();
                }
 
                // User/IP blocking
@@ -1935,9 +1930,9 @@ class User implements IDBAccessObject, UserIdentity {
                }
 
                // Avoid PHP 7.1 warning of passing $this by reference
-               $user = $this;
+               $thisUser = $this;
                // Extensions
-               Hooks::run( 'GetBlockedStatus', [ &$user ] );
+               Hooks::run( 'GetBlockedStatus', [ &$thisUser ] );
        }
 
        /**