MediaWiki  master
GrantsInfo.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Permissions;
22 
24 
32 class GrantsInfo {
36  public const CONSTRUCTOR_OPTIONS = [
37  'GrantPermissions',
38  'GrantPermissionGroups',
39  ];
40 
42  private $options;
43 
47  public function __construct(
49  ) {
50  $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
51  $this->options = $options;
52  }
53 
58  public function getValidGrants(): array {
59  return array_keys( $this->options->get( 'GrantPermissions' ) );
60  }
61 
66  public function getRightsByGrant(): array {
67  $res = [];
68  foreach ( $this->options->get( 'GrantPermissions' ) as $grant => $rights ) {
69  $res[$grant] = array_keys( array_filter( $rights ) );
70  }
71  return $res;
72  }
73 
79  public function getGrantRights( $grants ): array {
80  $rights = [];
81  foreach ( (array)$grants as $grant ) {
82  if ( isset( $this->options->get( 'GrantPermissions' )[$grant] ) ) {
83  $rights = array_merge(
84  $rights,
85  array_keys( array_filter( $this->options->get( 'GrantPermissions' )[$grant] ) )
86  );
87  }
88  }
89  return array_unique( $rights );
90  }
91 
97  public function grantsAreValid( array $grants ): bool {
98  return array_diff( $grants, $this->getValidGrants() ) === [];
99  }
100 
106  public function getGrantGroups( array $grantsFilter = null ): array {
107  if ( is_array( $grantsFilter ) ) {
108  $grantsFilter = array_fill_keys( $grantsFilter, true );
109  }
110 
111  $groups = [];
112  foreach ( $this->options->get( 'GrantPermissions' ) as $grant => $rights ) {
113  if ( $grantsFilter !== null && !isset( $grantsFilter[$grant] ) ) {
114  continue;
115  }
116  if ( isset( $this->options->get( 'GrantPermissionGroups' )[$grant] ) ) {
117  $groups[$this->options->get( 'GrantPermissionGroups' )[$grant]][] = $grant;
118  } else {
119  $groups['other'][] = $grant;
120  }
121  }
122 
123  return $groups;
124  }
125 
130  public function getHiddenGrants(): array {
131  $grants = [];
132  foreach ( $this->options->get( 'GrantPermissionGroups' ) as $grant => $group ) {
133  if ( $group === 'hidden' ) {
134  $grants[] = $grant;
135  }
136  }
137  return $grants;
138  }
139 }
MediaWiki\Permissions\GrantsInfo\$options
ServiceOptions $options
Definition: GrantsInfo.php:42
MediaWiki\Permissions\GrantsInfo\getGrantRights
getGrantRights( $grants)
Fetch the rights allowed by a set of grants.
Definition: GrantsInfo.php:79
MediaWiki\Permissions\GrantsInfo\getRightsByGrant
getRightsByGrant()
Map all grants to corresponding user rights.
Definition: GrantsInfo.php:66
$res
$res
Definition: testCompression.php:57
MediaWiki\Permissions\GrantsInfo\getValidGrants
getValidGrants()
List all known grants.
Definition: GrantsInfo.php:58
MediaWiki\Config\ServiceOptions
A class for passing options to services.
Definition: ServiceOptions.php:27
MediaWiki\Permissions\GrantsInfo\getGrantGroups
getGrantGroups(array $grantsFilter=null)
Divide the grants into groups.
Definition: GrantsInfo.php:106
MediaWiki\Permissions\GrantsInfo\getHiddenGrants
getHiddenGrants()
Get the list of grants that are hidden and should always be granted.
Definition: GrantsInfo.php:130
MediaWiki\Permissions\GrantsInfo\__construct
__construct(ServiceOptions $options)
Definition: GrantsInfo.php:47
MediaWiki\Permissions\GrantsInfo\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
Definition: GrantsInfo.php:36
MediaWiki\Permissions\GrantsInfo
Users can authorize applications to use their account via OAuth.
Definition: GrantsInfo.php:32
MediaWiki\Permissions\GrantsInfo\grantsAreValid
grantsAreValid(array $grants)
Test that all grants in the list are known.
Definition: GrantsInfo.php:97
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
Definition: ServiceOptions.php:71
MediaWiki\Permissions
Definition: Authority.php:21