MediaWiki master
GroupPermissionsLookup.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Permissions;
22
25
41
46 public const CONSTRUCTOR_OPTIONS = [
50 ];
51
53 private $groupPermissions;
54
56 private $revokePermissions;
57
59 private $groupInheritance;
60
64 public function __construct( ServiceOptions $options ) {
65 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
66 $this->groupPermissions = $options->get( MainConfigNames::GroupPermissions );
67 $this->revokePermissions = $options->get( MainConfigNames::RevokePermissions );
68 $this->groupInheritance = $options->get( MainConfigNames::GroupInheritsPermissions );
69 }
70
83 public function groupHasPermission( string $group, string $permission ): bool {
84 $inheritsFrom = $this->groupInheritance[$group] ?? false;
85 $has = isset( $this->groupPermissions[$group][$permission] ) &&
86 $this->groupPermissions[$group][$permission];
87 // If the group doesn't have the permission and inherits from somewhere,
88 // check that group too
89 if ( !$has && $inheritsFrom !== false ) {
90 $has = isset( $this->groupPermissions[$inheritsFrom][$permission] ) &&
91 $this->groupPermissions[$inheritsFrom][$permission];
92 }
93 if ( !$has ) {
94 // If they don't have the permission, exit early
95 return false;
96 }
97
98 // Check if the permission has been revoked
99 $revoked = isset( $this->revokePermissions[$group][$permission] ) &&
100 $this->revokePermissions[$group][$permission];
101 if ( !$revoked && $inheritsFrom !== false ) {
102 $revoked = isset( $this->revokePermissions[$inheritsFrom][$permission] ) &&
103 $this->revokePermissions[$inheritsFrom][$permission];
104 }
105
106 return !$revoked;
107 }
108
119 public function getGrantedPermissions( string $group ): array {
120 $rights = array_keys( array_filter( $this->groupPermissions[$group] ?? [] ) );
121 $inheritsFrom = $this->groupInheritance[$group] ?? false;
122 if ( $inheritsFrom !== false ) {
123 $rights = array_merge(
124 $rights,
125 // array_filter removes empty items
126 array_keys( array_filter( $this->groupPermissions[$inheritsFrom] ?? [] ) )
127 );
128 }
129
130 return array_unique( $rights );
131 }
132
140 public function getRevokedPermissions( string $group ): array {
141 $rights = array_keys( array_filter( $this->revokePermissions[$group] ?? [] ) );
142 $inheritsFrom = $this->groupInheritance[$group] ?? false;
143 if ( $inheritsFrom !== false ) {
144 $rights = array_merge(
145 $rights,
146 // array_filter removes empty items
147 array_keys( array_filter( $this->revokePermissions[$inheritsFrom] ?? [] ) )
148 );
149 }
150
151 return array_unique( $rights );
152 }
153
163 public function getGroupPermissions( array $groups ): array {
164 $rights = [];
165 $checkGroups = [];
166
167 // Add inherited groups to the list of groups to check
168 foreach ( $groups as $group ) {
169 $checkGroups[] = $group;
170 if ( isset( $this->groupInheritance[$group] ) ) {
171 $checkGroups[] = $this->groupInheritance[$group];
172 }
173 }
174
175 // grant every granted permission first
176 foreach ( $checkGroups as $group ) {
177 if ( isset( $this->groupPermissions[$group] ) ) {
178 $rights = array_merge(
179 $rights,
180 // array_filter removes empty items
181 array_keys( array_filter( $this->groupPermissions[$group] ) )
182 );
183 }
184 }
185 // now revoke the revoked permissions
186 foreach ( $checkGroups as $group ) {
187 if ( isset( $this->revokePermissions[$group] ) ) {
188 $rights = array_diff(
189 $rights,
190 array_keys( array_filter( $this->revokePermissions[$group] ) )
191 );
192 }
193 }
194 return array_unique( $rights );
195 }
196
203 public function getGroupsWithPermission( string $permission ): array {
204 $allowedGroups = [];
205 $groups = array_unique( array_merge(
206 array_keys( $this->groupPermissions ),
207 array_keys( $this->groupInheritance )
208 ) );
209 foreach ( $groups as $group ) {
210 if ( $this->groupHasPermission( $group, $permission ) ) {
211 $allowedGroups[] = $group;
212 }
213 }
214 return $allowedGroups;
215 }
216}
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.