From: Petr Pchelko Date: Fri, 23 Aug 2019 15:33:21 +0000 (-0700) Subject: Move Title::isNamespaceProtected() to PermissionManager. X-Git-Tag: 1.34.0-rc.0~531^2 X-Git-Url: http://git.cyclocoop.org/%22.htmlspecialchars%28%24url_syndic%29.%22?a=commitdiff_plain;h=333b6e71109ed09d8f505ee58b877fd935526299;p=lhc%2Fweb%2Fwiklou.git Move Title::isNamespaceProtected() to PermissionManager. Bug: T11977 Change-Id: I589b2558fc410c9f744ec80f7310e85754506b37 --- diff --git a/includes/Permissions/PermissionManager.php b/includes/Permissions/PermissionManager.php index ec0157b20e..43d57a7748 100644 --- a/includes/Permissions/PermissionManager.php +++ b/includes/Permissions/PermissionManager.php @@ -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. diff --git a/includes/Title.php b/includes/Title.php index 75ddea8dd4..0d077910c8 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -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 */ diff --git a/tests/phpunit/includes/Permissions/PermissionManagerTest.php b/tests/phpunit/includes/Permissions/PermissionManagerTest.php index 88847e255d..122f377674 100644 --- a/tests/phpunit/includes/Permissions/PermissionManagerTest.php +++ b/tests/phpunit/includes/Permissions/PermissionManagerTest.php @@ -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 ) ); diff --git a/tests/phpunit/includes/TitlePermissionTest.php b/tests/phpunit/includes/TitlePermissionTest.php index e6cf8c8b89..c5baeed6be 100644 --- a/tests/phpunit/includes/TitlePermissionTest.php +++ b/tests/phpunit/includes/TitlePermissionTest.php @@ -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 ) );