MediaWiki  master
CentralIdLookup.php
Go to the documentation of this file.
1 <?php
27 
35 abstract class CentralIdLookup implements IDBAccessObject {
36  // Audience options for accessors
37  public const AUDIENCE_PUBLIC = 1;
38  public const AUDIENCE_RAW = 2;
39 
41  private $providerId;
42 
45 
52  public static function factory( $providerId = null ) {
53  wfDeprecated( __METHOD__, '1.37' );
54  try {
55  return MediaWikiServices::getInstance()
56  ->getCentralIdLookupFactory()
57  ->getLookup( $providerId );
58  } catch ( Throwable $unused ) {
59  return null;
60  }
61  }
62 
77  public static function factoryNonLocal(): ?self {
78  wfDeprecated( __METHOD__, '1.37' );
79  return MediaWikiServices::getInstance()
80  ->getCentralIdLookupFactory()
81  ->getNonLocalLookup();
82  }
83 
91  public function init(
92  string $providerId,
94  ) {
95  if ( $this->providerId !== null ) {
96  throw new LogicException( "CentralIdProvider $providerId already initialized" );
97  }
98  $this->providerId = $providerId;
99  $this->userIdentityLookup = $userIdentityLookup;
100  }
101 
107  public function getProviderId(): string {
108  return $this->providerId;
109  }
110 
117  protected function checkAudience( $audience ): ?Authority {
118  if ( $audience instanceof Authority ) {
119  return $audience;
120  }
121  if ( $audience === self::AUDIENCE_PUBLIC ) {
122  // TODO: when available, inject AuthorityFactory
123  // via init and use it to create anon authority
124  return new User;
125  }
126  if ( $audience === self::AUDIENCE_RAW ) {
127  return null;
128  }
129  throw new InvalidArgumentException( 'Invalid audience' );
130  }
131 
143  abstract public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool;
144 
156  abstract public function lookupCentralIds(
157  array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
158  ): array;
159 
171  abstract public function lookupUserNames(
172  array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
173  ): array;
174 
185  public function nameFromCentralId(
186  $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
187  ): ?string {
188  $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
189  return $idToName[$id];
190  }
191 
200  public function namesFromCentralIds(
201  array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
202  ): array {
203  $idToName = array_fill_keys( $ids, false );
204  $names = $this->lookupCentralIds( $idToName, $audience, $flags );
205  $names = array_unique( $names );
206  $names = array_filter( $names, static function ( $name ) {
207  return $name !== false && $name !== '';
208  } );
209 
210  return array_values( $names );
211  }
212 
223  public function centralIdFromName(
224  $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
225  ): int {
226  $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
227  return $nameToId[$name];
228  }
229 
238  public function centralIdsFromNames(
239  array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
240  ): array {
241  $nameToId = array_fill_keys( $names, false );
242  $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
243  $ids = array_unique( $ids );
244  $ids = array_filter( $ids, static function ( $id ) {
245  return $id !== false;
246  } );
247 
248  return array_values( $ids );
249  }
250 
263  public function localUserFromCentralId(
264  $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
265  ): ?UserIdentity {
266  $name = $this->nameFromCentralId( $id, $audience, $flags );
267  if ( !$name ) {
268  return null;
269  }
270  $user = $this->userIdentityLookup->getUserIdentityByName( $name );
271  if ( $user && $user->isRegistered() && $this->isAttached( $user ) ) {
272  return $user;
273  }
274  return null;
275  }
276 
289  public function centralIdFromLocalUser(
290  UserIdentity $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
291  ): int {
292  return $this->isAttached( $user )
293  ? $this->centralIdFromName( $user->getName(), $audience, $flags )
294  : 0;
295  }
296 
297 }
CentralIdLookup\init
init(string $providerId, UserIdentityLookup $userIdentityLookup)
Initialize the provider.
Definition: CentralIdLookup.php:91
CentralIdLookup\namesFromCentralIds
namesFromCentralIds(array $ids, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a an array of central user IDs, return the (local) user names.
Definition: CentralIdLookup.php:200
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
CentralIdLookup\nameFromCentralId
nameFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return the (local) user name.
Definition: CentralIdLookup.php:185
CentralIdLookup\$userIdentityLookup
UserIdentityLookup $userIdentityLookup
Definition: CentralIdLookup.php:44
IDBAccessObject
Interface for database access objects.
Definition: IDBAccessObject.php:57
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
MediaWiki\User\UserIdentity\isRegistered
isRegistered()
MediaWiki\User\UserIdentity\getName
getName()
CentralIdLookup\centralIdsFromNames
centralIdsFromNames(array $names, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given an array of (local) user names, return the central IDs.
Definition: CentralIdLookup.php:238
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
MediaWiki\User\UserIdentityLookup
Definition: UserIdentityLookup.php:33
CentralIdLookup\AUDIENCE_PUBLIC
const AUDIENCE_PUBLIC
Definition: CentralIdLookup.php:37
CentralIdLookup\centralIdFromLocalUser
centralIdFromLocalUser(UserIdentity $user, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a local UserIdentity object, return the central ID.
Definition: CentralIdLookup.php:289
CentralIdLookup\lookupCentralIds
lookupCentralIds(array $idToName, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given central user IDs, return the (local) user names.
CentralIdLookup\centralIdFromName
centralIdFromName( $name, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a (local) user name, return the central ID.
Definition: CentralIdLookup.php:223
CentralIdLookup\$providerId
string $providerId
Definition: CentralIdLookup.php:41
CentralIdLookup\lookupUserNames
lookupUserNames(array $nameToId, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given (local) user names, return the central IDs.
CentralIdLookup
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Definition: CentralIdLookup.php:35
CentralIdLookup\isAttached
isAttached(UserIdentity $user, $wikiId=UserIdentity::LOCAL)
Check that a user is attached on the specified wiki.
CentralIdLookup\getProviderId
getProviderId()
Get the provider id.
Definition: CentralIdLookup.php:107
CentralIdLookup\checkAudience
checkAudience( $audience)
Check that the "audience" parameter is valid.
Definition: CentralIdLookup.php:117
CentralIdLookup\AUDIENCE_RAW
const AUDIENCE_RAW
Definition: CentralIdLookup.php:38
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:52
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
CentralIdLookup\factoryNonLocal
static factoryNonLocal()
Returns a CentralIdLookup that is guaranteed to be non-local.
Definition: CentralIdLookup.php:77
CentralIdLookup\localUserFromCentralId
localUserFromCentralId( $id, $audience=self::AUDIENCE_PUBLIC, $flags=self::READ_NORMAL)
Given a central user ID, return a local user object.
Definition: CentralIdLookup.php:263