Reapply "GrantsInfo service to replace MWGrants"
This patch adds a service as a replacement for MWGrants. This is done as it allows proper dependency injection of used services and configuration settings. This was previously committed as Iac52dba15f and reverted because it introduced recursive service instantiation. To avoid this recursive service instantiantion all UI related methods get moved to a new GrantsLocalization service, instead of the GrantsInfo service. Bug: T253077 Change-Id: Ib900bc424fc272ec709d272dcaff71398fa856f8
This commit is contained in:
parent
31d5609554
commit
ecbaedbad2
8 changed files with 613 additions and 99 deletions
|
|
@ -83,6 +83,8 @@ because of Phabricator reports.
|
|||
been used outside of MediaWiki core.
|
||||
|
||||
=== Deprecations in 1.38 ===
|
||||
* The MWGrants class is deprecated in favor of the new GrantsInfo and
|
||||
GrantsLocalization services.
|
||||
* …
|
||||
|
||||
=== Other changes in 1.38 ===
|
||||
|
|
|
|||
|
|
@ -17,150 +17,93 @@
|
|||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
||||
/**
|
||||
* @deprecated since 1.38, use GrantsInfo and GrantsLocalization instead
|
||||
*
|
||||
* A collection of public static functions to deal with grants.
|
||||
*/
|
||||
class MWGrants {
|
||||
|
||||
/**
|
||||
* List all known grants.
|
||||
* @deprecated since 1.38, use GrantsInfo::getValidGrants() instead
|
||||
* @return array
|
||||
*/
|
||||
public static function getValidGrants() {
|
||||
global $wgGrantPermissions;
|
||||
|
||||
return array_keys( $wgGrantPermissions );
|
||||
return MediaWikiServices::getInstance()->getGrantsInfo()->getValidGrants();
|
||||
}
|
||||
|
||||
/**
|
||||
* Map all grants to corresponding user rights.
|
||||
* @deprecated since 1.38, use GrantsInfo::getRightsByGrant() instead
|
||||
* @return array grant => array of rights
|
||||
*/
|
||||
public static function getRightsByGrant() {
|
||||
global $wgGrantPermissions;
|
||||
|
||||
$res = [];
|
||||
foreach ( $wgGrantPermissions as $grant => $rights ) {
|
||||
$res[$grant] = array_keys( array_filter( $rights ) );
|
||||
}
|
||||
return $res;
|
||||
return MediaWikiServices::getInstance()->getGrantsInfo()->getRightsByGrant();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the display name of the grant
|
||||
* Fetch the description of the grant
|
||||
* @deprecated since 1.38, use GrantsLocalization::getGrantDescription() instead
|
||||
* @param string $grant
|
||||
* @param Language|string|null $lang
|
||||
* @return string Grant description
|
||||
*/
|
||||
public static function grantName( $grant, $lang = null ) {
|
||||
// Give grep a chance to find the usages:
|
||||
// grant-blockusers, grant-createeditmovepage, grant-delete,
|
||||
// grant-editinterface, grant-editmycssjs, grant-editmywatchlist,
|
||||
// grant-editsiteconfig, grant-editpage, grant-editprotected,
|
||||
// grant-highvolume, grant-oversight, grant-patrol, grant-protect,
|
||||
// grant-rollback, grant-sendemail, grant-uploadeditmovefile,
|
||||
// grant-uploadfile, grant-basic, grant-viewdeleted,
|
||||
// grant-viewmywatchlist, grant-createaccount, grant-mergehistory,
|
||||
// grant-import
|
||||
$msg = wfMessage( "grant-$grant" );
|
||||
|
||||
if ( $lang ) {
|
||||
$msg->inLanguage( $lang );
|
||||
}
|
||||
|
||||
if ( !$msg->exists() ) {
|
||||
$msg = $lang
|
||||
? wfMessage( 'grant-generic', $grant )->inLanguage( $lang )
|
||||
: wfMessage( 'grant-generic', $grant );
|
||||
}
|
||||
|
||||
return $msg->text();
|
||||
return MediaWikiServices::getInstance()->getGrantsLocalization()->getGrantDescription( $grant, $lang );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the display names for the grants.
|
||||
* Fetch the descriptions for the grants.
|
||||
* @deprecated since 1.38, use GrantsLocalization::getGrantDescriptions() instead
|
||||
* @param string[] $grants
|
||||
* @param Language|string|null $lang
|
||||
* @return string[] Corresponding grant descriptions
|
||||
*/
|
||||
public static function grantNames( array $grants, $lang = null ) {
|
||||
$ret = [];
|
||||
|
||||
foreach ( $grants as $grant ) {
|
||||
$ret[] = self::grantName( $grant, $lang );
|
||||
}
|
||||
return $ret;
|
||||
return MediaWikiServices::getInstance()->getGrantsLocalization()->getGrantDescriptions( $grants, $lang );
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the rights allowed by a set of grants.
|
||||
* @deprecated since 1.38, use GrantsInfo::getGrantRights() instead
|
||||
* @param string[]|string $grants
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getGrantRights( $grants ) {
|
||||
global $wgGrantPermissions;
|
||||
|
||||
$rights = [];
|
||||
foreach ( (array)$grants as $grant ) {
|
||||
if ( isset( $wgGrantPermissions[$grant] ) ) {
|
||||
$rights = array_merge( $rights, array_keys( array_filter( $wgGrantPermissions[$grant] ) ) );
|
||||
}
|
||||
}
|
||||
return array_unique( $rights );
|
||||
return MediaWikiServices::getInstance()->getGrantsInfo()->getGrantRights( $grants );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that all grants in the list are known.
|
||||
* @deprecated since 1.38, use GrantsInfo::grantsAreValid() instead
|
||||
* @param string[] $grants
|
||||
* @return bool
|
||||
*/
|
||||
public static function grantsAreValid( array $grants ) {
|
||||
return array_diff( $grants, self::getValidGrants() ) === [];
|
||||
return MediaWikiServices::getInstance()->getGrantsInfo()->grantsAreValid( $grants );
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide the grants into groups.
|
||||
* @deprecated since 1.38, use GrantsInfo::getGrantGroups() instead
|
||||
* @param string[]|null $grantsFilter
|
||||
* @return array Map of (group => (grant list))
|
||||
*/
|
||||
public static function getGrantGroups( $grantsFilter = null ) {
|
||||
global $wgGrantPermissions, $wgGrantPermissionGroups;
|
||||
|
||||
if ( is_array( $grantsFilter ) ) {
|
||||
$grantsFilter = array_fill_keys( $grantsFilter, true );
|
||||
}
|
||||
|
||||
$groups = [];
|
||||
foreach ( $wgGrantPermissions as $grant => $rights ) {
|
||||
if ( $grantsFilter !== null && !isset( $grantsFilter[$grant] ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( isset( $wgGrantPermissionGroups[$grant] ) ) {
|
||||
$groups[$wgGrantPermissionGroups[$grant]][] = $grant;
|
||||
} else {
|
||||
$groups['other'][] = $grant;
|
||||
}
|
||||
}
|
||||
|
||||
return $groups;
|
||||
return MediaWikiServices::getInstance()->getGrantsInfo()->getGrantGroups( $grantsFilter );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of grants that are hidden and should always be granted
|
||||
* @deprecated since 1.38, use GrantsInfo::getHiddenGrants() instead
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getHiddenGrants() {
|
||||
global $wgGrantPermissionGroups;
|
||||
|
||||
$grants = [];
|
||||
foreach ( $wgGrantPermissionGroups as $grant => $group ) {
|
||||
if ( $group === 'hidden' ) {
|
||||
$grants[] = $grant;
|
||||
}
|
||||
}
|
||||
return $grants;
|
||||
return MediaWikiServices::getInstance()->getGrantsInfo()->getHiddenGrants();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -169,41 +112,26 @@ class MWGrants {
|
|||
* This should be used to link end users to a full description of what
|
||||
* rights they are giving when they authorize a grant.
|
||||
*
|
||||
* @deprecated since 1.38, use GrantsLocalization::getGrantsLink() instead
|
||||
*
|
||||
* @param string $grant the grant name
|
||||
* @param Language|string|null $lang
|
||||
* @return string (proto-relative) HTML link
|
||||
*/
|
||||
public static function getGrantsLink( $grant, $lang = null ) {
|
||||
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
|
||||
return $linkRenderer->makeKnownLink(
|
||||
\SpecialPage::getTitleFor( 'Listgrants', false, $grant ),
|
||||
self::grantName( $grant, $lang )
|
||||
);
|
||||
return MediaWikiServices::getInstance()->getGrantsLocalization()->getGrantsLink( $grant, $lang );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate wikitext to display a list of grants
|
||||
* @deprecated since 1.38, use GrantsLocalization::getGrantsWikiText() instead
|
||||
*
|
||||
* @param string[]|null $grantsFilter If non-null, only display these grants.
|
||||
* @param Language|string|null $lang
|
||||
* @return string Wikitext
|
||||
*/
|
||||
public static function getGrantsWikiText( $grantsFilter, $lang = null ) {
|
||||
if ( is_string( $lang ) ) {
|
||||
$lang = MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( $lang );
|
||||
} elseif ( $lang === null ) {
|
||||
$lang = MediaWikiServices::getInstance()->getContentLanguage();
|
||||
}
|
||||
|
||||
$s = '';
|
||||
foreach ( self::getGrantGroups( $grantsFilter ) as $group => $grants ) {
|
||||
if ( $group === 'hidden' ) {
|
||||
continue; // implicitly granted
|
||||
}
|
||||
$s .= "*<span class=\"mw-grantgroup\">" .
|
||||
wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
|
||||
$s .= ":" . $lang->semicolonList( self::grantNames( $grants, $lang ) ) . "\n";
|
||||
}
|
||||
return "$s\n";
|
||||
return MediaWikiServices::getInstance()->getGrantsLocalization()->getGrantsWikiText( $grantsFilter, $lang );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,6 +71,8 @@ use MediaWiki\Page\ParserOutputAccess;
|
|||
use MediaWiki\Page\RollbackPageFactory;
|
||||
use MediaWiki\Page\WikiPageFactory;
|
||||
use MediaWiki\Parser\ParserCacheFactory;
|
||||
use MediaWiki\Permissions\GrantsInfo;
|
||||
use MediaWiki\Permissions\GrantsLocalization;
|
||||
use MediaWiki\Permissions\GroupPermissionsLookup;
|
||||
use MediaWiki\Permissions\PermissionManager;
|
||||
use MediaWiki\Permissions\RestrictionStore;
|
||||
|
|
@ -946,6 +948,22 @@ class MediaWikiServices extends ServiceContainer {
|
|||
return $this->getService( 'GlobalIdGenerator' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.38
|
||||
* @return GrantsInfo
|
||||
*/
|
||||
public function getGrantsInfo(): GrantsInfo {
|
||||
return $this->getService( 'GrantsInfo' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.38
|
||||
* @return GrantsLocalization
|
||||
*/
|
||||
public function getGrantsLocalization(): GrantsLocalization {
|
||||
return $this->getService( 'GrantsLocalization' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.36
|
||||
* @return GroupPermissionsLookup
|
||||
|
|
|
|||
139
includes/Permissions/GrantsInfo.php
Normal file
139
includes/Permissions/GrantsInfo.php
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
/**
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
namespace MediaWiki\Permissions;
|
||||
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
|
||||
/**
|
||||
* Users can authorize applications to use their account via OAuth. Grants are used to
|
||||
* limit permissions for these application. This service allows application logic to
|
||||
* access grants.
|
||||
*
|
||||
* @since 1.38
|
||||
*/
|
||||
class GrantsInfo {
|
||||
/**
|
||||
* @internal For use by ServiceWiring
|
||||
*/
|
||||
public const CONSTRUCTOR_OPTIONS = [
|
||||
'GrantPermissions',
|
||||
'GrantPermissionGroups',
|
||||
];
|
||||
|
||||
/** @var ServiceOptions */
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @param ServiceOptions $options
|
||||
*/
|
||||
public function __construct(
|
||||
ServiceOptions $options
|
||||
) {
|
||||
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all known grants.
|
||||
* @return string[]
|
||||
*/
|
||||
public function getValidGrants(): array {
|
||||
return array_keys( $this->options->get( 'GrantPermissions' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Map all grants to corresponding user rights.
|
||||
* @return string[][] grant => array of rights in the grant
|
||||
*/
|
||||
public function getRightsByGrant(): array {
|
||||
$res = [];
|
||||
foreach ( $this->options->get( 'GrantPermissions' ) as $grant => $rights ) {
|
||||
$res[$grant] = array_keys( array_filter( $rights ) );
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the rights allowed by a set of grants.
|
||||
* @param string[]|string $grants
|
||||
* @return string[]
|
||||
*/
|
||||
public function getGrantRights( $grants ): array {
|
||||
$rights = [];
|
||||
foreach ( (array)$grants as $grant ) {
|
||||
if ( isset( $this->options->get( 'GrantPermissions' )[$grant] ) ) {
|
||||
$rights = array_merge(
|
||||
$rights,
|
||||
array_keys( array_filter( $this->options->get( 'GrantPermissions' )[$grant] ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
return array_unique( $rights );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that all grants in the list are known.
|
||||
* @param string[] $grants
|
||||
* @return bool
|
||||
*/
|
||||
public function grantsAreValid( array $grants ): bool {
|
||||
return array_diff( $grants, $this->getValidGrants() ) === [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide the grants into groups.
|
||||
* @param string[]|null $grantsFilter
|
||||
* @return string[][] Map of (group => (grant list))
|
||||
*/
|
||||
public function getGrantGroups( array $grantsFilter = null ): array {
|
||||
if ( is_array( $grantsFilter ) ) {
|
||||
$grantsFilter = array_fill_keys( $grantsFilter, true );
|
||||
}
|
||||
|
||||
$groups = [];
|
||||
foreach ( $this->options->get( 'GrantPermissions' ) as $grant => $rights ) {
|
||||
if ( $grantsFilter !== null && !isset( $grantsFilter[$grant] ) ) {
|
||||
continue;
|
||||
}
|
||||
if ( isset( $this->options->get( 'GrantPermissionGroups' )[$grant] ) ) {
|
||||
$groups[$this->options->get( 'GrantPermissionGroups' )[$grant]][] = $grant;
|
||||
} else {
|
||||
$groups['other'][] = $grant;
|
||||
}
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of grants that are hidden and should always be granted.
|
||||
* @return string[]
|
||||
*/
|
||||
public function getHiddenGrants(): array {
|
||||
$grants = [];
|
||||
foreach ( $this->options->get( 'GrantPermissionGroups' ) as $grant => $group ) {
|
||||
if ( $group === 'hidden' ) {
|
||||
$grants[] = $grant;
|
||||
}
|
||||
}
|
||||
return $grants;
|
||||
}
|
||||
}
|
||||
164
includes/Permissions/GrantsLocalization.php
Normal file
164
includes/Permissions/GrantsLocalization.php
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
<?php
|
||||
/**
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
namespace MediaWiki\Permissions;
|
||||
|
||||
use Language;
|
||||
use MediaWiki\Languages\LanguageFactory;
|
||||
use MediaWiki\Linker\LinkRenderer;
|
||||
use SpecialPage;
|
||||
|
||||
/**
|
||||
* This separate service is needed because the ::getGrantsLink method requires a LinkRenderer
|
||||
* and if we tried to inject a LinkRenderer into the GrantsInfo service, it would result in
|
||||
* recursive service instantiation for sessions using the BotPasswordSessionProvider, as a
|
||||
* result of injecting the LinkRenderer when trying to use a GrantsInfo method that doesn't
|
||||
* even need it.
|
||||
*
|
||||
* @since 1.38
|
||||
*/
|
||||
class GrantsLocalization {
|
||||
/** @var GrantsInfo */
|
||||
private $grantsInfo;
|
||||
|
||||
/** @var LinkRenderer */
|
||||
private $linkRenderer;
|
||||
|
||||
/** @var LanguageFactory */
|
||||
private $languageFactory;
|
||||
|
||||
/** @var Language */
|
||||
private $contentLanguage;
|
||||
|
||||
/**
|
||||
* @param GrantsInfo $grantsInfo
|
||||
* @param LinkRenderer $linkRenderer
|
||||
* @param LanguageFactory $languageFactory
|
||||
* @param Language $contentLanguage
|
||||
*/
|
||||
public function __construct(
|
||||
GrantsInfo $grantsInfo,
|
||||
LinkRenderer $linkRenderer,
|
||||
LanguageFactory $languageFactory,
|
||||
Language $contentLanguage
|
||||
) {
|
||||
$this->grantsInfo = $grantsInfo;
|
||||
$this->linkRenderer = $linkRenderer;
|
||||
$this->languageFactory = $languageFactory;
|
||||
$this->contentLanguage = $contentLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the description of the grant.
|
||||
* @param string $grant
|
||||
* @param Language|string|null $lang
|
||||
* @return string Grant description
|
||||
*/
|
||||
public function getGrantDescription( string $grant, $lang = null ): string {
|
||||
// Give grep a chance to find the usages:
|
||||
// grant-blockusers, grant-createeditmovepage, grant-delete,
|
||||
// grant-editinterface, grant-editmycssjs, grant-editmywatchlist,
|
||||
// grant-editsiteconfig, grant-editpage, grant-editprotected,
|
||||
// grant-highvolume, grant-oversight, grant-patrol, grant-protect,
|
||||
// grant-rollback, grant-sendemail, grant-uploadeditmovefile,
|
||||
// grant-uploadfile, grant-basic, grant-viewdeleted,
|
||||
// grant-viewmywatchlist, grant-createaccount, grant-mergehistory,
|
||||
// grant-import
|
||||
|
||||
// TODO: replace wfMessage with something that can be injected like TextFormatter
|
||||
$msg = wfMessage( "grant-$grant" );
|
||||
|
||||
if ( $lang ) {
|
||||
$msg->inLanguage( $lang );
|
||||
}
|
||||
|
||||
if ( !$msg->exists() ) {
|
||||
$msg = $lang
|
||||
? wfMessage( 'grant-generic', $grant )->inLanguage( $lang )
|
||||
: wfMessage( 'grant-generic', $grant );
|
||||
}
|
||||
|
||||
return $msg->text();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the descriptions for the grants.
|
||||
* @param string[] $grants
|
||||
* @param Language|string|null $lang
|
||||
* @return string[] Corresponding grant descriptions
|
||||
*/
|
||||
public function getGrantDescriptions( array $grants, $lang = null ): array {
|
||||
$ret = [];
|
||||
|
||||
foreach ( $grants as $grant ) {
|
||||
$ret[] = $this->getGrantDescription( $grant, $lang );
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a link to Special:ListGrants for a particular grant name.
|
||||
*
|
||||
* This should be used to link end users to a full description of what
|
||||
* rights they are giving when they authorize a grant.
|
||||
*
|
||||
* @param string $grant the grant name
|
||||
* @param Language|string|null $lang
|
||||
* @return string (proto-relative) HTML link
|
||||
*/
|
||||
public function getGrantsLink( string $grant, $lang = null ): string {
|
||||
return $this->linkRenderer->makeKnownLink(
|
||||
SpecialPage::getTitleFor( 'Listgrants', false, $grant ),
|
||||
$this->getGrantDescription( $grant, $lang )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate wikitext to display a list of grants.
|
||||
* @param string[]|null $grantsFilter If non-null, only display these grants.
|
||||
* @param Language|string|null $lang
|
||||
* @return string Wikitext
|
||||
*/
|
||||
public function getGrantsWikiText( $grantsFilter, $lang = null ): string {
|
||||
if ( is_string( $lang ) ) {
|
||||
$lang = $this->languageFactory->getLanguage( $lang );
|
||||
} elseif ( $lang === null ) {
|
||||
$lang = $this->contentLanguage;
|
||||
}
|
||||
|
||||
$s = '';
|
||||
foreach ( $this->grantsInfo->getGrantGroups( $grantsFilter ) as $group => $grants ) {
|
||||
if ( $group === 'hidden' ) {
|
||||
continue; // implicitly granted
|
||||
}
|
||||
// Give grep a chance to find the usages:
|
||||
// grant-group-page-interaction, grant-group-file-interaction
|
||||
// grant-group-watchlist-interaction, grant-group-email,
|
||||
// grant-group-high-volume, grant-group-customization,
|
||||
// grant-group-administration, grant-group-private-information,
|
||||
// grant-group-other
|
||||
$s .= "*<span class=\"mw-grantgroup\">" .
|
||||
// TODO: replace wfMessage with something that can be injected like TextFormatter
|
||||
wfMessage( "grant-group-$group" )->inLanguage( $lang )->text() . "</span>\n";
|
||||
$s .= ":" . $lang->semicolonList( $this->getGrantDescriptions( $grants, $lang ) ) . "\n";
|
||||
}
|
||||
return "$s\n";
|
||||
}
|
||||
}
|
||||
|
|
@ -102,6 +102,8 @@ use MediaWiki\Page\ParserOutputAccess;
|
|||
use MediaWiki\Page\RollbackPageFactory;
|
||||
use MediaWiki\Page\WikiPageFactory;
|
||||
use MediaWiki\Parser\ParserCacheFactory;
|
||||
use MediaWiki\Permissions\GrantsInfo;
|
||||
use MediaWiki\Permissions\GrantsLocalization;
|
||||
use MediaWiki\Permissions\GroupPermissionsLookup;
|
||||
use MediaWiki\Permissions\PermissionManager;
|
||||
use MediaWiki\Permissions\RestrictionStore;
|
||||
|
|
@ -603,6 +605,24 @@ return [
|
|||
);
|
||||
},
|
||||
|
||||
'GrantsInfo' => static function ( MediaWikiServices $services ): GrantsInfo {
|
||||
return new GrantsInfo(
|
||||
new ServiceOptions(
|
||||
GrantsInfo::CONSTRUCTOR_OPTIONS,
|
||||
$services->getMainConfig()
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
'GrantsLocalization' => static function ( MediaWikiServices $services ): GrantsLocalization {
|
||||
return new GrantsLocalization(
|
||||
$services->getGrantsInfo(),
|
||||
$services->getLinkRenderer(),
|
||||
$services->getLanguageFactory(),
|
||||
$services->getContentLanguage()
|
||||
);
|
||||
},
|
||||
|
||||
'GroupPermissionsLookup' => static function ( MediaWikiServices $services ): GroupPermissionsLookup {
|
||||
return new GroupPermissionsLookup(
|
||||
new ServiceOptions( GroupPermissionsLookup::CONSTRUCTOR_OPTIONS, $services->getMainConfig() )
|
||||
|
|
|
|||
|
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Tests\Integration\Permissions;
|
||||
|
||||
use MediaWiki\Permissions\GrantsLocalization;
|
||||
use MediaWikiIntegrationTestCase;
|
||||
use Message;
|
||||
use SpecialPage;
|
||||
|
||||
/**
|
||||
* @author Zabe
|
||||
*
|
||||
* @coversDefaultClass \MediaWiki\Permissions\GrantsLocalization
|
||||
*/
|
||||
class GrantsLocalizationTest extends MediaWikiIntegrationTestCase {
|
||||
/** @var GrantsLocalization */
|
||||
private $grantsLocalization;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->grantsLocalization = $this->getServiceContainer()->getGrantsLocalization();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getGrantDescription
|
||||
* @dataProvider grantDescriptions
|
||||
*/
|
||||
public function testGetGrantDescription( string $grant ) {
|
||||
$message = new Message( 'grant-' . $grant );
|
||||
$this->assertSame(
|
||||
$message->text(),
|
||||
$this->grantsLocalization->getGrantDescription( $grant )
|
||||
);
|
||||
$this->assertSame(
|
||||
$message->inLanguage( 'de' )->text(),
|
||||
$this->grantsLocalization->getGrantDescription( $grant, 'de' )
|
||||
);
|
||||
}
|
||||
|
||||
public function grantDescriptions() {
|
||||
yield [ 'blockusers' ];
|
||||
yield [ 'createeditmovepage' ];
|
||||
yield [ 'delete' ];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getGrantDescription
|
||||
*/
|
||||
public function testGetNonExistingGrantDescription() {
|
||||
$message = ( new Message( 'grant-generic' ) )->params( 'foo' );
|
||||
$this->assertSame(
|
||||
$message->text(),
|
||||
$this->grantsLocalization->getGrantDescription( 'foo' )
|
||||
);
|
||||
$this->assertSame(
|
||||
$message->inLanguage( 'zh' )->text(),
|
||||
$this->grantsLocalization->getGrantDescription( 'foo', 'zh' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getGrantDescriptions
|
||||
*/
|
||||
public function testGetGrantDescriptions() {
|
||||
$this->assertSame(
|
||||
[
|
||||
( new Message( 'grant-blockusers' ) )->inLanguage( 'de' )->text(),
|
||||
( new Message( 'grant-delete' ) )->inLanguage( 'de' )->text()
|
||||
],
|
||||
$this->grantsLocalization->getGrantDescriptions(
|
||||
[
|
||||
'blockusers',
|
||||
'delete'
|
||||
],
|
||||
'de'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getGrantsLink
|
||||
*/
|
||||
public function testGetGrantsLink() {
|
||||
$this->assertSame(
|
||||
$this->getServiceContainer()->getLinkRenderer()->makeKnownLink(
|
||||
SpecialPage::getTitleFor( 'Listgrants', false, 'delete' ),
|
||||
( new Message( 'grant-delete' ) )->text()
|
||||
),
|
||||
$this->grantsLocalization->getGrantsLink( 'delete' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getGrantsWikiText
|
||||
*/
|
||||
public function testGetGrantsWikiText() {
|
||||
$this->assertSame(
|
||||
"*<span class=\"mw-grantgroup\">Perform high volume activity</span>\n:High-volume editing\n\n",
|
||||
$this->grantsLocalization->getGrantsWikiText( [ 'highvolume' ] )
|
||||
);
|
||||
}
|
||||
}
|
||||
140
tests/phpunit/unit/includes/Permissions/GrantsInfoTest.php
Normal file
140
tests/phpunit/unit/includes/Permissions/GrantsInfoTest.php
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Tests\Unit\Permissions;
|
||||
|
||||
use MediaWiki\Config\ServiceOptions;
|
||||
use MediaWiki\Permissions\GrantsInfo;
|
||||
use MediaWikiUnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \MediaWiki\Permissions\GrantsInfo
|
||||
*/
|
||||
class GrantsInfoTest extends MediaWikiUnitTestCase {
|
||||
|
||||
/** @var GrantsInfo */
|
||||
private $grantsInfo;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$config = [
|
||||
'GrantPermissions' => [
|
||||
'hidden1' => [ 'read' => true, 'autoconfirmed' => false ],
|
||||
'hidden2' => [ 'autoconfirmed' => true ],
|
||||
'normal' => [ 'edit' => true ],
|
||||
'normal2' => [ 'edit' => true, 'create' => true ],
|
||||
'admin' => [ 'protect' => true, 'delete' => true ],
|
||||
],
|
||||
'GrantPermissionGroups' => [
|
||||
'hidden1' => 'hidden',
|
||||
'hidden2' => 'hidden',
|
||||
'normal' => 'normal-group',
|
||||
'admin' => 'admin',
|
||||
],
|
||||
];
|
||||
|
||||
$this->grantsInfo = new GrantsInfo(
|
||||
new ServiceOptions(
|
||||
GrantsInfo::CONSTRUCTOR_OPTIONS,
|
||||
$config
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getValidGrants
|
||||
*/
|
||||
public function testGetValidGrants() {
|
||||
$this->assertSame(
|
||||
[ 'hidden1', 'hidden2', 'normal', 'normal2', 'admin' ],
|
||||
$this->grantsInfo->getValidGrants()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getRightsByGrant
|
||||
*/
|
||||
public function testGetRightsByGrant() {
|
||||
$this->assertSame(
|
||||
[
|
||||
'hidden1' => [ 'read' ],
|
||||
'hidden2' => [ 'autoconfirmed' ],
|
||||
'normal' => [ 'edit' ],
|
||||
'normal2' => [ 'edit', 'create' ],
|
||||
'admin' => [ 'protect', 'delete' ],
|
||||
],
|
||||
$this->grantsInfo->getRightsByGrant()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideGetGrantRights
|
||||
* @covers ::getGrantRights
|
||||
* @param array|string $grants
|
||||
* @param array $rights
|
||||
*/
|
||||
public function testGetGrantRights( $grants, $rights ) {
|
||||
$this->assertSame( $rights, $this->grantsInfo->getGrantRights( $grants ) );
|
||||
}
|
||||
|
||||
public function provideGetGrantRights() {
|
||||
return [
|
||||
'anon' => [ 'hidden1', [ 'read' ] ],
|
||||
'newbie' => [ [ 'hidden1', 'hidden2', 'hidden3' ], [ 'read', 'autoconfirmed' ] ],
|
||||
'basic' => [ [ 'normal1', 'normal2' ], [ 'edit', 'create' ] ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideGrantsAreValid
|
||||
* @covers ::grantsAreValid
|
||||
* @param array $grants
|
||||
* @param bool $valid
|
||||
*/
|
||||
public function testGrantsAreValid( $grants, $valid ) {
|
||||
$this->assertSame( $valid, $this->grantsInfo->grantsAreValid( $grants ) );
|
||||
}
|
||||
|
||||
public function provideGrantsAreValid() {
|
||||
return [
|
||||
[ [ 'hidden1', 'hidden2' ], true ],
|
||||
[ [ 'hidden1', 'hidden3' ], false ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideGetGrantGroups
|
||||
* @covers ::getGrantGroups
|
||||
* @param array|null $grants
|
||||
* @param array $expect
|
||||
*/
|
||||
public function testGetGrantGroups( $grants, $expect ) {
|
||||
$this->assertSame( $expect, $this->grantsInfo->getGrantGroups( $grants ) );
|
||||
}
|
||||
|
||||
public function provideGetGrantGroups() {
|
||||
return [
|
||||
[ null, [
|
||||
'hidden' => [ 'hidden1', 'hidden2' ],
|
||||
'normal-group' => [ 'normal' ],
|
||||
'other' => [ 'normal2' ],
|
||||
'admin' => [ 'admin' ],
|
||||
] ],
|
||||
[ [ 'hidden1', 'normal' ], [
|
||||
'hidden' => [ 'hidden1' ],
|
||||
'normal-group' => [ 'normal' ],
|
||||
] ],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getHiddenGrants
|
||||
*/
|
||||
public function testGetHiddenGrants() {
|
||||
$this->assertSame(
|
||||
[ 'hidden1', 'hidden2' ],
|
||||
$this->grantsInfo->getHiddenGrants()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue