MediaWiki master
MultiFormatUserIdentityLookup.php
Go to the documentation of this file.
1<?php
6
7namespace MediaWiki\User;
8
9use MediaWiki\Config\ServiceOptions;
10use MediaWiki\MainConfigNames;
11use MediaWiki\Permissions\Authority;
12use MediaWiki\Status\Status;
13use MediaWiki\WikiMap\WikiMap;
14use Wikimedia\IPUtils;
15
30
32 public const CONSTRUCTOR_OPTIONS = [
35 ];
36
37 public function __construct(
38 private readonly ActorStoreFactory $actorStoreFactory,
39 private readonly UserFactory $userFactory,
40 private readonly ServiceOptions $options,
41 ) {
42 $this->options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
43 }
44
60 public function getUserIdentity( string $designator, ?Authority $viewer = null ): Status {
61 [ $name, $wikiId ] = $this->parseUserDesignator( $designator );
62
63 // Check if the wikiId is valid
64 if ( $wikiId !== UserIdentity::LOCAL ) {
65 $localDatabases = $this->options->get( MainConfigNames::LocalDatabases );
66 if ( !in_array( $wikiId, $localDatabases ) ) {
67 return Status::newFatal( 'userrights-nodatabase', $wikiId );
68 }
69 }
70
71 if ( $name === '' ) {
72 return Status::newFatal( 'nouserspecified' );
73 }
74
75 if ( IPUtils::isValid( $name ) ) {
76 return Status::newGood( UserIdentityValue::newAnonymous( $name, $wikiId ) );
77 }
78
79 $userIdentityLookup = $this->actorStoreFactory->getUserIdentityLookup( $wikiId );
80 if ( $name[0] == '#' ) {
81 $id = intval( substr( $name, 1 ) );
82 $user = $userIdentityLookup->getUserIdentityByUserId( $id );
83 } else {
84 $user = $userIdentityLookup->getUserIdentityByName( $name );
85 }
86
87 if ( !$user ) {
88 return Status::newFatal( 'nosuchusershort', $designator );
89 }
90
91 // If an authority is specified, check if the viewer is allowed to see the user
92 // If they can't, pretend the user doesn't exist
93 if (
94 $viewer !== null &&
95 $user->getWikiId() === UserIdentity::LOCAL &&
96 $this->userFactory->newFromUserIdentity( $user )->isHidden() &&
97 !$viewer->isAllowed( 'hideuser' )
98 ) {
99 return Status::newFatal( 'nosuchusershort', $designator );
100 }
101
102 return Status::newGood( $user );
103 }
104
110 private function parseUserDesignator( string $designator ): array {
111 $interwikiSeparator = $this->options->get( MainConfigNames::UserrightsInterwikiDelimiter );
112 $designatorParts = explode( $interwikiSeparator, $designator );
113
114 $name = trim( $designator );
116 if ( count( $designatorParts ) >= 2 ) {
117 $name = trim( $designatorParts[0] );
118 $wikiId = trim( $designatorParts[1] );
119
120 if ( WikiMap::isCurrentWikiId( $wikiId ) ) {
122 }
123 }
124 return [ $name, $wikiId ];
125 }
126}
A class for passing options to services.
A class containing constants representing the names of configuration variables.
const LocalDatabases
Name constant for the LocalDatabases setting, for use with Config\get().
const UserrightsInterwikiDelimiter
Name constant for the UserrightsInterwikiDelimiter setting, for use with Config\get().
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
ActorStore factory for any wiki domain.
A service to look up user identities based on the user input.
getUserIdentity(string $designator, ?Authority $viewer=null)
Looks up for a user identity based on the passed designator string.
__construct(private readonly ActorStoreFactory $actorStoreFactory, private readonly UserFactory $userFactory, private readonly ServiceOptions $options,)
Create User objects.
static newAnonymous(string $name, $wikiId=self::LOCAL)
Create UserIdentity for an anonymous user.
options(array $options)
Manually set multiple options in the $options array to be passed to IReadableDatabase\select().
const LOCAL
Wiki ID value to use with instances that are defined relative to the local wiki.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:23
count()
Get the number of rows in a result object.