| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
class CentralIdLookupFactory { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public const CONSTRUCTOR_OPTIONS = [ |
| 40 |
MainConfigNames::CentralIdLookupProviders, |
| 41 |
MainConfigNames::CentralIdLookupProvider, |
| 42 |
]; |
| 43 |
|
| 44 |
|
| 45 |
private $providers; |
| 46 |
|
| 47 |
|
| 48 |
private $defaultProvider; |
| 49 |
|
| 50 |
|
| 51 |
private $objectFactory; |
| 52 |
|
| 53 |
|
| 54 |
private $userIdentityLookup; |
| 55 |
private UserFactory $userFactory; |
| 56 |
|
| 57 |
|
| 58 |
private $instanceCache = []; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function getProviderIds(): array { |
| 85 |
return array_keys( $this->providers ); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function getDefaultProviderId(): string { |
| 94 |
return $this->defaultProvider; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function getNonLocalLookup( ?string $providerID = null ): ?CentralIdLookup { |
| 138 |
$centralIdLookup = $this->getLookup( $providerID ); |
| 139 |
if ( $centralIdLookup instanceof LocalIdLookup ) { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
return null; |
| 147 |
} |
| 148 |
return $centralIdLookup; |
| 149 |
} |
| 150 |
} |