Move Title::isNamespaceProtected() to PermissionManager.
authorPetr Pchelko <ppchelko@wikimedia.org>
Fri, 23 Aug 2019 15:33:21 +0000 (08:33 -0700)
committerPetr Pchelko <ppchelko@wikimedia.org>
Fri, 23 Aug 2019 17:14:55 +0000 (10:14 -0700)
Bug: T11977
Change-Id: I589b2558fc410c9f744ec80f7310e85754506b37

includes/Permissions/PermissionManager.php
includes/Title.php
tests/phpunit/includes/Permissions/PermissionManagerTest.php
tests/phpunit/includes/TitlePermissionTest.php

index ec0157b..43d57a7 100644 (file)
@@ -984,7 +984,7 @@ class PermissionManager {
         * Check permissions on special pages & namespaces
         *
         * @param string $action The action to check
-        * @param User $user User to check
+        * @param UserIdentity $user User to check
         * @param array $errors List of current errors
         * @param string $rigor One of PermissionManager::RIGOR_ constants
         *   - RIGOR_QUICK  : does cheap permission checks from replica DBs (usable for GUI creation)
@@ -998,7 +998,7 @@ class PermissionManager {
         */
        private function checkSpecialsAndNSPermissions(
                $action,
-               User $user,
+               UserIdentity $user,
                $errors,
                $rigor,
                $short,
@@ -1014,7 +1014,7 @@ class PermissionManager {
                }
 
                # Check $wgNamespaceProtection for restricted namespaces
-               if ( $title->isNamespaceProtected( $user ) ) {
+               if ( $this->isNamespaceProtected( $title->getNamespace(), $user ) ) {
                        $ns = $title->getNamespace() == NS_MAIN ?
                                wfMessage( 'nstab-main' )->text() : $title->getNsText();
                        $errors[] = $title->getNamespace() == NS_MEDIAWIKI ?
@@ -1453,6 +1453,20 @@ class PermissionManager {
                return $this->allRights;
        }
 
+       /**
+        * Determines if $user is unable to edit pages in namespace because it has been protected.
+        * @param $index
+        * @param UserIdentity $user
+        * @return bool
+        */
+       private function isNamespaceProtected( $index, UserIdentity $user ) {
+               $namespaceProtection = $this->options->get( 'NamespaceProtection' );
+               if ( isset( $namespaceProtection[$index] ) ) {
+                       return !$this->userHasAllRights( $user, ...(array)$namespaceProtection[$index] );
+               }
+               return false;
+       }
+
        /**
         * Determine which restriction levels it makes sense to use in a namespace,
         * optionally filtered by a user's rights.
index 75ddea8..0d07791 100644 (file)
@@ -2499,6 +2499,7 @@ class Title implements LinkTarget, IDBAccessObject {
         * Determines if $user is unable to edit this page because it has been protected
         * by $wgNamespaceProtection.
         *
+        * @deprecated since 1.34 Don't use this function in new code.
         * @param User $user User object to check permissions
         * @return bool
         */
index 88847e2..122f377 100644 (file)
@@ -505,7 +505,6 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
         * @covers MediaWiki\Permissions\PermissionManager::checkSpecialsAndNSPermissions
         */
        public function testSpecialsAndNSPermissions() {
-               global $wgNamespaceProtection;
                $this->setUser( $this->userName );
 
                $this->setTitle( NS_SPECIAL );
@@ -526,10 +525,13 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
                        MediaWikiServices::getInstance()->getPermissionManager()
                                ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
 
-               $wgNamespaceProtection[NS_USER] = [ 'bogus' ];
+               $this->mergeMwGlobalArrayValue( 'wgNamespaceProtection', [
+                       NS_USER => [ 'bogus' ]
+               ] );
+               $this->resetServices();
+               $this->overrideUserPermissions( $this->user, '' );
 
                $this->setTitle( NS_USER );
-               $this->overrideUserPermissions( $this->user, '' );
                $this->assertEquals( [ [ 'badaccess-group0' ],
                        [ 'namespaceprotected', 'User', 'bogus' ] ],
                        MediaWikiServices::getInstance()->getPermissionManager()
@@ -547,9 +549,10 @@ class PermissionManagerTest extends MediaWikiLangTestCase {
                        MediaWikiServices::getInstance()->getPermissionManager()
                                ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
 
-               $wgNamespaceProtection = null;
-
+               $this->setMwGlobals( 'wgNamespaceProtection', null );
+               $this->resetServices();
                $this->overrideUserPermissions( $this->user, 'bogus' );
+
                $this->assertEquals( [],
                        MediaWikiServices::getInstance()->getPermissionManager()
                                ->getPermissionErrors( 'bogus', $this->user, $this->title ) );
index e6cf8c8..c5baeed 100644 (file)
@@ -410,7 +410,6 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
         * @covers \MediaWiki\Permissions\PermissionManager::checkSpecialsAndNSPermissions
         */
        public function testSpecialsAndNSPermissions() {
-               global $wgNamespaceProtection;
                $this->setUser( $this->userName );
 
                $this->setTitle( NS_SPECIAL );
@@ -428,8 +427,10 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
                $this->assertEquals( [ [ 'badaccess-group0' ] ],
                        $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
 
-               $wgNamespaceProtection[NS_USER] = [ 'bogus' ];
-
+               $this->mergeMwGlobalArrayValue( 'wgNamespaceProtection', [
+                       NS_USER => [ 'bogus' ]
+               ] );
+               $this->resetServices();
                $this->setTitle( NS_USER );
                $this->overrideUserPermissions( $this->user );
                $this->assertEquals( [ [ 'badaccess-group0' ],
@@ -446,8 +447,8 @@ class TitlePermissionTest extends MediaWikiLangTestCase {
                $this->assertEquals( [ [ 'protectedinterface', 'bogus' ] ],
                        $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );
 
-               $wgNamespaceProtection = null;
-
+               $this->setMwGlobals( 'wgNamespaceProtection', null );
+               $this->resetServices();
                $this->overrideUserPermissions( $this->user, 'bogus' );
                $this->assertEquals( [],
                        $this->title->getUserPermissionsErrors( 'bogus', $this->user ) );