| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\User\TempUser; |
| 4 |
|
| 5 |
use MediaWiki\Auth\AuthManager; |
| 6 |
use MediaWiki\Auth\Throttler; |
| 7 |
use MediaWiki\Permissions\Authority; |
| 8 |
use MediaWiki\Registration\ExtensionRegistry; |
| 9 |
use MediaWiki\Request\WebRequest; |
| 10 |
use MediaWiki\Session\Session; |
| 11 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 12 |
use MediaWiki\User\UserFactory; |
| 13 |
use MediaWiki\User\UserRigorOptions; |
| 14 |
use MediaWiki\Utils\MWTimestamp; |
| 15 |
use UnexpectedValueException; |
| 16 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 17 |
use Wikimedia\Rdbms\IExpression; |
| 18 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
class TempUserCreator implements TempUserConfig { |
| 31 |
private RealTempUserConfig $config; |
| 32 |
private UserFactory $userFactory; |
| 33 |
private AuthManager $authManager; |
| 34 |
private CentralIdLookup $centralIdLookup; |
| 35 |
private Throttler $tempAccountCreationThrottler; |
| 36 |
private Throttler $tempAccountNameAcquisitionThrottler; |
| 37 |
private array $serialProviderConfig; |
| 38 |
private array $serialMappingConfig; |
| 39 |
private ObjectFactory $objectFactory; |
| 40 |
private ?SerialProvider $serialProvider; |
| 41 |
private ?SerialMapping $serialMapping; |
| 42 |
|
| 43 |
|
| 44 |
private const SERIAL_PROVIDERS = [ |
| 45 |
'local' => [ |
| 46 |
'class' => LocalSerialProvider::class, |
| 47 |
'services' => [ 'DBLoadBalancer' ], |
| 48 |
] |
| 49 |
]; |
| 50 |
|
| 51 |
|
| 52 |
private const SERIAL_MAPPINGS = [ |
| 53 |
'plain-numeric' => [ |
| 54 |
'class' => PlainNumericSerialMapping::class, |
| 55 |
], |
| 56 |
'localized-numeric' => [ |
| 57 |
'class' => LocalizedNumericSerialMapping::class, |
| 58 |
'services' => [ 'LanguageFactory' ], |
| 59 |
], |
| 60 |
'filtered-radix' => [ |
| 61 |
'class' => FilteredRadixSerialMapping::class, |
| 62 |
], |
| 63 |
'scramble' => [ |
| 64 |
'class' => ScrambleMapping::class, |
| 65 |
] |
| 66 |
]; |
| 67 |
|
| 68 |
public function __construct( |
| 69 |
RealTempUserConfig $config, |
| 70 |
ObjectFactory $objectFactory, |
| 71 |
UserFactory $userFactory, |
| 72 |
AuthManager $authManager, |
| 73 |
CentralIdLookup $centralIdLookup, |
| 74 |
Throttler $tempAccountCreationThrottler, |
| 75 |
Throttler $tempAccountNameAcquisitionThrottler |
| 76 |
) { |
| 77 |
$this->config = $config; |
| 78 |
$this->objectFactory = $objectFactory; |
| 79 |
$this->userFactory = $userFactory; |
| 80 |
$this->authManager = $authManager; |
| 81 |
$this->centralIdLookup = $centralIdLookup; |
| 82 |
$this->tempAccountCreationThrottler = $tempAccountCreationThrottler; |
| 83 |
$this->tempAccountNameAcquisitionThrottler = $tempAccountNameAcquisitionThrottler; |
| 84 |
$this->serialProviderConfig = $config->getSerialProviderConfig(); |
| 85 |
$this->serialMappingConfig = $config->getSerialMappingConfig(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function create( ?string $name, WebRequest $request ): CreateStatus { |
| 96 |
$status = new CreateStatus; |
| 97 |
|
| 98 |
|
| 99 |
if ( $name === null ) { |
| 100 |
$name = $this->acquireName( $request->getIP() ); |
| 101 |
if ( $name === null ) { |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
return CreateStatus::newFatal( 'temp-user-unable-to-acquire' ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
$result = $this->tempAccountCreationThrottler->increase( |
| 115 |
null, $request->getIP(), 'TempUserCreator' ); |
| 116 |
if ( $result ) { |
| 117 |
|
| 118 |
$message = wfMessage( 'acct_creation_throttle_hit' )->params( $result['count'] ) |
| 119 |
->durationParams( $result['wait'] ); |
| 120 |
$status->fatal( $message ); |
| 121 |
return $status; |
| 122 |
} |
| 123 |
|
| 124 |
$createStatus = $this->attemptAutoCreate( $name ); |
| 125 |
|
| 126 |
if ( $createStatus->isOK() ) { |
| 127 |
|
| 128 |
|
| 129 |
$this->attemptAutoCreate( $name, true ); |
| 130 |
} |
| 131 |
return $createStatus; |
| 132 |
} |
| 133 |
|
| 134 |
public function isEnabled() { |
| 135 |
return $this->config->isEnabled(); |
| 136 |
} |
| 137 |
|
| 138 |
public function isKnown() { |
| 139 |
return $this->config->isKnown(); |
| 140 |
} |
| 141 |
|
| 142 |
public function isAutoCreateAction( string $action ) { |
| 143 |
return $this->config->isAutoCreateAction( $action ); |
| 144 |
} |
| 145 |
|
| 146 |
public function shouldAutoCreate( Authority $authority, string $action ) { |
| 147 |
return $this->config->shouldAutoCreate( $authority, $action ); |
| 148 |
} |
| 149 |
|
| 150 |
public function isTempName( string $name ) { |
| 151 |
return $this->config->isTempName( $name ); |
| 152 |
} |
| 153 |
|
| 154 |
public function isReservedName( string $name ) { |
| 155 |
return $this->config->isReservedName( $name ); |
| 156 |
} |
| 157 |
|
| 158 |
public function getPlaceholderName(): string { |
| 159 |
return $this->config->getPlaceholderName(); |
| 160 |
} |
| 161 |
|
| 162 |
public function getMatchPattern(): Pattern { |
| 163 |
return $this->config->getMatchPattern(); |
| 164 |
} |
| 165 |
|
| 166 |
public function getMatchPatterns(): array { |
| 167 |
return $this->config->getMatchPatterns(); |
| 168 |
} |
| 169 |
|
| 170 |
public function getMatchCondition( IReadableDatabase $db, string $field, string $op ): IExpression { |
| 171 |
return $this->config->getMatchCondition( $db, $field, $op ); |
| 172 |
} |
| 173 |
|
| 174 |
public function getExpireAfterDays(): ?int { |
| 175 |
return $this->config->getExpireAfterDays(); |
| 176 |
} |
| 177 |
|
| 178 |
public function getNotifyBeforeExpirationDays(): ?int { |
| 179 |
return $this->config->getNotifyBeforeExpirationDays(); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
private function attemptAutoCreate( string $name, bool $login = false ): CreateStatus { |
| 192 |
$createStatus = new CreateStatus; |
| 193 |
|
| 194 |
$user = $this->userFactory->newFromName( $name, UserRigorOptions::RIGOR_USABLE ); |
| 195 |
if ( !$user ) { |
| 196 |
$createStatus->fatal( 'internalerror_info', |
| 197 |
'Unable to create user with automatically generated name' ); |
| 198 |
return $createStatus; |
| 199 |
} |
| 200 |
$status = $this->authManager->autoCreateUser( $user, AuthManager::AUTOCREATE_SOURCE_TEMP, $login ); |
| 201 |
$createStatus->merge( $status ); |
| 202 |
|
| 203 |
|
| 204 |
if ( $createStatus->hasMessage( 'userexists' ) ) { |
| 205 |
$createStatus->fatal( 'temp-user-unable-to-acquire' ); |
| 206 |
} |
| 207 |
if ( $createStatus->isOK() ) { |
| 208 |
$createStatus->value = $user; |
| 209 |
} |
| 210 |
return $createStatus; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
private function acquireName( string $ip ): ?string { |
| 222 |
if ( $this->tempAccountNameAcquisitionThrottler->increase( |
| 223 |
null, $ip, 'TempUserCreator' |
| 224 |
) ) { |
| 225 |
return null; |
| 226 |
} |
| 227 |
$year = null; |
| 228 |
if ( $this->serialProviderConfig['useYear'] ?? false ) { |
| 229 |
$year = MWTimestamp::getInstance()->format( 'Y' ); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
$index = $this->getSerialProvider()->acquireIndex( (int)$year ); |
| 234 |
$serialId = $this->getSerialMapping()->getSerialIdForIndex( $index ); |
| 235 |
$username = $this->config->getGeneratorPattern()->generate( $serialId, $year ); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
$centralId = $this->centralIdLookup->centralIdFromName( |
| 242 |
$username, |
| 243 |
CentralIdLookup::AUDIENCE_RAW |
| 244 |
); |
| 245 |
if ( !$centralId ) { |
| 246 |
|
| 247 |
return $username; |
| 248 |
} |
| 249 |
return null; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private function getSerialProvider(): SerialProvider { |
| 257 |
if ( !isset( $this->serialProvider ) ) { |
| 258 |
$this->serialProvider = $this->createSerialProvider(); |
| 259 |
} |
| 260 |
return $this->serialProvider; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
private function createSerialProvider(): SerialProvider { |
| 268 |
$type = $this->serialProviderConfig['type']; |
| 269 |
if ( isset( self::SERIAL_PROVIDERS[$type] ) ) { |
| 270 |
$spec = self::SERIAL_PROVIDERS[$type]; |
| 271 |
} else { |
| 272 |
$extensionProviders = ExtensionRegistry::getInstance() |
| 273 |
->getAttribute( 'TempUserSerialProviders' ); |
| 274 |
if ( isset( $extensionProviders[$type] ) ) { |
| 275 |
$spec = $extensionProviders[$type]; |
| 276 |
} else { |
| 277 |
throw new UnexpectedValueException( __CLASS__ . ": unknown serial provider \"$type\"" ); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
return $this->objectFactory->createObject( |
| 284 |
$spec, |
| 285 |
[ |
| 286 |
'assertClass' => SerialProvider::class, |
| 287 |
'extraArgs' => [ $this->serialProviderConfig ] |
| 288 |
] |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private function getSerialMapping(): SerialMapping { |
| 297 |
if ( !isset( $this->serialMapping ) ) { |
| 298 |
$this->serialMapping = $this->createSerialMapping(); |
| 299 |
} |
| 300 |
return $this->serialMapping; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
private function createSerialMapping(): SerialMapping { |
| 308 |
$type = $this->serialMappingConfig['type']; |
| 309 |
if ( isset( self::SERIAL_MAPPINGS[$type] ) ) { |
| 310 |
$spec = self::SERIAL_MAPPINGS[$type]; |
| 311 |
} else { |
| 312 |
$extensionMappings = ExtensionRegistry::getInstance() |
| 313 |
->getAttribute( 'TempUserSerialMappings' ); |
| 314 |
if ( isset( $extensionMappings[$type] ) ) { |
| 315 |
$spec = $extensionMappings[$type]; |
| 316 |
} else { |
| 317 |
throw new UnexpectedValueException( __CLASS__ . ": unknown serial mapping \"$type\"" ); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
return $this->objectFactory->createObject( |
| 323 |
$spec, |
| 324 |
[ |
| 325 |
'assertClass' => SerialMapping::class, |
| 326 |
'extraArgs' => [ $this->serialMappingConfig ] |
| 327 |
] |
| 328 |
); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public function acquireAndStashName( Session $session ) { |
| 342 |
$name = $session->get( 'TempUser:name' ); |
| 343 |
if ( $name !== null ) { |
| 344 |
return $name; |
| 345 |
} |
| 346 |
$name = $this->acquireName( $session->getRequest()->getIP() ); |
| 347 |
if ( $name !== null ) { |
| 348 |
$session->set( 'TempUser:name', $name ); |
| 349 |
$session->save(); |
| 350 |
} |
| 351 |
return $name; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
public function getStashedName( Session $session ): ?string { |
| 366 |
return $session->get( 'TempUser:name' ); |
| 367 |
} |
| 368 |
} |