MediaWiki master
GroupPermissionsLookup.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Permissions;
22
25
41
45 public const CONSTRUCTOR_OPTIONS = [
49 ];
50
52 private $groupPermissions;
53
55 private $revokePermissions;
56
58 private $groupInheritance;
59
63 public function __construct( ServiceOptions $options ) {
64 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
65 $this->groupPermissions = $options->get( MainConfigNames::GroupPermissions );
66 $this->revokePermissions = $options->get( MainConfigNames::RevokePermissions );
67 $this->groupInheritance = $options->get( MainConfigNames::GroupInheritsPermissions );
68 }
69
82 public function groupHasPermission( string $group, string $permission ): bool {
83 $inheritsFrom = $this->groupInheritance[$group] ?? false;
84 $has = isset( $this->groupPermissions[$group][$permission] ) &&
85 $this->groupPermissions[$group][$permission];
86 // If the group doesn't have the permission and inherits from somewhere,
87 // check that group too
88 if ( !$has && $inheritsFrom !== false ) {
89 $has = isset( $this->groupPermissions[$inheritsFrom][$permission] ) &&
90 $this->groupPermissions[$inheritsFrom][$permission];
91 }
92 if ( !$has ) {
93 // If they don't have the permission, exit early
94 return false;
95 }
96
97 // Check if the permission has been revoked
98 $revoked = isset( $this->revokePermissions[$group][$permission] ) &&
99 $this->revokePermissions[$group][$permission];
100 if ( !$revoked && $inheritsFrom !== false ) {
101 $revoked = isset( $this->revokePermissions[$inheritsFrom][$permission] ) &&
102 $this->revokePermissions[$inheritsFrom][$permission];
103 }
104
105 return !$revoked;
106 }
107
118 public function getGrantedPermissions( string $group ): array {
119 $rights = array_keys( array_filter( $this->groupPermissions[$group] ?? [] ) );
120 $inheritsFrom = $this->groupInheritance[$group] ?? false;
121 if ( $inheritsFrom !== false ) {
122 $rights = array_merge(
123 $rights,
124 // array_filter removes empty items
125 array_keys( array_filter( $this->groupPermissions[$inheritsFrom] ?? [] ) )
126 );
127 }
128
129 return array_unique( $rights );
130 }
131
139 public function getRevokedPermissions( string $group ): array {
140 $rights = array_keys( array_filter( $this->revokePermissions[$group] ?? [] ) );
141 $inheritsFrom = $this->groupInheritance[$group] ?? false;
142 if ( $inheritsFrom !== false ) {
143 $rights = array_merge(
144 $rights,
145 // array_filter removes empty items
146 array_keys( array_filter( $this->revokePermissions[$inheritsFrom] ?? [] ) )
147 );
148 }
149
150 return array_unique( $rights );
151 }
152
162 public function getGroupPermissions( array $groups ): array {
163 $rights = [];
164 $checkGroups = [];
165
166 // Add inherited groups to the list of groups to check
167 foreach ( $groups as $group ) {
168 $checkGroups[] = $group;
169 if ( isset( $this->groupInheritance[$group] ) ) {
170 $checkGroups[] = $this->groupInheritance[$group];
171 }
172 }
173
174 // grant every granted permission first
175 foreach ( $checkGroups as $group ) {
176 if ( isset( $this->groupPermissions[$group] ) ) {
177 $rights = array_merge(
178 $rights,
179 // array_filter removes empty items
180 array_keys( array_filter( $this->groupPermissions[$group] ) )
181 );
182 }
183 }
184 // now revoke the revoked permissions
185 foreach ( $checkGroups as $group ) {
186 if ( isset( $this->revokePermissions[$group] ) ) {
187 $rights = array_diff(
188 $rights,
189 array_keys( array_filter( $this->revokePermissions[$group] ) )
190 );
191 }
192 }
193 return array_unique( $rights );
194 }
195
202 public function getGroupsWithPermission( string $permission ): array {
203 $allowedGroups = [];
204 $groups = array_unique( array_merge(
205 array_keys( $this->groupPermissions ),
206 array_keys( $this->groupInheritance )
207 ) );
208 foreach ( $groups as $group ) {
209 if ( $this->groupHasPermission( $group, $permission ) ) {
210 $allowedGroups[] = $group;
211 }
212 }
213 return $allowedGroups;
214 }
215}
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
A class containing constants representing the names of configuration variables.
const GroupInheritsPermissions
Name constant for the GroupInheritsPermissions setting, for use with Config::get()
const RevokePermissions
Name constant for the RevokePermissions setting, for use with Config::get()
const GroupPermissions
Name constant for the GroupPermissions setting, for use with Config::get()
getGroupsWithPermission(string $permission)
Get all the groups who have a given permission.
groupHasPermission(string $group, string $permission)
Check, if the given group has the given permission.
getRevokedPermissions(string $group)
Get a list of permissions revoked from this group.
getGroupPermissions(array $groups)
Get the permissions associated with membership in a combination of groups.
getGrantedPermissions(string $group)
Get a list of permissions granted to this group.