Debian Codegraph @

extensions/AbuseFilter/includes/AbuseFilterPermissionManager.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\AbuseFilter;
4
5 use LogPage;
6 use MediaWiki\Config\ServiceOptions;
7 use MediaWiki\Extension\AbuseFilter\Filter\AbstractFilter;
8 use MediaWiki\Extension\AbuseFilter\Filter\Flags;
9 use MediaWiki\Permissions\Authority;
10 use MediaWiki\Revision\RevisionRecord;
11 use MediaWiki\User\Options\UserOptionsLookup;
12 use RCCacheEntry;
13
14 /**
15 * This class simplifies the interactions between the AbuseFilter code and Authority, knowing
16 * what rights are required to perform AF-related actions.
17 */
18 class AbuseFilterPermissionManager {
19 public const SERVICE_NAME = 'AbuseFilterPermissionManager';
20
21 public const CONSTRUCTOR_OPTIONS = [
22 'AbuseFilterProtectedVariables',
23 ];
24
25 /**
26 * @var string[] Protected variables defined in config via AbuseFilterProtectedVariables
27 */
28 private $protectedVariables;
29
30 private UserOptionsLookup $userOptionsLookup;
31
32 /**
33 * @param ServiceOptions $options
34 * @param UserOptionsLookup $userOptionsLookup
35 */
36 public function __construct(
37 ServiceOptions $options,
38 UserOptionsLookup $userOptionsLookup
39 ) {
40 $this->protectedVariables = $options->get( 'AbuseFilterProtectedVariables' );
41 $this->userOptionsLookup = $userOptionsLookup;
42 }
43
44 /**
45 * @param Authority $performer
46 * @return bool
47 */
48 public function canEdit( Authority $performer ): bool {
49 $block = $performer->getBlock();
50 return (
51 !( $block && $block->isSitewide() ) &&
52 $performer->isAllowed( 'abusefilter-modify' )
53 );
54 }
55
56 /**
57 * @param Authority $performer
58 * @return bool
59 */
60 public function canEditGlobal( Authority $performer ): bool {
61 return $performer->isAllowed( 'abusefilter-modify-global' );
62 }
63
64 /**
65 * Whether the user can edit the given filter.
66 *
67 * @param Authority $performer
68 * @param AbstractFilter $filter
69 * @return bool
70 */
71 public function canEditFilter( Authority $performer, AbstractFilter $filter ): bool {
72 return (
73 $this->canEdit( $performer ) &&
74 !( $filter->isGlobal() && !$this->canEditGlobal( $performer ) )
75 );
76 }
77
78 /**
79 * Whether the user can edit a filter with restricted actions enabled.
80 *
81 * @param Authority $performer
82 * @return bool
83 */
84 public function canEditFilterWithRestrictedActions( Authority $performer ): bool {
85 return $performer->isAllowed( 'abusefilter-modify-restricted' );
86 }
87
88 /**
89 * @param Authority $performer
90 * @return bool
91 */
92 public function canViewPrivateFilters( Authority $performer ): bool {
93 $block = $performer->getBlock();
94 return (
95 !( $block && $block->isSitewide() ) &&
96 $performer->isAllowedAny(
97 'abusefilter-modify',
98 'abusefilter-view-private'
99 )
100 );
101 }
102
103 /**
104 * @param Authority $performer
105 * @return bool
106 */
107 public function canViewProtectedVariables( Authority $performer ) {
108 $block = $performer->getBlock();
109 return (
110 !( $block && $block->isSitewide() ) &&
111 $performer->isAllowed( 'abusefilter-access-protected-vars' )
112 );
113 }
114
115 /**
116 * @param Authority $performer
117 * @return bool
118 */
119 public function canViewProtectedVariableValues( Authority $performer ) {
120 return (
121 $this->canViewProtectedVariables( $performer ) &&
122 $this->userOptionsLookup->getOption(
123 $performer->getUser(),
124 'abusefilter-protected-vars-view-agreement'
125 )
126 );
127 }
128
129 /**
130 * Return all used protected variables from an array of variables. Ignore user permissions.
131 *
132 * @param string[] $usedVariables
133 * @return string[]
134 */
135 public function getUsedProtectedVariables( array $usedVariables ): array {
136 return array_intersect( $usedVariables, $this->protectedVariables );
137 }
138
139 /**
140 * Check if the filter uses variables that the user is not allowed to use (i.e., variables that are protected, if
141 * the user can't view protected variables), and return them.
142 *
143 * @param Authority $performer
144 * @param string[] $usedVariables
145 * @return string[]
146 */
147 public function getForbiddenVariables( Authority $performer, array $usedVariables ): array {
148 $usedProtectedVariables = array_intersect( $usedVariables, $this->protectedVariables );
149 // All good if protected variables aren't used, or the user can view them.
150 if ( count( $usedProtectedVariables ) === 0 || $this->canViewProtectedVariables( $performer ) ) {
151 return [];
152 }
153 return $usedProtectedVariables;
154 }
155
156 /**
157 * Return an array of protected variables (originally defined in configuration)
158 *
159 * @return string[]
160 */
161 public function getProtectedVariables() {
162 return $this->protectedVariables;
163 }
164
165 /**
166 * @param Authority $performer
167 * @return bool
168 */
169 public function canViewPrivateFiltersLogs( Authority $performer ): bool {
170 return $this->canViewPrivateFilters( $performer ) ||
171 $performer->isAllowed( 'abusefilter-log-private' );
172 }
173
174 /**
175 * @param Authority $performer
176 * @return bool
177 */
178 public function canViewAbuseLog( Authority $performer ): bool {
179 return $performer->isAllowed( 'abusefilter-log' );
180 }
181
182 /**
183 * @param Authority $performer
184 * @return bool
185 */
186 public function canHideAbuseLog( Authority $performer ): bool {
187 return $performer->isAllowed( 'abusefilter-hide-log' );
188 }
189
190 /**
191 * @param Authority $performer
192 * @return bool
193 */
194 public function canRevertFilterActions( Authority $performer ): bool {
195 return $performer->isAllowed( 'abusefilter-revert' );
196 }
197
198 /**
199 * @param Authority $performer
200 * @param int $privacyLevel Bitmask of privacy flags
201 * @todo Take a Filter parameter
202 * @return bool
203 */
204 public function canSeeLogDetailsForFilter( Authority $performer, int $privacyLevel ): bool {
205 if ( !$this->canSeeLogDetails( $performer ) ) {
206 return false;
207 }
208
209 if ( $privacyLevel === Flags::FILTER_PUBLIC ) {
210 return true;
211 }
212 if ( FilterUtils::isHidden( $privacyLevel ) && !$this->canViewPrivateFiltersLogs( $performer ) ) {
213 return false;
214 }
215 if ( FilterUtils::isProtected( $privacyLevel ) && !$this->canViewProtectedVariables( $performer ) ) {
216 return false;
217 }
218
219 return true;
220 }
221
222 /**
223 * @param Authority $performer
224 * @return bool
225 */
226 public function canSeeLogDetails( Authority $performer ): bool {
227 return $performer->isAllowed( 'abusefilter-log-detail' );
228 }
229
230 /**
231 * @param Authority $performer
232 * @return bool
233 */
234 public function canSeePrivateDetails( Authority $performer ): bool {
235 return $performer->isAllowed( 'abusefilter-privatedetails' );
236 }
237
238 /**
239 * @param Authority $performer
240 * @return bool
241 */
242 public function canSeeHiddenLogEntries( Authority $performer ): bool {
243 return $performer->isAllowed( 'abusefilter-hidden-log' );
244 }
245
246 /**
247 * @param Authority $performer
248 * @return bool
249 */
250 public function canUseTestTools( Authority $performer ): bool {
251 // TODO: make independent
252 return $this->canViewPrivateFilters( $performer );
253 }
254
255 /**
256 * Determine whether the current user is allowed to view a revision
257 * at all, given its current visibility restrictions.
258 *
259 * Unlike `RevisionRecord::userCanBitfield`, which checks whether the user
260 * may view a specific deleted aspect of a revision (e.g. text or comment),
261 * this method evaluates whether the revision itself is viewable, considering
262 * all applicable visibility flags together.
263 *
264 * @param int $visibility Current visibility bit field (the `rev_deleted` value)
265 * @param Authority $authority User on whose behalf to check access
266 * @return bool
267 * @todo Consider moving this to core if similar logic is needed elsewhere
268 * @see RevisionRecord::userCanBitfield
269 */
270 public static function hasRevisionAccess( int $visibility, Authority $authority ): bool {
271 if ( !$visibility ) {
272 return true;
273 }
274 if ( $visibility & RevisionRecord::DELETED_RESTRICTED ) {
275 // Suppressed revisions require suppressor rights regardless of other flags
276 return $authority->isAllowedAny( 'suppressrevision', 'viewsuppressed' );
277 }
278 if ( ( $visibility & RevisionRecord::DELETED_TEXT ) && !$authority->isAllowed( 'deletedtext' ) ) {
279 return false;
280 }
281 if ( ( $visibility & ( RevisionRecord::DELETED_COMMENT | RevisionRecord::DELETED_USER ) ) &&
282 !$authority->isAllowed( 'deletedhistory' )
283 ) {
284 return false;
285 }
286 return true;
287 }
288
289 /**
290 * Determine whether the current user is allowed to view a recent change row
291 * at all, given its source and current visibility restrictions.
292 *
293 * Unlike `ChangesList::userCan`, which checks whether the user may view a
294 * specific deleted aspect of a recent change and delegates to
295 * `LogEventsList::userCanBitfield` for log entries or
296 * `RevisionRecord::userCanBitfield` otherwise, this method evaluates whether
297 * the recent change row itself is viewable, considering all applicable
298 * visibility flags together, and delegates to `::hasRevisionAccess` for
299 * non-log entries.
300 *
301 * @param RCCacheEntry|RecentChange $rc
302 * @param Authority $authority User on whose behalf to check
303 * @return bool
304 * @see ChangesList::userCan
305 * @see LogEventsList::userCanBitfield
306 * @see RevisionRecord::userCanBitfield
307 * @see AbuseFilterPermissionManager::hasRevisionAccess
308 */
309 public static function hasRCEntryAccess( $rc, Authority $authority ): bool {
310 $visibility = (int)$rc->getAttribute( 'rc_deleted' );
311 if ( $visibility === 0 ) {
312 return true;
313 }
314 if ( $rc->getAttribute( 'rc_source' ) === RecentChange::SRC_LOG ) {
315 if ( $visibility & LogPage::DELETED_RESTRICTED ) {
316 return $authority->isAllowedAny( 'suppressrevision', 'viewsuppressed' );
317 } else {
318 return $authority->isAllowed( 'deletedhistory' );
319 }
320 }
321 return self::hasRevisionAccess( $visibility, $authority );
322 }
323
324 }