Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| RestrictedUserGroupConfigReader | |
100.00% |
17 / 17 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConfig | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
6 | |||
| getConfigForLocalWiki | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getConfigForRemoteWiki | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /* |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\User; |
| 8 | |
| 9 | use MediaWiki\Config\ServiceOptions; |
| 10 | use MediaWiki\MainConfigNames; |
| 11 | use MediaWiki\WikiMap\WikiMap; |
| 12 | |
| 13 | /** |
| 14 | * A helper class to read the restricted user groups configuration for a given wiki. |
| 15 | * |
| 16 | * @since 1.46 |
| 17 | */ |
| 18 | class RestrictedUserGroupConfigReader { |
| 19 | |
| 20 | /** @internal */ |
| 21 | public const CONSTRUCTOR_OPTIONS = [ |
| 22 | MainConfigNames::RestrictedGroups, |
| 23 | ]; |
| 24 | |
| 25 | /** |
| 26 | * Scope value representing user groups as supported by MediaWiki core. |
| 27 | * Other scopes may be added by extensions. |
| 28 | */ |
| 29 | public const SCOPE_LOCAL = 'local'; |
| 30 | |
| 31 | public function __construct( |
| 32 | private readonly ServiceOptions $options, |
| 33 | private readonly UserRequirementsConditionValidator $userRequirementsConditionValidator, |
| 34 | ) { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Reads the restricted group configuration for the specified wiki, either from the ServiceOptions provided to |
| 39 | * the constructor (if the wiki is the local wiki) or from the global $wgConf variable (otherwise). |
| 40 | * Only restrictions relevant for the given scope are returned: restrictions without a `scope` key are always |
| 41 | * included, while restrictions with a `scope` key are only included if the requested scope is listed. |
| 42 | * @param false|string $wiki The wiki ID for which to read the configuration. `false` means the current wiki. |
| 43 | * @param string $scope The scope for which to read the configuration. Defaults to {@see self::SCOPE_LOCAL}. |
| 44 | * @return array<string, UserGroupRestrictions> An array mapping group names to their restrictions. |
| 45 | */ |
| 46 | public function getConfig( false|string $wiki = false, string $scope = self::SCOPE_LOCAL ): array { |
| 47 | $isLocal = $wiki === false || $wiki === WikiMap::getCurrentWikiId(); |
| 48 | if ( $isLocal ) { |
| 49 | $rawConfig = $this->getConfigForLocalWiki(); |
| 50 | } else { |
| 51 | $rawConfig = $this->getConfigForRemoteWiki( $wiki ); |
| 52 | } |
| 53 | |
| 54 | $result = []; |
| 55 | foreach ( $rawConfig as $groupName => $spec ) { |
| 56 | if ( isset( $spec['scope'] ) && !in_array( $scope, $spec['scope'], true ) ) { |
| 57 | continue; |
| 58 | } |
| 59 | $result[$groupName] = UserGroupRestrictions::newFromSpecValidated( |
| 60 | $spec, |
| 61 | $this->userRequirementsConditionValidator |
| 62 | ); |
| 63 | } |
| 64 | return $result; |
| 65 | } |
| 66 | |
| 67 | private function getConfigForLocalWiki(): array { |
| 68 | $this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 69 | return $this->options->get( MainConfigNames::RestrictedGroups ); |
| 70 | } |
| 71 | |
| 72 | private function getConfigForRemoteWiki( string $wiki ): array { |
| 73 | global $wgConf; |
| 74 | '@phan-var \MediaWiki\Config\SiteConfiguration $wgConf'; |
| 75 | |
| 76 | return $wgConf->get( 'wgRestrictedGroups', $wiki ) ?? []; |
| 77 | } |
| 78 | } |