Debian Codegraph @

includes/user/CentralId/CentralIdLookupFactory.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\CentralId;
22
23 use InvalidArgumentException;
24 use MediaWiki\Config\ServiceOptions;
25 use MediaWiki\MainConfigNames;
26 use MediaWiki\User\UserFactory;
27 use MediaWiki\User\UserIdentityLookup;
28 use Wikimedia\ObjectFactory\ObjectFactory;
29
30 /**
31 * @since 1.37
32 * @package MediaWiki\User\CentralId
33 */
34 class CentralIdLookupFactory {
35
36 /**
37 * @internal
38 */
39 public const CONSTRUCTOR_OPTIONS = [
40 MainConfigNames::CentralIdLookupProviders,
41 MainConfigNames::CentralIdLookupProvider,
42 ];
43
44 /** @var array ObjectFactory specs indexed by provider name */
45 private $providers;
46
47 /** @var string */
48 private $defaultProvider;
49
50 /** @var ObjectFactory */
51 private $objectFactory;
52
53 /** @var UserIdentityLookup */
54 private $userIdentityLookup;
55 private UserFactory $userFactory;
56
57 /** @var CentralIdLookup[] */
58 private $instanceCache = [];
59
60 /**
61 * @param ServiceOptions $options
62 * @param ObjectFactory $objectFactory
63 * @param UserIdentityLookup $userIdentityLookup
64 */
65 public function __construct(
66 ServiceOptions $options,
67 ObjectFactory $objectFactory,
68 UserIdentityLookup $userIdentityLookup,
69 UserFactory $userFactory
70 ) {
71 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
72 $this->providers = $options->get( MainConfigNames::CentralIdLookupProviders );
73 $this->defaultProvider = $options->get( MainConfigNames::CentralIdLookupProvider );
74 $this->objectFactory = $objectFactory;
75 $this->userIdentityLookup = $userIdentityLookup;
76 $this->userFactory = $userFactory;
77 }
78
79 /**
80 * Get the IDs of the registered central ID lookup providers.
81 *
82 * @return string[]
83 */
84 public function getProviderIds(): array {
85 return array_keys( $this->providers );
86 }
87
88 /**
89 * Get the ID of the default central ID provider.
90 *
91 * @return string
92 */
93 public function getDefaultProviderId(): string {
94 return $this->defaultProvider;
95 }
96
97 /**
98 * Get an instance of a CentralIdLookup.
99 *
100 * @param string|null $providerId Provider ID from $wgCentralIdLookupProviders or null
101 * to use the provider configured in $wgCentralIdLookupProvider
102 * @return CentralIdLookup
103 * @throws InvalidArgumentException if $providerId is not properly configured
104 */
105 public function getLookup( ?string $providerId = null ): CentralIdLookup {
106 $providerId ??= $this->defaultProvider;
107
108 if ( !array_key_exists( $providerId, $this->instanceCache ) ) {
109 $providerSpec = $this->providers[$providerId] ?? null;
110 if ( !$providerSpec ) {
111 throw new InvalidArgumentException( "Invalid central ID provider $providerId" );
112 }
113 $provider = $this->objectFactory->createObject(
114 $providerSpec,
115 [ 'assertClass' => CentralIdLookup::class ]
116 );
117 $provider->init( $providerId, $this->userIdentityLookup, $this->userFactory );
118 $this->instanceCache[$providerId] = $provider;
119 }
120 return $this->instanceCache[$providerId];
121 }
122
123 /**
124 * Returns a CentralIdLookup that is guaranteed to be non-local.
125 * If no such guarantee can be made, returns null.
126 *
127 * If this function returns a non-null CentralIdLookup, that lookup is expected to provide IDs
128 * that are shared with some set of other wikis. However, you should still be cautious
129 * when using those IDs, as they will not necessarily work with *all* other wikis, and it can be
130 * hard to tell if another wiki is in the same set as this one or not.
131 *
132 * @param string|null $providerID Provider ID from $wgCentralIdLookupProviders or null
133 * to use the provider configured in $wgCentralIdLookupProvider
134 * @return ?CentralIdLookup
135 * @throws InvalidArgumentException if $providerId is not properly configured
136 */
137 public function getNonLocalLookup( ?string $providerID = null ): ?CentralIdLookup {
138 $centralIdLookup = $this->getLookup( $providerID );
139 if ( $centralIdLookup instanceof LocalIdLookup ) {
140 /*
141 * A LocalIdLookup (which is the default) may actually be non-local,
142 * if shared user tables are used.
143 * However, we cannot know that here, so play it safe and refuse to return it.
144 * See also T163277 and T170996.
145 */
146 return null;
147 }
148 return $centralIdLookup;
149 }
150 }