3 * A central user id lookup service
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.
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.
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
24 * The CentralIdLookup service allows for connecting local users with
29 abstract class CentralIdLookup implements IDBAccessObject {
30 // Audience options for accessors
31 const AUDIENCE_PUBLIC = 1;
32 const AUDIENCE_RAW = 2;
34 /** @var CentralIdLookup[] */
35 private static $instances = [];
41 * Fetch a CentralIdLookup
42 * @param string|null $providerId Provider ID from $wgCentralIdLookupProviders
43 * @return CentralIdLookup|null
45 public static function factory( $providerId = null ) {
46 global $wgCentralIdLookupProviders, $wgCentralIdLookupProvider;
48 if ( $providerId === null ) {
49 $providerId = $wgCentralIdLookupProvider;
52 if ( !array_key_exists( $providerId, self::$instances ) ) {
53 self::$instances[$providerId] = null;
55 if ( isset( $wgCentralIdLookupProviders[$providerId] ) ) {
56 $provider = ObjectFactory::getObjectFromSpec( $wgCentralIdLookupProviders[$providerId] );
57 if ( $provider instanceof CentralIdLookup ) {
58 $provider->providerId = $providerId;
59 self::$instances[$providerId] = $provider;
64 return self::$instances[$providerId];
68 * Reset internal cache for unit testing
70 public static function resetCache() {
71 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
72 throw new MWException( __METHOD__ . ' may only be called from unit tests!' );
74 self::$instances = [];
77 final public function getProviderId() {
78 return $this->providerId;
82 * Check that the "audience" parameter is valid
83 * @param int|User $audience One of the audience constants, or a specific user
84 * @return User|null User to check against, or null if no checks are needed
85 * @throws InvalidArgumentException
87 protected function checkAudience( $audience ) {
88 if ( $audience instanceof User ) {
91 if ( $audience === self::AUDIENCE_PUBLIC ) {
94 if ( $audience === self::AUDIENCE_RAW ) {
97 throw new InvalidArgumentException( 'Invalid audience' );
101 * Check that a User is attached on the specified wiki.
103 * If unattached local accounts don't exist in your extension, this comes
104 * down to a check whether the central account exists at all and that
105 * $wikiId is using the same central database.
108 * @param string|null $wikiId Wiki to check attachment status. If null, check the current wiki.
111 abstract public function isAttached( User $user, $wikiId = null );
114 * Given central user IDs, return the (local) user names
115 * @note There's no requirement that the user names actually exist locally,
116 * or if they do that they're actually attached to the central account.
117 * @param array $idToName Array with keys being central user IDs
118 * @param int|User $audience One of the audience constants, or a specific user
119 * @param int $flags IDBAccessObject read flags
120 * @return array Copy of $idToName with values set to user names (or
121 * empty-string if the user exists but $audience lacks the rights needed
122 * to see it). IDs not corresponding to a user are unchanged.
124 abstract public function lookupCentralIds(
125 array $idToName, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
129 * Given (local) user names, return the central IDs
130 * @note There's no requirement that the user names actually exist locally,
131 * or if they do that they're actually attached to the central account.
132 * @param array $nameToId Array with keys being canonicalized user names
133 * @param int|User $audience One of the audience constants, or a specific user
134 * @param int $flags IDBAccessObject read flags
135 * @return array Copy of $nameToId with values set to central IDs.
136 * Names not corresponding to a user (or $audience lacks the rights needed
137 * to see it) are unchanged.
139 abstract public function lookupUserNames(
140 array $nameToId, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
144 * Given a central user ID, return the (local) user name
145 * @note There's no requirement that the user name actually exists locally,
146 * or if it does that it's actually attached to the central account.
147 * @param int $id Central user ID
148 * @param int|User $audience One of the audience constants, or a specific user
149 * @param int $flags IDBAccessObject read flags
150 * @return string|null User name, or empty string if $audience lacks the
151 * rights needed to see it, or null if $id doesn't correspond to a user
153 public function nameFromCentralId(
154 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
156 $idToName = $this->lookupCentralIds( [ $id => null ], $audience, $flags );
157 return $idToName[$id];
161 * Given a an array of central user IDs, return the (local) user names.
162 * @param int[] $ids Central user IDs
163 * @param int|User $audience One of the audience constants, or a specific user
164 * @param int $flags IDBAccessObject read flags
165 * @return string[] User names
168 public function namesFromCentralIds(
169 array $ids, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
171 $idToName = array_fill_keys( $ids, false );
172 $names = $this->lookupCentralIds( $idToName, $audience, $flags );
173 $names = array_unique( $names );
174 $names = array_filter( $names, function ( $name ) {
175 return $name !== false && $name !== '';
178 return array_values( $names );
182 * Given a (local) user name, return the central ID
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 string $name Canonicalized user name
186 * @param int|User $audience One of the audience constants, or a specific user
187 * @param int $flags IDBAccessObject read flags
188 * @return int User ID; 0 if the name does not correspond to a user or
189 * $audience lacks the rights needed to see it.
191 public function centralIdFromName(
192 $name, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
194 $nameToId = $this->lookupUserNames( [ $name => 0 ], $audience, $flags );
195 return $nameToId[$name];
199 * Given an array of (local) user names, return the central IDs.
200 * @param string[] $names Canonicalized user names
201 * @param int|User $audience One of the audience constants, or a specific user
202 * @param int $flags IDBAccessObject read flags
203 * @return int[] User IDs
206 public function centralIdsFromNames(
207 array $names, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
209 $nameToId = array_fill_keys( $names, false );
210 $ids = $this->lookupUserNames( $nameToId, $audience, $flags );
211 $ids = array_unique( $ids );
212 $ids = array_filter( $ids, function ( $id ) {
213 return $id !== false;
216 return array_values( $ids );
220 * Given a central user ID, return a local User object
221 * @note Unlike nameFromCentralId(), this does guarantee that the local
222 * user exists and is attached to the central account.
223 * @param int $id Central user ID
224 * @param int|User $audience One of the audience constants, or a specific user
225 * @param int $flags IDBAccessObject read flags
226 * @return User|null Local user, or null if: $id doesn't correspond to a
227 * user, $audience lacks the rights needed to see the user, the user
228 * doesn't exist locally, or the user isn't locally attached.
230 public function localUserFromCentralId(
231 $id, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
233 $name = $this->nameFromCentralId( $id, $audience, $flags );
234 if ( $name !== null && $name !== '' ) {
235 $user = User::newFromName( $name );
236 if ( $user && $user->getId() && $this->isAttached( $user ) ) {
244 * Given a local User object, return the central ID
245 * @note Unlike centralIdFromName(), this does guarantee that the local
246 * user is attached to the central account.
247 * @param User $user Local user
248 * @param int|User $audience One of the audience constants, or a specific user
249 * @param int $flags IDBAccessObject read flags
250 * @return int User ID; 0 if the local user does not correspond to a
251 * central user, $audience lacks the rights needed to see it, or the
252 * central user isn't locally attached.
254 public function centralIdFromLocalUser(
255 User $user, $audience = self::AUDIENCE_PUBLIC, $flags = self::READ_NORMAL
257 return $this->isAttached( $user )
258 ? $this->centralIdFromName( $user->getName(), $audience, $flags )