MediaWiki master
GroupPermissionsLookup.php
Go to the documentation of this file.
1<?php
8
11
27
31 public const CONSTRUCTOR_OPTIONS = [
35 ];
36
38 private $groupPermissions;
39
41 private $revokePermissions;
42
44 private $groupInheritance;
45
46 public function __construct( ServiceOptions $options ) {
47 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
48 $this->groupPermissions = $options->get( MainConfigNames::GroupPermissions );
49 $this->revokePermissions = $options->get( MainConfigNames::RevokePermissions );
50 $this->groupInheritance = $options->get( MainConfigNames::GroupInheritsPermissions );
51 }
52
65 public function groupHasPermission( string $group, string $permission ): bool {
66 $inheritsFrom = $this->groupInheritance[$group] ?? false;
67 $has = isset( $this->groupPermissions[$group][$permission] ) &&
68 $this->groupPermissions[$group][$permission];
69 // If the group doesn't have the permission and inherits from somewhere,
70 // check that group too
71 if ( !$has && $inheritsFrom !== false ) {
72 $has = isset( $this->groupPermissions[$inheritsFrom][$permission] ) &&
73 $this->groupPermissions[$inheritsFrom][$permission];
74 }
75 if ( !$has ) {
76 // If they don't have the permission, exit early
77 return false;
78 }
79
80 // Check if the permission has been revoked
81 $revoked = isset( $this->revokePermissions[$group][$permission] ) &&
82 $this->revokePermissions[$group][$permission];
83 if ( !$revoked && $inheritsFrom !== false ) {
84 $revoked = isset( $this->revokePermissions[$inheritsFrom][$permission] ) &&
85 $this->revokePermissions[$inheritsFrom][$permission];
86 }
87
88 return !$revoked;
89 }
90
101 public function getGrantedPermissions( string $group ): array {
102 $rights = array_keys( array_filter( $this->groupPermissions[$group] ?? [] ) );
103 $inheritsFrom = $this->groupInheritance[$group] ?? false;
104 if ( $inheritsFrom !== false ) {
105 $rights = array_merge(
106 $rights,
107 // array_filter removes empty items
108 array_keys( array_filter( $this->groupPermissions[$inheritsFrom] ?? [] ) )
109 );
110 }
111
112 return array_unique( $rights );
113 }
114
122 public function getRevokedPermissions( string $group ): array {
123 $rights = array_keys( array_filter( $this->revokePermissions[$group] ?? [] ) );
124 $inheritsFrom = $this->groupInheritance[$group] ?? false;
125 if ( $inheritsFrom !== false ) {
126 $rights = array_merge(
127 $rights,
128 // array_filter removes empty items
129 array_keys( array_filter( $this->revokePermissions[$inheritsFrom] ?? [] ) )
130 );
131 }
132
133 return array_unique( $rights );
134 }
135
145 public function getGroupPermissions( array $groups ): array {
146 $rights = [];
147 $checkGroups = [];
148
149 // Add inherited groups to the list of groups to check
150 foreach ( $groups as $group ) {
151 $checkGroups[] = $group;
152 if ( isset( $this->groupInheritance[$group] ) ) {
153 $checkGroups[] = $this->groupInheritance[$group];
154 }
155 }
156
157 // grant every granted permission first
158 foreach ( $checkGroups as $group ) {
159 if ( isset( $this->groupPermissions[$group] ) ) {
160 $rights = array_merge(
161 $rights,
162 // array_filter removes empty items
163 array_keys( array_filter( $this->groupPermissions[$group] ) )
164 );
165 }
166 }
167 // now revoke the revoked permissions
168 foreach ( $checkGroups as $group ) {
169 if ( isset( $this->revokePermissions[$group] ) ) {
170 $rights = array_diff(
171 $rights,
172 array_keys( array_filter( $this->revokePermissions[$group] ) )
173 );
174 }
175 }
176 return array_unique( $rights );
177 }
178
185 public function getGroupsWithPermission( string $permission ): array {
186 $allowedGroups = [];
187 $groups = array_unique( array_merge(
188 array_keys( $this->groupPermissions ),
189 array_keys( $this->groupInheritance )
190 ) );
191 foreach ( $groups as $group ) {
192 if ( $this->groupHasPermission( $group, $permission ) ) {
193 $allowedGroups[] = $group;
194 }
195 }
196 return $allowedGroups;
197 }
198}
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.