From: jenkins-bot Date: Wed, 28 Aug 2019 09:19:14 +0000 (+0000) Subject: Merge "Move Title::isNamespaceProtected() to PermissionManager." X-Git-Tag: 1.34.0-rc.0~531 X-Git-Url: http://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=38190f6608f3d2807cb93b1bae7edb770af36ef8;hp=a3823ccd947005d8c897fc40e1d3c1eb42472de6 Merge "Move Title::isNamespaceProtected() to PermissionManager." --- 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 eb1907604e..96f196fe9b 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 208b955681..91c4a13873 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 ) );