Debian Codegraph @

includes/Permissions/GroupPermissionsLookup.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Permissions;
22
23 use MediaWiki\Config\ServiceOptions;
24 use MediaWiki\MainConfigNames;
25
26 /**
27 * A service class for looking up permissions bestowed to groups, groups bestowed with
28 * permissions, and permissions bestowed by membership in a combination of groups, solely
29 * according to site configuration for group permissions and inheritence thereof.
30 *
31 * This class does *not* account for implicit rights (which are not associated with groups).
32 * Callers might want to use {@see PermissionManager} if this is an issue.
33 *
34 * This class does *not* infer membership in one group (e.g. '*') from membership in another
35 * (e.g. 'user'). Callers must account for this when using {@see self::getGroupPermissions()}.
36 *
37 * @since 1.36
38 * @package MediaWiki\Permissions
39 */
40 class GroupPermissionsLookup {
41
42 /**
43 * @internal
44 */
45 public const CONSTRUCTOR_OPTIONS = [
46 MainConfigNames::GroupInheritsPermissions,
47 MainConfigNames::GroupPermissions,
48 MainConfigNames::RevokePermissions,
49 ];
50
51 /** @var array[] */
52 private $groupPermissions;
53
54 /** @var array[] */
55 private $revokePermissions;
56
57 /** @var string[] */
58 private $groupInheritance;
59
60 /**
61 * @param ServiceOptions $options
62 */
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
70 /**
71 * Check, if the given group has the given permission
72 *
73 * If you're wanting to check whether all users have a permission,
74 * use PermissionManager::isEveryoneAllowed() instead.
75 * That properly checks if it's revoked from anyone.
76 *
77 * @param string $group Group to check
78 * @param string $permission Role to check
79 *
80 * @return bool
81 */
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
108 /**
109 * Get a list of permissions granted to this group. This
110 * must *NOT* be used for permissions checking as it
111 * does not check whether a permission has been revoked
112 * from this group.
113 *
114 * @param string $group Group to get permissions of
115 * @return string[]
116 * @since 1.38
117 */
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
132 /**
133 * Get a list of permissions revoked from this group
134 *
135 * @param string $group Group to get revoked permissions of
136 * @return string[]
137 * @since 1.38
138 */
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
153 /**
154 * Get the permissions associated with membership in a combination of groups
155 *
156 * Group-based revocation of a permission negates all group-based assignments of that
157 * permission.
158 *
159 * @param string[] $groups internal group names
160 * @return string[] permission key names for given groups combined
161 */
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
196 /**
197 * Get all the groups who have a given permission
198 *
199 * @param string $permission
200 * @return string[] internal group names with the given permission
201 */
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 }