Debian Codegraph @

includes/user/UserGroupManager.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\User;
22
23 use InvalidArgumentException;
24 use JobQueueGroup;
25 use LogicException;
26 use ManualLogEntry;
27 use MediaWiki\Config\ServiceOptions;
28 use MediaWiki\Deferred\DeferredUpdates;
29 use MediaWiki\HookContainer\HookContainer;
30 use MediaWiki\HookContainer\HookRunner;
31 use MediaWiki\MainConfigNames;
32 use MediaWiki\Parser\Sanitizer;
33 use MediaWiki\Permissions\Authority;
34 use MediaWiki\Permissions\GroupPermissionsLookup;
35 use MediaWiki\User\TempUser\TempUserConfig;
36 use MediaWiki\WikiMap\WikiMap;
37 use Psr\Log\LoggerInterface;
38 use UserGroupExpiryJob;
39 use Wikimedia\Assert\Assert;
40 use Wikimedia\IPUtils;
41 use Wikimedia\Rdbms\IConnectionProvider;
42 use Wikimedia\Rdbms\IDBAccessObject;
43 use Wikimedia\Rdbms\ILBFactory;
44 use Wikimedia\Rdbms\IReadableDatabase;
45 use Wikimedia\Rdbms\ReadOnlyMode;
46 use Wikimedia\Rdbms\SelectQueryBuilder;
47
48 /**
49 * Manages user groups.
50 * @since 1.35
51 */
52 class UserGroupManager {
53
54 /**
55 * @internal For use by ServiceWiring
56 */
57 public const CONSTRUCTOR_OPTIONS = [
58 MainConfigNames::AddGroups,
59 MainConfigNames::AutoConfirmAge,
60 MainConfigNames::AutoConfirmCount,
61 MainConfigNames::Autopromote,
62 MainConfigNames::AutopromoteOnce,
63 MainConfigNames::AutopromoteOnceLogInRC,
64 MainConfigNames::EmailAuthentication,
65 MainConfigNames::ImplicitGroups,
66 MainConfigNames::GroupInheritsPermissions,
67 MainConfigNames::GroupPermissions,
68 MainConfigNames::GroupsAddToSelf,
69 MainConfigNames::GroupsRemoveFromSelf,
70 MainConfigNames::RevokePermissions,
71 MainConfigNames::RemoveGroups,
72 MainConfigNames::PrivilegedGroups,
73 ];
74
75 /**
76 * Logical operators recognized in $wgAutopromote.
77 *
78 * @since 1.42
79 */
80 public const VALID_OPS = [ '&', '|', '^', '!' ];
81
82 private ServiceOptions $options;
83 private IConnectionProvider $dbProvider;
84 private HookContainer $hookContainer;
85 private HookRunner $hookRunner;
86 private ReadOnlyMode $readOnlyMode;
87 private UserEditTracker $userEditTracker;
88 private GroupPermissionsLookup $groupPermissionsLookup;
89 private JobQueueGroup $jobQueueGroup;
90 private LoggerInterface $logger;
91 private TempUserConfig $tempUserConfig;
92
93 /** @var callable[] */
94 private $clearCacheCallbacks;
95
96 /** @var string|false */
97 private $wikiId;
98
99 /** string key for implicit groups cache */
100 private const CACHE_IMPLICIT = 'implicit';
101
102 /** string key for effective groups cache */
103 private const CACHE_EFFECTIVE = 'effective';
104
105 /** string key for group memberships cache */
106 private const CACHE_MEMBERSHIP = 'membership';
107
108 /** string key for former groups cache */
109 private const CACHE_FORMER = 'former';
110
111 /** string key for former groups cache */
112 private const CACHE_PRIVILEGED = 'privileged';
113
114 /**
115 * @var array Service caches, an assoc. array keyed after the user-keys generated
116 * by the getCacheKey method and storing values in the following format:
117 *
118 * userKey => [
119 * self::CACHE_IMPLICIT => implicit groups cache
120 * self::CACHE_EFFECTIVE => effective groups cache
121 * self::CACHE_MEMBERSHIP => [ ] // Array of UserGroupMembership objects
122 * self::CACHE_FORMER => former groups cache
123 * self::CACHE_PRIVILEGED => privileged groups cache
124 * ]
125 */
126 private $userGroupCache = [];
127
128 /**
129 * @var array An assoc. array that stores query flags used to retrieve user groups
130 * from the database and is stored in the following format:
131 *
132 * userKey => [
133 * self::CACHE_IMPLICIT => implicit groups query flag
134 * self::CACHE_EFFECTIVE => effective groups query flag
135 * self::CACHE_MEMBERSHIP => membership groups query flag
136 * self::CACHE_FORMER => former groups query flag
137 * self::CACHE_PRIVILEGED => privileged groups query flag
138 * ]
139 */
140 private $queryFlagsUsedForCaching = [];
141
142 /**
143 * @internal For use preventing an infinite loop when checking APCOND_BLOCKED
144 * @var array An assoc. array mapping the getCacheKey userKey to a bool indicating
145 * an ongoing condition check.
146 */
147 private $recursionMap = [];
148
149 /**
150 * @param ServiceOptions $options
151 * @param ReadOnlyMode $readOnlyMode
152 * @param ILBFactory $lbFactory
153 * @param HookContainer $hookContainer
154 * @param UserEditTracker $userEditTracker
155 * @param GroupPermissionsLookup $groupPermissionsLookup
156 * @param JobQueueGroup $jobQueueGroup
157 * @param LoggerInterface $logger
158 * @param TempUserConfig $tempUserConfig
159 * @param callable[] $clearCacheCallbacks
160 * @param string|false $wikiId
161 */
162 public function __construct(
163 ServiceOptions $options,
164 ReadOnlyMode $readOnlyMode,
165 ILBFactory $lbFactory,
166 HookContainer $hookContainer,
167 UserEditTracker $userEditTracker,
168 GroupPermissionsLookup $groupPermissionsLookup,
169 JobQueueGroup $jobQueueGroup,
170 LoggerInterface $logger,
171 TempUserConfig $tempUserConfig,
172 array $clearCacheCallbacks = [],
173 $wikiId = UserIdentity::LOCAL
174 ) {
175 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
176 $this->options = $options;
177 $this->dbProvider = $lbFactory;
178 $this->hookContainer = $hookContainer;
179 $this->hookRunner = new HookRunner( $hookContainer );
180 $this->userEditTracker = $userEditTracker;
181 $this->groupPermissionsLookup = $groupPermissionsLookup;
182 $this->jobQueueGroup = $jobQueueGroup;
183 $this->logger = $logger;
184 $this->tempUserConfig = $tempUserConfig;
185 $this->readOnlyMode = $readOnlyMode;
186 $this->clearCacheCallbacks = $clearCacheCallbacks;
187 $this->wikiId = $wikiId;
188 }
189
190 /**
191 * Return the set of defined explicit groups.
192 * The implicit groups (by default *, 'user' and 'autoconfirmed')
193 * are not included, as they are defined automatically, not in the database.
194 * @return string[] internal group names
195 */
196 public function listAllGroups(): array {
197 return array_values( array_unique(
198 array_diff(
199 array_merge(
200 array_keys( $this->options->get( MainConfigNames::GroupPermissions ) ),
201 array_keys( $this->options->get( MainConfigNames::RevokePermissions ) ),
202 array_keys( $this->options->get( MainConfigNames::GroupInheritsPermissions ) )
203 ),
204 $this->listAllImplicitGroups()
205 )
206 ) );
207 }
208
209 /**
210 * Get a list of all configured implicit groups
211 * @return string[]
212 */
213 public function listAllImplicitGroups(): array {
214 return $this->options->get( MainConfigNames::ImplicitGroups );
215 }
216
217 /**
218 * Creates a new UserGroupMembership instance from $row.
219 * The fields required to build an instance could be
220 * found using getQueryInfo() method.
221 *
222 * @param \stdClass $row A database result object
223 *
224 * @return UserGroupMembership
225 */
226 public function newGroupMembershipFromRow( \stdClass $row ): UserGroupMembership {
227 return new UserGroupMembership(
228 (int)$row->ug_user,
229 $row->ug_group,
230 $row->ug_expiry === null ? null : wfTimestamp(
231 TS_MW,
232 $row->ug_expiry
233 )
234 );
235 }
236
237 /**
238 * Load the user groups cache from the provided user groups data
239 * @internal for use by the User object only
240 * @param UserIdentity $user
241 * @param array $userGroups an array of database query results
242 * @param int $queryFlags
243 */
244 public function loadGroupMembershipsFromArray(
245 UserIdentity $user,
246 array $userGroups,
247 int $queryFlags = IDBAccessObject::READ_NORMAL
248 ) {
249 $user->assertWiki( $this->wikiId );
250 $membershipGroups = [];
251 reset( $userGroups );
252 foreach ( $userGroups as $row ) {
253 $ugm = $this->newGroupMembershipFromRow( $row );
254 $membershipGroups[ $ugm->getGroup() ] = $ugm;
255 }
256 $this->setCache(
257 $this->getCacheKey( $user ),
258 self::CACHE_MEMBERSHIP,
259 $membershipGroups,
260 $queryFlags
261 );
262 }
263
264 /**
265 * Get the list of implicit group memberships this user has.
266 *
267 * This includes 'user' if logged in, '*' for all accounts,
268 * and autopromoted groups
269 *
270 * @param UserIdentity $user
271 * @param int $queryFlags
272 * @param bool $recache Whether to avoid the cache
273 * @return string[] internal group names
274 */
275 public function getUserImplicitGroups(
276 UserIdentity $user,
277 int $queryFlags = IDBAccessObject::READ_NORMAL,
278 bool $recache = false
279 ): array {
280 $user->assertWiki( $this->wikiId );
281 $userKey = $this->getCacheKey( $user );
282 if ( $recache ||
283 !isset( $this->userGroupCache[$userKey][self::CACHE_IMPLICIT] ) ||
284 !$this->canUseCachedValues( $user, self::CACHE_IMPLICIT, $queryFlags )
285 ) {
286 $groups = [ '*' ];
287 if ( $this->tempUserConfig->isTempName( $user->getName() ) ) {
288 $groups[] = 'temp';
289 } elseif ( $user->isRegistered() ) {
290 $groups[] = 'user';
291 $groups = array_unique( array_merge(
292 $groups,
293 $this->getUserAutopromoteGroups( $user )
294 ) );
295 }
296 $this->setCache( $userKey, self::CACHE_IMPLICIT, $groups, $queryFlags );
297 if ( $recache ) {
298 // Assure data consistency with rights/groups,
299 // as getUserEffectiveGroups() depends on this function
300 $this->clearUserCacheForKind( $user, self::CACHE_EFFECTIVE );
301 }
302 }
303 return $this->userGroupCache[$userKey][self::CACHE_IMPLICIT];
304 }
305
306 /**
307 * Get the list of implicit group memberships the user has.
308 *
309 * This includes all explicit groups, plus 'user' if logged in,
310 * '*' for all accounts, and autopromoted groups
311 *
312 * @param UserIdentity $user
313 * @param int $queryFlags
314 * @param bool $recache Whether to avoid the cache
315 * @return string[] internal group names
316 */
317 public function getUserEffectiveGroups(
318 UserIdentity $user,
319 int $queryFlags = IDBAccessObject::READ_NORMAL,
320 bool $recache = false
321 ): array {
322 $user->assertWiki( $this->wikiId );
323 $userKey = $this->getCacheKey( $user );
324 // Ignore cache if the $recache flag is set, cached values can not be used
325 // or the cache value is missing
326 if ( $recache ||
327 !$this->canUseCachedValues( $user, self::CACHE_EFFECTIVE, $queryFlags ) ||
328 !isset( $this->userGroupCache[$userKey][self::CACHE_EFFECTIVE] )
329 ) {
330 $groups = array_unique( array_merge(
331 $this->getUserGroups( $user, $queryFlags ), // explicit groups
332 $this->getUserImplicitGroups( $user, $queryFlags, $recache ) // implicit groups
333 ) );
334 // TODO: Deprecate passing out user object in the hook by introducing
335 // an alternative hook
336 if ( $this->hookContainer->isRegistered( 'UserEffectiveGroups' ) ) {
337 $userObj = User::newFromIdentity( $user );
338 $userObj->load();
339 // Hook for additional groups
340 $this->hookRunner->onUserEffectiveGroups( $userObj, $groups );
341 }
342 // Force reindexation of groups when a hook has unset one of them
343 $effectiveGroups = array_values( array_unique( $groups ) );
344 $this->setCache( $userKey, self::CACHE_EFFECTIVE, $effectiveGroups, $queryFlags );
345 }
346 return $this->userGroupCache[$userKey][self::CACHE_EFFECTIVE];
347 }
348
349 /**
350 * Returns the groups the user has belonged to.
351 *
352 * The user may still belong to the returned groups. Compare with
353 * getUserGroups().
354 *
355 * The function will not return groups the user had belonged to before MW 1.17
356 *
357 * @param UserIdentity $user
358 * @param int $queryFlags
359 * @return string[] Names of the groups the user has belonged to.
360 */
361 public function getUserFormerGroups(
362 UserIdentity $user,
363 int $queryFlags = IDBAccessObject::READ_NORMAL
364 ): array {
365 $user->assertWiki( $this->wikiId );
366 $userKey = $this->getCacheKey( $user );
367
368 if ( $this->canUseCachedValues( $user, self::CACHE_FORMER, $queryFlags ) &&
369 isset( $this->userGroupCache[$userKey][self::CACHE_FORMER] )
370 ) {
371 return $this->userGroupCache[$userKey][self::CACHE_FORMER];
372 }
373
374 if ( !$user->isRegistered() ) {
375 // Anon users don't have groups stored in the database
376 return [];
377 }
378
379 $res = $this->getDBConnectionRefForQueryFlags( $queryFlags )->newSelectQueryBuilder()
380 ->select( 'ufg_group' )
381 ->from( 'user_former_groups' )
382 ->where( [ 'ufg_user' => $user->getId( $this->wikiId ) ] )
383 ->caller( __METHOD__ )
384 ->fetchResultSet();
385 $formerGroups = [];
386 foreach ( $res as $row ) {
387 $formerGroups[] = $row->ufg_group;
388 }
389 $this->setCache( $userKey, self::CACHE_FORMER, $formerGroups, $queryFlags );
390
391 return $this->userGroupCache[$userKey][self::CACHE_FORMER];
392 }
393
394 /**
395 * Get the groups for the given user based on $wgAutopromote.
396 *
397 * @param UserIdentity $user The user to get the groups for
398 * @return string[] Array of groups to promote to.
399 *
400 * @see $wgAutopromote
401 */
402 public function getUserAutopromoteGroups( UserIdentity $user ): array {
403 $user->assertWiki( $this->wikiId );
404 $promote = [];
405 // TODO: remove the need for the full user object
406 $userObj = User::newFromIdentity( $user );
407 if ( $userObj->isTemp() ) {
408 return [];
409 }
410 foreach ( $this->options->get( MainConfigNames::Autopromote ) as $group => $cond ) {
411 if ( $this->recCheckCondition( $cond, $userObj ) ) {
412 $promote[] = $group;
413 }
414 }
415
416 $this->hookRunner->onGetAutoPromoteGroups( $userObj, $promote );
417 return $promote;
418 }
419
420 /**
421 * Get the groups for the given user based on the given criteria.
422 *
423 * Does not return groups the user already belongs to or has once belonged.
424 *
425 * @param UserIdentity $user The user to get the groups for
426 * @param string $event Key in $wgAutopromoteOnce (each event has groups/criteria)
427 *
428 * @return string[] Groups the user should be promoted to.
429 *
430 * @see $wgAutopromoteOnce
431 */
432 public function getUserAutopromoteOnceGroups(
433 UserIdentity $user,
434 string $event
435 ): array {
436 $user->assertWiki( $this->wikiId );
437 $autopromoteOnce = $this->options->get( MainConfigNames::AutopromoteOnce );
438 $promote = [];
439
440 if ( isset( $autopromoteOnce[$event] ) && count( $autopromoteOnce[$event] ) ) {
441 // TODO: remove the need for the full user object
442 $userObj = User::newFromIdentity( $user );
443 if ( $userObj->isTemp() ) {
444 return [];
445 }
446 $currentGroups = $this->getUserGroups( $user );
447 $formerGroups = $this->getUserFormerGroups( $user );
448 foreach ( $autopromoteOnce[$event] as $group => $cond ) {
449 // Do not check if the user's already a member
450 if ( in_array( $group, $currentGroups ) ) {
451 continue;
452 }
453 // Do not autopromote if the user has belonged to the group
454 if ( in_array( $group, $formerGroups ) ) {
455 continue;
456 }
457 // Finally - check the conditions
458 if ( $this->recCheckCondition( $cond, $userObj ) ) {
459 $promote[] = $group;
460 }
461 }
462 }
463
464 return $promote;
465 }
466
467 /**
468 * Returns the list of privileged groups that $user belongs to.
469 * Privileged groups are ones that can be abused in a dangerous way.
470 *
471 * Depending on how extensions extend this method, it might return values
472 * that are not strictly user groups (ACL list names, etc.).
473 * It is meant for logging/auditing, not for passing to methods that expect group names.
474 *
475 * @param UserIdentity $user
476 * @param int $queryFlags
477 * @param bool $recache Whether to avoid the cache
478 * @return string[]
479 * @since 1.41 (also backported to 1.39.5 and 1.40.1)
480 * @see $wgPrivilegedGroups
481 * @see https://www.mediawiki.org/wiki/Manual:Hooks/UserGetPrivilegedGroups
482 */
483 public function getUserPrivilegedGroups(
484 UserIdentity $user,
485 int $queryFlags = IDBAccessObject::READ_NORMAL,
486 bool $recache = false
487 ): array {
488 $userKey = $this->getCacheKey( $user );
489
490 if ( !$recache &&
491 $this->canUseCachedValues( $user, self::CACHE_PRIVILEGED, $queryFlags ) &&
492 isset( $this->userGroupCache[$userKey][self::CACHE_PRIVILEGED] )
493 ) {
494 return $this->userGroupCache[$userKey][self::CACHE_PRIVILEGED];
495 }
496
497 if ( !$user->isRegistered() ) {
498 return [];
499 }
500
501 $groups = array_intersect(
502 $this->getUserEffectiveGroups( $user, $queryFlags, $recache ),
503 $this->options->get( MainConfigNames::PrivilegedGroups )
504 );
505
506 $this->hookRunner->onUserPrivilegedGroups( $user, $groups );
507
508 $this->setCache(
509 $this->getCacheKey( $user ),
510 self::CACHE_PRIVILEGED,
511 array_values( array_unique( $groups ) ),
512 $queryFlags
513 );
514
515 return $this->userGroupCache[$userKey][self::CACHE_PRIVILEGED];
516 }
517
518 /**
519 * Recursively check a condition. Conditions are in the form
520 * [ '&' or '|' or '^' or '!', cond1, cond2, ... ]
521 * where cond1, cond2, ... are themselves conditions; *OR*
522 * APCOND_EMAILCONFIRMED, *OR*
523 * [ APCOND_EMAILCONFIRMED ], *OR*
524 * [ APCOND_EDITCOUNT, number of edits ], *OR*
525 * [ APCOND_AGE, seconds since registration ], *OR*
526 * similar constructs defined by extensions.
527 * This function evaluates the former type recursively, and passes off to
528 * checkCondition for evaluation of the latter type.
529 *
530 * If you change the logic of this method, please update
531 * ApiQuerySiteinfo::appendAutoPromote(), as it depends on this method.
532 *
533 * @param mixed $cond A condition, possibly containing other conditions
534 * @param User $user The user to check the conditions against
535 *
536 * @return bool Whether the condition is true
537 */
538 private function recCheckCondition( $cond, User $user ): bool {
539 if ( is_array( $cond ) && count( $cond ) >= 2 && in_array( $cond[0], self::VALID_OPS ) ) {
540 // Recursive condition
541 if ( $cond[0] == '&' ) { // AND (all conds pass)
542 foreach ( array_slice( $cond, 1 ) as $subcond ) {
543 if ( !$this->recCheckCondition( $subcond, $user ) ) {
544 return false;
545 }
546 }
547
548 return true;
549 } elseif ( $cond[0] == '|' ) { // OR (at least one cond passes)
550 foreach ( array_slice( $cond, 1 ) as $subcond ) {
551 if ( $this->recCheckCondition( $subcond, $user ) ) {
552 return true;
553 }
554 }
555
556 return false;
557 } elseif ( $cond[0] == '^' ) { // XOR (exactly one cond passes)
558 if ( count( $cond ) > 3 ) {
559 $this->logger->warning(
560 'recCheckCondition() given XOR ("^") condition on three or more conditions.' .
561 ' Check your $wgAutopromote and $wgAutopromoteOnce settings.'
562 );
563 }
564 return $this->recCheckCondition( $cond[1], $user )
565 xor $this->recCheckCondition( $cond[2], $user );
566 } elseif ( $cond[0] == '!' ) { // NOT (no conds pass)
567 foreach ( array_slice( $cond, 1 ) as $subcond ) {
568 if ( $this->recCheckCondition( $subcond, $user ) ) {
569 return false;
570 }
571 }
572
573 return true;
574 }
575 }
576 // If we got here, the array presumably does not contain other conditions;
577 // it's not recursive. Pass it off to checkCondition.
578 if ( !is_array( $cond ) ) {
579 $cond = [ $cond ];
580 }
581
582 return $this->checkCondition( $cond, $user );
583 }
584
585 /**
586 * As recCheckCondition, but *not* recursive. The only valid conditions
587 * are those whose first element is one of APCOND_* defined in Defines.php.
588 * Other types will throw an exception if no extension evaluates them.
589 *
590 * @param array $cond A condition, which must not contain other conditions
591 * @param User $user The user to check the condition against
592 * @return bool Whether the condition is true for the user
593 * @throws InvalidArgumentException if autopromote condition was not recognized.
594 * @throws LogicException if APCOND_BLOCKED is checked again before returning a result.
595 */
596 private function checkCondition( array $cond, User $user ): bool {
597 if ( count( $cond ) < 1 ) {
598 return false;
599 }
600
601 switch ( $cond[0] ) {
602 case APCOND_EMAILCONFIRMED:
603 if ( Sanitizer::validateEmail( $user->getEmail() ) ) {
604 if ( $this->options->get( MainConfigNames::EmailAuthentication ) ) {
605 return (bool)$user->getEmailAuthenticationTimestamp();
606 } else {
607 return true;
608 }
609 }
610 return false;
611 case APCOND_EDITCOUNT:
612 $reqEditCount = $cond[1] ?? $this->options->get( MainConfigNames::AutoConfirmCount );
613
614 // T157718: Avoid edit count lookup if specified edit count is 0 or invalid
615 if ( $reqEditCount <= 0 ) {
616 return true;
617 }
618 return (int)$this->userEditTracker->getUserEditCount( $user ) >= $reqEditCount;
619 case APCOND_AGE:
620 $reqAge = $cond[1] ?? $this->options->get( MainConfigNames::AutoConfirmAge );
621 $age = time() - (int)wfTimestampOrNull( TS_UNIX, $user->getRegistration() );
622 return $age >= $reqAge;
623 case APCOND_AGE_FROM_EDIT:
624 $age = time() - (int)wfTimestampOrNull(
625 TS_UNIX, $this->userEditTracker->getFirstEditTimestamp( $user ) );
626 return $age >= $cond[1];
627 case APCOND_INGROUPS:
628 $groups = array_slice( $cond, 1 );
629 return count( array_intersect( $groups, $this->getUserGroups( $user ) ) ) == count( $groups );
630 case APCOND_ISIP:
631 return $cond[1] == $user->getRequest()->getIP();
632 case APCOND_IPINRANGE:
633 return IPUtils::isInRange( $user->getRequest()->getIP(), $cond[1] );
634 case APCOND_BLOCKED:
635 // Because checking for ipblock-exempt leads back to here (thus infinite recursion),
636 // we if we've been here before for this user without having returned a value.
637 // See T270145 and T349608
638 $userKey = $this->getCacheKey( $user );
639 if ( $this->recursionMap[$userKey] ?? false ) {
640 throw new LogicException(
641 "Unexpected recursion! APCOND_BLOCKED is being checked during" .
642 " an existing APCOND_BLOCKED check for \"{$user->getName()}\"!"
643 );
644 }
645 $this->recursionMap[$userKey] = true;
646 // Setting the second parameter here to true prevents us from getting back here
647 // during standard MediaWiki core behavior
648 $block = $user->getBlock( IDBAccessObject::READ_LATEST, true );
649 $this->recursionMap[$userKey] = false;
650
651 return $block && $block->isSitewide();
652 case APCOND_ISBOT:
653 return in_array( 'bot', $this->groupPermissionsLookup
654 ->getGroupPermissions( $this->getUserGroups( $user ) ) );
655 default:
656 $result = null;
657 $this->hookRunner->onAutopromoteCondition( $cond[0],
658 // @phan-suppress-next-line PhanTypeMismatchArgument Type mismatch on pass-by-ref args
659 array_slice( $cond, 1 ), $user, $result );
660 if ( $result === null ) {
661 throw new InvalidArgumentException(
662 "Unrecognized condition {$cond[0]} for autopromotion!"
663 );
664 }
665
666 return (bool)$result;
667 }
668 }
669
670 /**
671 * Add the user to the group if he/she meets given criteria.
672 *
673 * Contrary to autopromotion by $wgAutopromote, the group will be
674 * possible to remove manually via Special:UserRights. In such case it
675 * will not be re-added automatically. The user will also not lose the
676 * group if they no longer meet the criteria.
677 *
678 * @param UserIdentity $user User to add to the groups
679 * @param string $event Key in $wgAutopromoteOnce (each event has groups/criteria)
680 *
681 * @return string[] Array of groups the user has been promoted to.
682 *
683 * @see $wgAutopromoteOnce
684 */
685 public function addUserToAutopromoteOnceGroups(
686 UserIdentity $user,
687 string $event
688 ): array {
689 $user->assertWiki( $this->wikiId );
690 Assert::precondition(
691 !$this->wikiId || WikiMap::isCurrentWikiDbDomain( $this->wikiId ),
692 __METHOD__ . " is not supported for foreign wikis: {$this->wikiId} used"
693 );
694
695 if (
696 $this->readOnlyMode->isReadOnly( $this->wikiId ) ||
697 !$user->isRegistered() ||
698 $this->tempUserConfig->isTempName( $user->getName() )
699 ) {
700 return [];
701 }
702
703 $toPromote = $this->getUserAutopromoteOnceGroups( $user, $event );
704 if ( $toPromote === [] ) {
705 return [];
706 }
707
708 $userObj = User::newFromIdentity( $user );
709 if ( !$userObj->checkAndSetTouched() ) {
710 return []; // raced out (bug T48834)
711 }
712
713 $oldGroups = $this->getUserGroups( $user ); // previous groups
714 $oldUGMs = $this->getUserGroupMemberships( $user );
715 $this->addUserToMultipleGroups( $user, $toPromote );
716 $newGroups = array_merge( $oldGroups, $toPromote ); // all groups
717 $newUGMs = $this->getUserGroupMemberships( $user );
718
719 // update groups in external authentication database
720 // TODO: deprecate passing full User object to hook
721 $this->hookRunner->onUserGroupsChanged(
722 $userObj,
723 $toPromote, [],
724 false,
725 false,
726 $oldUGMs,
727 $newUGMs
728 );
729
730 $logEntry = new ManualLogEntry( 'rights', 'autopromote' );
731 $logEntry->setPerformer( $user );
732 $logEntry->setTarget( $userObj->getUserPage() );
733 $logEntry->setParameters( [
734 '4::oldgroups' => $oldGroups,
735 '5::newgroups' => $newGroups,
736 ] );
737 $logid = $logEntry->insert();
738 if ( $this->options->get( MainConfigNames::AutopromoteOnceLogInRC ) ) {
739 $logEntry->publish( $logid );
740 }
741
742 return $toPromote;
743 }
744
745 /**
746 * Get the list of explicit group memberships this user has.
747 * The implicit * and user groups are not included.
748 *
749 * @param UserIdentity $user
750 * @param int $queryFlags
751 * @return string[]
752 */
753 public function getUserGroups(
754 UserIdentity $user,
755 int $queryFlags = IDBAccessObject::READ_NORMAL
756 ): array {
757 return array_keys( $this->getUserGroupMemberships( $user, $queryFlags ) );
758 }
759
760 /**
761 * Loads and returns UserGroupMembership objects for all the groups a user currently
762 * belongs to.
763 *
764 * @param UserIdentity $user the user to search for
765 * @param int $queryFlags
766 * @return UserGroupMembership[] Associative array of (group name => UserGroupMembership object)
767 */
768 public function getUserGroupMemberships(
769 UserIdentity $user,
770 int $queryFlags = IDBAccessObject::READ_NORMAL
771 ): array {
772 $user->assertWiki( $this->wikiId );
773 $userKey = $this->getCacheKey( $user );
774
775 if ( $this->canUseCachedValues( $user, self::CACHE_MEMBERSHIP, $queryFlags ) &&
776 isset( $this->userGroupCache[$userKey][self::CACHE_MEMBERSHIP] )
777 ) {
778 /** @suppress PhanTypeMismatchReturn */
779 return $this->userGroupCache[$userKey][self::CACHE_MEMBERSHIP];
780 }
781
782 if ( !$user->isRegistered() ) {
783 // Anon users don't have groups stored in the database
784 return [];
785 }
786
787 $queryBuilder = $this->newQueryBuilder( $this->getDBConnectionRefForQueryFlags( $queryFlags ) );
788 $res = $queryBuilder
789 ->where( [ 'ug_user' => $user->getId( $this->wikiId ) ] )
790 ->caller( __METHOD__ )
791 ->fetchResultSet();
792
793 $ugms = [];
794 foreach ( $res as $row ) {
795 $ugm = $this->newGroupMembershipFromRow( $row );
796 if ( !$ugm->isExpired() ) {
797 $ugms[$ugm->getGroup()] = $ugm;
798 }
799 }
800 ksort( $ugms );
801
802 $this->setCache( $userKey, self::CACHE_MEMBERSHIP, $ugms, $queryFlags );
803
804 return $ugms;
805 }
806
807 /**
808 * Add the user to the given group. This takes immediate effect.
809 * If the user is already in the group, the expiry time will be updated to the new
810 * expiry time. (If $expiry is omitted or null, the membership will be altered to
811 * never expire.)
812 *
813 * @param UserIdentity $user
814 * @param string $group Name of the group to add
815 * @param string|null $expiry Optional expiry timestamp in any format acceptable to
816 * wfTimestamp(), or null if the group assignment should not expire
817 * @param bool $allowUpdate Whether to perform "upsert" instead of INSERT
818 *
819 * @throws InvalidArgumentException
820 * @return bool
821 */
822 public function addUserToGroup(
823 UserIdentity $user,
824 string $group,
825 ?string $expiry = null,
826 bool $allowUpdate = false
827 ): bool {
828 $user->assertWiki( $this->wikiId );
829 if ( $this->readOnlyMode->isReadOnly( $this->wikiId ) ) {
830 return false;
831 }
832
833 $isTemp = $this->tempUserConfig->isTempName( $user->getName() );
834 if ( !$user->isRegistered() ) {
835 throw new InvalidArgumentException(
836 'UserGroupManager::addUserToGroup() needs a positive user ID. ' .
837 'Perhaps addUserToGroup() was called before the user was added to the database.'
838 );
839 }
840 if ( $isTemp ) {
841 throw new InvalidArgumentException(
842 'UserGroupManager::addUserToGroup() cannot be called on a temporary user.'
843 );
844 }
845
846 if ( $expiry ) {
847 $expiry = wfTimestamp( TS_MW, $expiry );
848 }
849
850 // TODO: Deprecate passing out user object in the hook by introducing
851 // an alternative hook
852 if ( $this->hookContainer->isRegistered( 'UserAddGroup' ) ) {
853 $userObj = User::newFromIdentity( $user );
854 $userObj->load();
855 if ( !$this->hookRunner->onUserAddGroup( $userObj, $group, $expiry ) ) {
856 return false;
857 }
858 }
859
860 $oldUgms = $this->getUserGroupMemberships( $user, IDBAccessObject::READ_LATEST );
861 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
862
863 $dbw->startAtomic( __METHOD__ );
864 $dbw->newInsertQueryBuilder()
865 ->insertInto( 'user_groups' )
866 ->ignore()
867 ->row( [
868 'ug_user' => $user->getId( $this->wikiId ),
869 'ug_group' => $group,
870 'ug_expiry' => $expiry ? $dbw->timestamp( $expiry ) : null,
871 ] )
872 ->caller( __METHOD__ )->execute();
873
874 $affected = $dbw->affectedRows();
875 if ( !$affected ) {
876 // Conflicting row already exists; it should be overridden if it is either expired
877 // or if $allowUpdate is true and the current row is different than the loaded row.
878 $conds = [
879 'ug_user' => $user->getId( $this->wikiId ),
880 'ug_group' => $group
881 ];
882 if ( $allowUpdate ) {
883 // Update the current row if its expiry does not match that of the loaded row
884 $conds[] = $expiry
885 ? $dbw->expr( 'ug_expiry', '=', null )
886 ->or( 'ug_expiry', '!=', $dbw->timestamp( $expiry ) )
887 : $dbw->expr( 'ug_expiry', '!=', null );
888 } else {
889 // Update the current row if it is expired
890 $conds[] = $dbw->expr( 'ug_expiry', '<', $dbw->timestamp() );
891 }
892 $dbw->newUpdateQueryBuilder()
893 ->update( 'user_groups' )
894 ->set( [ 'ug_expiry' => $expiry ? $dbw->timestamp( $expiry ) : null ] )
895 ->where( $conds )
896 ->caller( __METHOD__ )->execute();
897 $affected = $dbw->affectedRows();
898 }
899 $dbw->endAtomic( __METHOD__ );
900
901 // Purge old, expired memberships from the DB
902 DeferredUpdates::addCallableUpdate( function ( $fname ) {
903 $dbr = $this->dbProvider->getReplicaDatabase( $this->wikiId );
904 $hasExpiredRow = (bool)$dbr->newSelectQueryBuilder()
905 ->select( '1' )
906 ->from( 'user_groups' )
907 ->where( [ $dbr->expr( 'ug_expiry', '<', $dbr->timestamp() ) ] )
908 ->caller( $fname )
909 ->fetchField();
910 if ( $hasExpiredRow ) {
911 $this->jobQueueGroup->push( new UserGroupExpiryJob( [] ) );
912 }
913 } );
914
915 if ( $affected > 0 ) {
916 $oldUgms[$group] = new UserGroupMembership( $user->getId( $this->wikiId ), $group, $expiry );
917 if ( !$oldUgms[$group]->isExpired() ) {
918 $this->setCache(
919 $this->getCacheKey( $user ),
920 self::CACHE_MEMBERSHIP,
921 $oldUgms,
922 IDBAccessObject::READ_LATEST
923 );
924 $this->clearUserCacheForKind( $user, self::CACHE_EFFECTIVE );
925 }
926 foreach ( $this->clearCacheCallbacks as $callback ) {
927 $callback( $user );
928 }
929 return true;
930 }
931 return false;
932 }
933
934 /**
935 * Add the user to the given list of groups.
936 *
937 * @since 1.37
938 *
939 * @param UserIdentity $user
940 * @param string[] $groups Names of the groups to add
941 * @param string|null $expiry Optional expiry timestamp in any format acceptable to
942 * wfTimestamp(), or null if the group assignment should not expire
943 * @param bool $allowUpdate Whether to perform "upsert" instead of INSERT
944 *
945 * @throws InvalidArgumentException
946 */
947 public function addUserToMultipleGroups(
948 UserIdentity $user,
949 array $groups,
950 ?string $expiry = null,
951 bool $allowUpdate = false
952 ) {
953 foreach ( $groups as $group ) {
954 $this->addUserToGroup( $user, $group, $expiry, $allowUpdate );
955 }
956 }
957
958 /**
959 * Remove the user from the given group. This takes immediate effect.
960 *
961 * @param UserIdentity $user
962 * @param string $group Name of the group to remove
963 * @throws InvalidArgumentException
964 * @return bool
965 */
966 public function removeUserFromGroup( UserIdentity $user, string $group ): bool {
967 $user->assertWiki( $this->wikiId );
968 // TODO: Deprecate passing out user object in the hook by introducing
969 // an alternative hook
970 if ( $this->hookContainer->isRegistered( 'UserRemoveGroup' ) ) {
971 $userObj = User::newFromIdentity( $user );
972 $userObj->load();
973 if ( !$this->hookRunner->onUserRemoveGroup( $userObj, $group ) ) {
974 return false;
975 }
976 }
977
978 if ( $this->readOnlyMode->isReadOnly( $this->wikiId ) ) {
979 return false;
980 }
981
982 if ( !$user->isRegistered() ) {
983 throw new InvalidArgumentException(
984 'UserGroupManager::removeUserFromGroup() needs a positive user ID. ' .
985 'Perhaps removeUserFromGroup() was called before the user was added to the database.'
986 );
987 }
988
989 $oldUgms = $this->getUserGroupMemberships( $user, IDBAccessObject::READ_LATEST );
990 $oldFormerGroups = $this->getUserFormerGroups( $user, IDBAccessObject::READ_LATEST );
991 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
992 $dbw->newDeleteQueryBuilder()
993 ->deleteFrom( 'user_groups' )
994 ->where( [ 'ug_user' => $user->getId( $this->wikiId ), 'ug_group' => $group ] )
995 ->caller( __METHOD__ )->execute();
996
997 if ( !$dbw->affectedRows() ) {
998 return false;
999 }
1000 // Remember that the user was in this group
1001 $dbw->newInsertQueryBuilder()
1002 ->insertInto( 'user_former_groups' )
1003 ->ignore()
1004 ->row( [ 'ufg_user' => $user->getId( $this->wikiId ), 'ufg_group' => $group ] )
1005 ->caller( __METHOD__ )->execute();
1006
1007 unset( $oldUgms[$group] );
1008 $userKey = $this->getCacheKey( $user );
1009 $this->setCache( $userKey, self::CACHE_MEMBERSHIP, $oldUgms, IDBAccessObject::READ_LATEST );
1010 $oldFormerGroups[] = $group;
1011 $this->setCache( $userKey, self::CACHE_FORMER, $oldFormerGroups, IDBAccessObject::READ_LATEST );
1012 $this->clearUserCacheForKind( $user, self::CACHE_EFFECTIVE );
1013 foreach ( $this->clearCacheCallbacks as $callback ) {
1014 $callback( $user );
1015 }
1016 return true;
1017 }
1018
1019 /**
1020 * Return the query builder to build upon and query
1021 *
1022 * @param IReadableDatabase $db
1023 * @return SelectQueryBuilder
1024 * @internal
1025 */
1026 public function newQueryBuilder( IReadableDatabase $db ): SelectQueryBuilder {
1027 return $db->newSelectQueryBuilder()
1028 ->select( [
1029 'ug_user',
1030 'ug_group',
1031 'ug_expiry',
1032 ] )
1033 ->from( 'user_groups' );
1034 }
1035
1036 /**
1037 * Purge expired memberships from the user_groups table
1038 * @internal
1039 * @note this could be slow and is intended for use in a background job
1040 * @return int|false false if purging wasn't attempted (e.g. because of
1041 * readonly), the number of rows purged (might be 0) otherwise
1042 */
1043 public function purgeExpired() {
1044 if ( $this->readOnlyMode->isReadOnly( $this->wikiId ) ) {
1045 return false;
1046 }
1047
1048 $ticket = $this->dbProvider->getEmptyTransactionTicket( __METHOD__ );
1049 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
1050
1051 $lockKey = "{$dbw->getDomainID()}:UserGroupManager:purge"; // per-wiki
1052 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__, 0 );
1053 if ( !$scopedLock ) {
1054 return false; // already running
1055 }
1056
1057 $now = time();
1058 $purgedRows = 0;
1059 do {
1060 $dbw->startAtomic( __METHOD__ );
1061 $res = $this->newQueryBuilder( $dbw )
1062 ->where( [ $dbw->expr( 'ug_expiry', '<', $dbw->timestamp( $now ) ) ] )
1063 ->forUpdate()
1064 ->limit( 100 )
1065 ->caller( __METHOD__ )
1066 ->fetchResultSet();
1067
1068 if ( $res->numRows() > 0 ) {
1069 $insertData = []; // array of users/groups to insert to user_former_groups
1070 $deleteCond = []; // array for deleting the rows that are to be moved around
1071 foreach ( $res as $row ) {
1072 $insertData[] = [ 'ufg_user' => $row->ug_user, 'ufg_group' => $row->ug_group ];
1073 $deleteCond[] = $dbw
1074 ->expr( 'ug_user', '=', $row->ug_user )
1075 ->and( 'ug_group', '=', $row->ug_group );
1076 }
1077 // Delete the rows we're about to move
1078 $dbw->newDeleteQueryBuilder()
1079 ->deleteFrom( 'user_groups' )
1080 ->where( $dbw->orExpr( $deleteCond ) )
1081 ->caller( __METHOD__ )->execute();
1082 // Push the groups to user_former_groups
1083 $dbw->newInsertQueryBuilder()
1084 ->insertInto( 'user_former_groups' )
1085 ->ignore()
1086 ->rows( $insertData )
1087 ->caller( __METHOD__ )->execute();
1088 // Count how many rows were purged
1089 $purgedRows += $res->numRows();
1090 }
1091
1092 $dbw->endAtomic( __METHOD__ );
1093
1094 $this->dbProvider->commitAndWaitForReplication( __METHOD__, $ticket );
1095 } while ( $res->numRows() > 0 );
1096 return $purgedRows;
1097 }
1098
1099 /**
1100 * @param array $config
1101 * @param string $group
1102 * @return string[]
1103 */
1104 private function expandChangeableGroupConfig( array $config, string $group ): array {
1105 if ( empty( $config[$group] ) ) {
1106 return [];
1107 } elseif ( $config[$group] === true ) {
1108 // You get everything
1109 return $this->listAllGroups();
1110 } elseif ( is_array( $config[$group] ) ) {
1111 return $config[$group];
1112 }
1113 return [];
1114 }
1115
1116 /**
1117 * Returns an array of the groups that a particular group can add/remove.
1118 *
1119 * @since 1.37
1120 * @param string $group The group to check for whether it can add/remove
1121 * @return array [
1122 * 'add' => [ addablegroups ],
1123 * 'remove' => [ removablegroups ],
1124 * 'add-self' => [ addablegroups to self ],
1125 * 'remove-self' => [ removable groups from self ] ]
1126 */
1127 public function getGroupsChangeableByGroup( string $group ): array {
1128 return [
1129 'add' => $this->expandChangeableGroupConfig(
1130 $this->options->get( MainConfigNames::AddGroups ), $group
1131 ),
1132 'remove' => $this->expandChangeableGroupConfig(
1133 $this->options->get( MainConfigNames::RemoveGroups ), $group
1134 ),
1135 'add-self' => $this->expandChangeableGroupConfig(
1136 $this->options->get( MainConfigNames::GroupsAddToSelf ), $group
1137 ),
1138 'remove-self' => $this->expandChangeableGroupConfig(
1139 $this->options->get( MainConfigNames::GroupsRemoveFromSelf ), $group
1140 ),
1141 ];
1142 }
1143
1144 /**
1145 * Returns an array of groups that this $actor can add and remove.
1146 *
1147 * @since 1.37
1148 * @param Authority $authority
1149 * @return array [
1150 * 'add' => [ addablegroups ],
1151 * 'remove' => [ removablegroups ],
1152 * 'add-self' => [ addablegroups to self ],
1153 * 'remove-self' => [ removable groups from self ]
1154 * ]
1155 * @phan-return array{add:list<string>,remove:list<string>,add-self:list<string>,remove-self:list<string>}
1156 */
1157 public function getGroupsChangeableBy( Authority $authority ): array {
1158 if ( $authority->isAllowed( 'userrights' ) ) {
1159 // This group gives the right to modify everything (reverse-
1160 // compatibility with old "userrights lets you change
1161 // everything")
1162 // Using array_merge to make the groups reindexed
1163 $all = array_merge( $this->listAllGroups() );
1164 return [
1165 'add' => $all,
1166 'remove' => $all,
1167 'add-self' => [],
1168 'remove-self' => []
1169 ];
1170 }
1171
1172 // Okay, it's not so simple, we will have to go through the arrays
1173 $groups = [
1174 'add' => [],
1175 'remove' => [],
1176 'add-self' => [],
1177 'remove-self' => []
1178 ];
1179 $actorGroups = $this->getUserEffectiveGroups( $authority->getUser() );
1180
1181 foreach ( $actorGroups as $actorGroup ) {
1182 $groups = array_merge_recursive(
1183 $groups, $this->getGroupsChangeableByGroup( $actorGroup )
1184 );
1185 $groups['add'] = array_unique( $groups['add'] );
1186 $groups['remove'] = array_unique( $groups['remove'] );
1187 $groups['add-self'] = array_unique( $groups['add-self'] );
1188 $groups['remove-self'] = array_unique( $groups['remove-self'] );
1189 }
1190 return $groups;
1191 }
1192
1193 /**
1194 * Cleans cached group memberships for a given user
1195 *
1196 * @param UserIdentity $user
1197 */
1198 public function clearCache( UserIdentity $user ) {
1199 $user->assertWiki( $this->wikiId );
1200 $userKey = $this->getCacheKey( $user );
1201 unset( $this->userGroupCache[$userKey] );
1202 unset( $this->queryFlagsUsedForCaching[$userKey] );
1203 }
1204
1205 /**
1206 * Sets cached group memberships and query flags for a given user
1207 *
1208 * @param string $userKey
1209 * @param string $cacheKind one of self::CACHE_KIND_* constants
1210 * @param array $groupValue
1211 * @param int $queryFlags
1212 */
1213 private function setCache(
1214 string $userKey,
1215 string $cacheKind,
1216 array $groupValue,
1217 int $queryFlags
1218 ) {
1219 $this->userGroupCache[$userKey][$cacheKind] = $groupValue;
1220 $this->queryFlagsUsedForCaching[$userKey][$cacheKind] = $queryFlags;
1221 }
1222
1223 /**
1224 * Clears a cached group membership and query key for a given user
1225 *
1226 * @param UserIdentity $user
1227 * @param string $cacheKind one of self::CACHE_* constants
1228 */
1229 private function clearUserCacheForKind( UserIdentity $user, string $cacheKind ) {
1230 $userKey = $this->getCacheKey( $user );
1231 unset( $this->userGroupCache[$userKey][$cacheKind] );
1232 unset( $this->queryFlagsUsedForCaching[$userKey][$cacheKind] );
1233 }
1234
1235 /**
1236 * @param int $recency a bit field composed of IDBAccessObject::READ_XXX flags
1237 * @return IReadableDatabase
1238 */
1239 private function getDBConnectionRefForQueryFlags( int $recency ): IReadableDatabase {
1240 if ( ( IDBAccessObject::READ_LATEST & $recency ) == IDBAccessObject::READ_LATEST ) {
1241 return $this->dbProvider->getPrimaryDatabase( $this->wikiId );
1242 }
1243 return $this->dbProvider->getReplicaDatabase( $this->wikiId );
1244 }
1245
1246 /**
1247 * Gets a unique key for various caches.
1248 * @param UserIdentity $user
1249 * @return string
1250 */
1251 private function getCacheKey( UserIdentity $user ): string {
1252 return $user->isRegistered() ? "u:{$user->getId( $this->wikiId )}" : "anon:{$user->getName()}";
1253 }
1254
1255 /**
1256 * Determines if it's ok to use cached options values for a given user and query flags
1257 * @param UserIdentity $user
1258 * @param string $cacheKind one of self::CACHE_* constants
1259 * @param int $queryFlags
1260 * @return bool
1261 */
1262 private function canUseCachedValues(
1263 UserIdentity $user,
1264 string $cacheKind,
1265 int $queryFlags
1266 ): bool {
1267 if ( !$user->isRegistered() ) {
1268 // Anon users don't have groups stored in the database,
1269 // so $queryFlags are ignored.
1270 return true;
1271 }
1272 if ( $queryFlags >= IDBAccessObject::READ_LOCKING ) {
1273 return false;
1274 }
1275 $userKey = $this->getCacheKey( $user );
1276 $queryFlagsUsed = $this->queryFlagsUsedForCaching[$userKey][$cacheKind] ?? IDBAccessObject::READ_NONE;
1277 return $queryFlagsUsed >= $queryFlags;
1278 }
1279 }