Debian Codegraph @

includes/user/CentralId/CentralIdLookup.php

raw symbols SCIP
1 <?php
2 /**
3 * A central user id lookup service
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\User\CentralId;
24
25 use InvalidArgumentException;
26 use LogicException;
27 use MediaWiki\MediaWikiServices;
28 use MediaWiki\Permissions\Authority;
29 use MediaWiki\User\UserFactory;
30 use MediaWiki\User\UserIdentity;
31 use MediaWiki\User\UserIdentityLookup;
32 use Throwable;
33 use Wikimedia\Rdbms\IDBAccessObject;
34
35 /**
36 * The CentralIdLookup service allows for connecting local users with
37 * cluster-wide IDs.
38 *
39 * @since 1.27
40 * @stable to extend
41 */
42 abstract class CentralIdLookup {
43 // Audience options for accessors
44 public const AUDIENCE_PUBLIC = 1;
45 public const AUDIENCE_RAW = 2;
46
47 /** @var string */
48 private $providerId;
49
50 /** @var UserIdentityLookup */
51 private $userIdentityLookup;
52 private UserFactory $userFactory;
53
54 /**
55 * Fetch a CentralIdLookup
56 * @deprecated since 1.37 Use MediaWikiServices to obtain an instance.
57 * @param string|null $providerId Provider ID from $wgCentralIdLookupProviders
58 * @return CentralIdLookup|null
59 */
60 public static function factory( $providerId = null ) {
61 wfDeprecated( __METHOD__, '1.37' );
62 try {
63 return MediaWikiServices::getInstance()
64 ->getCentralIdLookupFactory()
65 ->getLookup( $providerId );
66 } catch ( Throwable $unused ) {
67 return null;
68 }
69 }
70
71 /**
72 * Initialize the provider.
73 *
74 * @param string $providerId
75 * @param UserIdentityLookup $userIdentityLookup
76 * @internal
77 */
78 public function init(
79 string $providerId,
80 UserIdentityLookup $userIdentityLookup,
81 UserFactory $userFactory
82 ) {
83 if ( $this->providerId !== null ) {
84 throw new LogicException( "CentralIdProvider $providerId already initialized" );
85 }
86 $this->providerId = $providerId;
87 $this->userIdentityLookup = $userIdentityLookup;
88 $this->userFactory = $userFactory;
89 }
90
91 /**
92 * Get the provider id.
93 *
94 * @return string
95 */
96 public function getProviderId(): string {
97 return $this->providerId;
98 }
99
100 /**
101 * Check that the "audience" parameter is valid
102 * @param int|Authority $audience One of the audience constants, or a specific authority
103 * @return Authority|null authority to check against, or null if no checks are needed
104 * @throws InvalidArgumentException
105 */
106 protected function checkAudience( $audience ): ?Authority {
107 if ( $audience instanceof Authority ) {
108 return $audience;
109 }
110 if ( $audience === self::AUDIENCE_PUBLIC ) {
111 // TODO: when available, inject AuthorityFactory
112 // via init and use it to create anon authority
113 return $this->userFactory->newAnonymous();
114 }
115 if ( $audience === self::AUDIENCE_RAW ) {
116 return null;
117 }
118 throw new InvalidArgumentException( 'Invalid audience' );
119 }
120
121 /**
122 * Check that a user is attached on the specified wiki.
123 *
124 * If unattached local accounts don't exist in your extension, this comes
125 * down to a check whether the central account exists at all and that
126 * $wikiId is using the same central database.
127 *
128 * @param UserIdentity $user
129 * @param string|false $wikiId Wiki to check attachment status. If false, check the current wiki.
130 * @return bool
131 */
132 abstract public function isAttached( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool;
133
134 /**
135 * Check that a username is owned by the central user on the specified wiki.
136 *
137 * This should return true if the local account exists and is attached (see isAttached()),
138 * or if it does not exist but is reserved for the central user (it's guaranteed that
139 * if it's ever created, then it will be attached to the central user).
140 *
141 * @since 1.43
142 * @stable to override
143 * @param UserIdentity $user
144 * @param string|false $wikiId Wiki to check attachment status. If false, check the current wiki.
145 * @return bool
146 */
147 public function isOwned( UserIdentity $user, $wikiId = UserIdentity::LOCAL ): bool {
148 return $this->isAttached( $user, $wikiId );
149 }
150
151 /**
152 * Given central user IDs, return the (local) user names
153 * @note There's no requirement that the user names actually exist locally,
154 * or if they do that they're actually attached to the central account.
155 * @param array $idToName Array with keys being central user IDs
156 * @param int|Authority $audience One of the audience constants, or a specific authority
157 * @param int $flags IDBAccessObject read flags
158 * @return string[] Copy of $idToName with values set to user names (or
159 * empty-string if the user exists but $audience lacks the rights needed
160 * to see it). IDs not corresponding to a user are unchanged.
161 */
162 abstract public function lookupCentralIds(
163 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
164 ): array;
165
166 /**
167 * Given (local) user names, return the central IDs
168 * @note There's no requirement that the user names actually exist locally,
169 * or if they do that they're actually attached to the central account.
170 * @param array $nameToId Array with keys being canonicalized user names
171 * @param int|Authority $audience One of the audience constants, or a specific authority
172 * @param int $flags IDBAccessObject read flags
173 * @return int[] Copy of $nameToId with values set to central IDs.
174 * Names not corresponding to a user (or $audience lacks the rights needed
175 * to see it) are unchanged.
176 */
177 abstract public function lookupUserNames(
178 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
179 ): array;
180
181 /**
182 * Given a central user ID, return the (local) user name
183 * @note There's no requirement that the user name actually exists locally,
184 * or if it does that it's actually attached to the central account.
185 * @param int $id Central user ID
186 * @param int|Authority $audience One of the audience constants, or a specific authority
187 * @param int $flags IDBAccessObject read flags
188 * @return string|null user name, or empty string if $audience lacks the
189 * rights needed to see it, or null if $id doesn't correspond to a user
190 */
191 public function nameFromCentralId(
192 $id, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
193 ): ?string {
194 $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
195 return $idToName[$id];
196 }
197
198 /**
199 * Given a an array of central user IDs, return the (local) user names.
200 * @param int[] $ids Central user IDs
201 * @param int|Authority $audience One of the audience constants, or a specific authority
202 * @param int $flags IDBAccessObject read flags
203 * @return string[] user names
204 * @since 1.30
205 */
206 public function namesFromCentralIds(
207 array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
208 ): array {
209 $idToName = array_fill_keys( $ids, false );
210 $names = $this->lookupCentralIds( $idToName, $audience, $flags );
211 $names = array_unique( $names );
212 $names = array_filter( $names, static function ( $name ) {
213 return $name !== false && $name !== '';
214 } );
215
216 return array_values( $names );
217 }
218
219 /**
220 * Given a (local) user name, return the central ID
221 * @note There's no requirement that the user name actually exists locally,
222 * or if it does that it's actually attached to the central account.
223 * @param string $name Canonicalized user name
224 * @param int|Authority $audience One of the audience constants, or a specific authority
225 * @param int $flags IDBAccessObject read flags
226 * @return int user ID; 0 if the name does not correspond to a user or
227 * $audience lacks the rights needed to see it.
228 */
229 public function centralIdFromName(
230 $name, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
231 ): int {
232 $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
233 return $nameToId[$name];
234 }
235
236 /**
237 * Given an array of (local) user names, return the central IDs.
238 * @param string[] $names Canonicalized user names
239 * @param int|Authority $audience One of the audience constants, or a specific authority
240 * @param int $flags IDBAccessObject read flags
241 * @return int[] user IDs
242 * @since 1.30
243 */
244 public function centralIdsFromNames(
245 array $names, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
246 ): array {
247 $nameToId = array_fill_keys( $names, false );
248 $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
249 $ids = array_unique( $ids );
250 $ids = array_filter( $ids, static function ( $id ) {
251 return $id !== false;
252 } );
253
254 return array_values( $ids );
255 }
256
257 /**
258 * Given a central user ID, return a local user object
259 * @note Unlike nameFromCentralId(), this does guarantee that the local
260 * user exists and is attached to the central account.
261 * @stable to override
262 * @param int $id Central user ID
263 * @param int|Authority $audience One of the audience constants, or a specific authority
264 * @param int $flags IDBAccessObject read flags
265 * @return UserIdentity|null Local user, or null if: $id doesn't correspond to a
266 * user, $audience lacks the rights needed to see the user, the user
267 * doesn't exist locally, or the user isn't locally attached.
268 */
269 public function localUserFromCentralId(
270 $id, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
271 ): ?UserIdentity {
272 $name = $this->nameFromCentralId( $id, $audience, $flags );
273 if ( !$name ) {
274 return null;
275 }
276 $user = $this->userIdentityLookup->getUserIdentityByName( $name );
277 if ( $user && $user->isRegistered() && $this->isAttached( $user ) ) {
278 return $user;
279 }
280 return null;
281 }
282
283 /**
284 * Given a local UserIdentity object, return the central ID
285 * @stable to override
286 * @note Unlike centralIdFromName(), this does guarantee that the local
287 * user is attached to the central account.
288 * @param UserIdentity $user Local user
289 * @param int|Authority $audience One of the audience constants, or a specific authority
290 * @param int $flags IDBAccessObject read flags
291 * @return int user ID; 0 if the local user does not correspond to a
292 * central user, $audience lacks the rights needed to see it, or the
293 * central user isn't locally attached.
294 */
295 public function centralIdFromLocalUser(
296 UserIdentity $user, $audience = self::AUDIENCE_PUBLIC, $flags = IDBAccessObject::READ_NORMAL
297 ): int {
298 return $this->isAttached( $user )
299 ? $this->centralIdFromName( $user->getName(), $audience, $flags )
300 : 0;
301 }
302
303 }
304
305 /** @deprecated class alias since 1.41 */
306 class_alias( CentralIdLookup::class, 'CentralIdLookup' );