| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\User; |
| 24 |
|
| 25 |
use AllowDynamicProperties; |
| 26 |
use ArrayIterator; |
| 27 |
use BadMethodCallException; |
| 28 |
use InvalidArgumentException; |
| 29 |
use MailAddress; |
| 30 |
use MediaWiki\Auth\AuthenticationRequest; |
| 31 |
use MediaWiki\Auth\AuthManager; |
| 32 |
use MediaWiki\Block\AbstractBlock; |
| 33 |
use MediaWiki\Block\Block; |
| 34 |
use MediaWiki\Block\SystemBlock; |
| 35 |
use MediaWiki\Context\RequestContext; |
| 36 |
use MediaWiki\DAO\WikiAwareEntityTrait; |
| 37 |
use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
| 38 |
use MediaWiki\Logger\LoggerFactory; |
| 39 |
use MediaWiki\Mail\UserEmailContact; |
| 40 |
use MediaWiki\MainConfigNames; |
| 41 |
use MediaWiki\MainConfigSchema; |
| 42 |
use MediaWiki\MediaWikiServices; |
| 43 |
use MediaWiki\Page\PageIdentity; |
| 44 |
use MediaWiki\Parser\Sanitizer; |
| 45 |
use MediaWiki\Password\PasswordFactory; |
| 46 |
use MediaWiki\Password\UserPasswordPolicy; |
| 47 |
use MediaWiki\Permissions\Authority; |
| 48 |
use MediaWiki\Permissions\PermissionStatus; |
| 49 |
use MediaWiki\Permissions\RateLimitSubject; |
| 50 |
use MediaWiki\Permissions\UserAuthority; |
| 51 |
use MediaWiki\Request\WebRequest; |
| 52 |
use MediaWiki\Session\SessionManager; |
| 53 |
use MediaWiki\Session\Token; |
| 54 |
use MediaWiki\Status\Status; |
| 55 |
use MediaWiki\Title\Title; |
| 56 |
use MWCryptHash; |
| 57 |
use MWCryptRand; |
| 58 |
use MWExceptionHandler; |
| 59 |
use RuntimeException; |
| 60 |
use stdClass; |
| 61 |
use Stringable; |
| 62 |
use UnexpectedValueException; |
| 63 |
use Wikimedia\Assert\Assert; |
| 64 |
use Wikimedia\Assert\PreconditionException; |
| 65 |
use Wikimedia\DebugInfo\DebugInfoTrait; |
| 66 |
use Wikimedia\IPUtils; |
| 67 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 68 |
use Wikimedia\Rdbms\Database; |
| 69 |
use Wikimedia\Rdbms\DBAccessObjectUtils; |
| 70 |
use Wikimedia\Rdbms\DBExpectedError; |
| 71 |
use Wikimedia\Rdbms\IDatabase; |
| 72 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 73 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 74 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 75 |
use Wikimedia\ScopedCallback; |
| 76 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
#[AllowDynamicProperties] |
| 94 |
class User implements Stringable, Authority, UserIdentity, UserEmailContact { |
| 95 |
use DebugInfoTrait; |
| 96 |
use ProtectedHookAccessorTrait; |
| 97 |
use WikiAwareEntityTrait; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public const READ_EXCLUSIVE = IDBAccessObject::READ_EXCLUSIVE; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public const READ_LOCKING = IDBAccessObject::READ_LOCKING; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public const TOKEN_LENGTH = 32; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public const INVALID_TOKEN = '*** INVALID ***'; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
private const VERSION = 17; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public const MAINTENANCE_SCRIPT_USER = 'Maintenance script'; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
protected static $mCacheVars = [ |
| 139 |
|
| 140 |
'mId', |
| 141 |
'mName', |
| 142 |
'mRealName', |
| 143 |
'mEmail', |
| 144 |
'mTouched', |
| 145 |
'mToken', |
| 146 |
'mEmailAuthenticated', |
| 147 |
'mEmailToken', |
| 148 |
'mEmailTokenExpires', |
| 149 |
'mRegistration', |
| 150 |
|
| 151 |
'mActorId', |
| 152 |
]; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public $mId; |
| 160 |
|
| 161 |
public $mName; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public $mActorId; |
| 168 |
|
| 169 |
public $mRealName; |
| 170 |
|
| 171 |
|
| 172 |
public $mEmail; |
| 173 |
|
| 174 |
public $mTouched; |
| 175 |
|
| 176 |
protected $mQuickTouched; |
| 177 |
|
| 178 |
protected $mToken; |
| 179 |
|
| 180 |
public $mEmailAuthenticated; |
| 181 |
|
| 182 |
protected $mEmailToken; |
| 183 |
|
| 184 |
protected $mEmailTokenExpires; |
| 185 |
|
| 186 |
protected $mRegistration; |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
protected $mLoadedItems = []; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public $mFrom; |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
protected $mDatePreference; |
| 213 |
|
| 214 |
protected $mHash; |
| 215 |
|
| 216 |
protected $mGlobalBlock; |
| 217 |
|
| 218 |
protected $mLocked; |
| 219 |
|
| 220 |
|
| 221 |
private $mRequest; |
| 222 |
|
| 223 |
|
| 224 |
protected $queryFlagsUsed = IDBAccessObject::READ_NORMAL; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
private $mThisAsAuthority; |
| 231 |
|
| 232 |
|
| 233 |
private $isTemp; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public function __construct() { |
| 251 |
$this->clearInstanceCache( 'defaults' ); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public function getWikiId() { |
| 261 |
return self::LOCAL; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function __toString() { |
| 268 |
return $this->getName(); |
| 269 |
} |
| 270 |
|
| 271 |
public function &__get( $name ) { |
| 272 |
|
| 273 |
if ( $name === 'mRights' ) { |
| 274 |
|
| 275 |
wfDeprecated( 'User::$mRights', '1.34' ); |
| 276 |
$copy = MediaWikiServices::getInstance() |
| 277 |
->getPermissionManager() |
| 278 |
->getUserPermissions( $this ); |
| 279 |
return $copy; |
| 280 |
} elseif ( !property_exists( $this, $name ) ) { |
| 281 |
|
| 282 |
wfLogWarning( 'tried to get non-existent property' ); |
| 283 |
$this->$name = null; |
| 284 |
return $this->$name; |
| 285 |
} else { |
| 286 |
wfLogWarning( 'tried to get non-visible property' ); |
| 287 |
$null = null; |
| 288 |
return $null; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
public function __set( $name, $value ) { |
| 293 |
|
| 294 |
|
| 295 |
if ( $name === 'mRights' ) { |
| 296 |
|
| 297 |
wfDeprecated( 'User::$mRights', '1.34' ); |
| 298 |
MediaWikiServices::getInstance()->getPermissionManager()->overrideUserRightsForTesting( |
| 299 |
$this, |
| 300 |
$value ?? [] |
| 301 |
); |
| 302 |
} elseif ( !property_exists( $this, $name ) ) { |
| 303 |
$this->$name = $value; |
| 304 |
} else { |
| 305 |
wfLogWarning( 'tried to set non-visible property' ); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
public function __sleep(): array { |
| 310 |
return array_diff( |
| 311 |
array_keys( get_object_vars( $this ) ), |
| 312 |
[ |
| 313 |
'mThisAsAuthority' |
| 314 |
] |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public function isSafeToLoad() { |
| 333 |
global $wgFullyInitialised; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
return ( !defined( 'MW_NO_SESSION' ) && $wgFullyInitialised ) || |
| 341 |
$this->mLoadedItems === true || $this->mFrom !== 'session'; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public function load( $flags = IDBAccessObject::READ_NORMAL ) { |
| 350 |
global $wgFullyInitialised; |
| 351 |
|
| 352 |
if ( $this->mLoadedItems === true ) { |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
$oldLoadedItems = $this->mLoadedItems; |
| 358 |
$this->mLoadedItems = true; |
| 359 |
$this->queryFlagsUsed = $flags; |
| 360 |
|
| 361 |
|
| 362 |
if ( !$wgFullyInitialised && $this->mFrom === 'session' ) { |
| 363 |
LoggerFactory::getInstance( 'session' ) |
| 364 |
->warning( 'User::loadFromSession called before the end of Setup.php', [ |
| 365 |
'exception' => new RuntimeException( |
| 366 |
'User::loadFromSession called before the end of Setup.php' |
| 367 |
), |
| 368 |
] ); |
| 369 |
$this->loadDefaults(); |
| 370 |
$this->mLoadedItems = $oldLoadedItems; |
| 371 |
return; |
| 372 |
} elseif ( $this->mFrom === 'session' |
| 373 |
&& defined( 'MW_NO_SESSION' ) && MW_NO_SESSION !== 'warn' |
| 374 |
) { |
| 375 |
|
| 376 |
|
| 377 |
$this->loadDefaults(); |
| 378 |
$this->mLoadedItems = $oldLoadedItems; |
| 379 |
$ep = defined( 'MW_ENTRY_POINT' ) ? MW_ENTRY_POINT : 'this'; |
| 380 |
throw new BadMethodCallException( "Sessions are disabled for $ep entry point" ); |
| 381 |
} |
| 382 |
|
| 383 |
switch ( $this->mFrom ) { |
| 384 |
case 'defaults': |
| 385 |
$this->loadDefaults(); |
| 386 |
break; |
| 387 |
case 'id': |
| 388 |
|
| 389 |
if ( $this->mId != 0 ) { |
| 390 |
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); |
| 391 |
if ( $lb->hasOrMadeRecentPrimaryChanges() ) { |
| 392 |
$flags |= IDBAccessObject::READ_LATEST; |
| 393 |
$this->queryFlagsUsed = $flags; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
$this->loadFromId( $flags ); |
| 398 |
break; |
| 399 |
case 'actor': |
| 400 |
case 'name': |
| 401 |
|
| 402 |
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer(); |
| 403 |
if ( $lb->hasOrMadeRecentPrimaryChanges() ) { |
| 404 |
$flags |= IDBAccessObject::READ_LATEST; |
| 405 |
$this->queryFlagsUsed = $flags; |
| 406 |
} |
| 407 |
|
| 408 |
$dbr = DBAccessObjectUtils::getDBFromRecency( |
| 409 |
MediaWikiServices::getInstance()->getDBLoadBalancerFactory(), |
| 410 |
$flags |
| 411 |
); |
| 412 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 413 |
->select( [ 'actor_id', 'actor_user', 'actor_name' ] ) |
| 414 |
->from( 'actor' ) |
| 415 |
->recency( $flags ); |
| 416 |
if ( $this->mFrom === 'name' ) { |
| 417 |
|
| 418 |
$queryBuilder->where( [ 'actor_name' => IPUtils::sanitizeIP( $this->mName ) ] ); |
| 419 |
} else { |
| 420 |
$queryBuilder->where( [ 'actor_id' => $this->mActorId ] ); |
| 421 |
} |
| 422 |
$row = $queryBuilder->caller( __METHOD__ )->fetchRow(); |
| 423 |
|
| 424 |
if ( !$row ) { |
| 425 |
|
| 426 |
$this->loadDefaults( $this->mFrom === 'name' ? $this->mName : false ); |
| 427 |
} elseif ( $row->actor_user ) { |
| 428 |
$this->mId = $row->actor_user; |
| 429 |
$this->loadFromId( $flags ); |
| 430 |
} else { |
| 431 |
$this->loadDefaults( $row->actor_name, $row->actor_id ); |
| 432 |
} |
| 433 |
break; |
| 434 |
case 'session': |
| 435 |
if ( !$this->loadFromSession() ) { |
| 436 |
|
| 437 |
$this->loadDefaults(); |
| 438 |
} |
| 439 |
$this->getHookRunner()->onUserLoadAfterLoadFromSession( $this ); |
| 440 |
break; |
| 441 |
default: |
| 442 |
throw new UnexpectedValueException( |
| 443 |
"Unrecognised value for User->mFrom: \"{$this->mFrom}\"" ); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
public function loadFromId( $flags = IDBAccessObject::READ_NORMAL ) { |
| 453 |
if ( $this->mId == 0 ) { |
| 454 |
|
| 455 |
$this->loadDefaults(); |
| 456 |
return false; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
$latest = DBAccessObjectUtils::hasFlags( $flags, IDBAccessObject::READ_LATEST ); |
| 462 |
if ( $latest ) { |
| 463 |
if ( !$this->loadFromDatabase( $flags ) ) { |
| 464 |
|
| 465 |
return false; |
| 466 |
} |
| 467 |
} else { |
| 468 |
$this->loadFromCache(); |
| 469 |
} |
| 470 |
|
| 471 |
$this->mLoadedItems = true; |
| 472 |
$this->queryFlagsUsed = $flags; |
| 473 |
|
| 474 |
return true; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public static function purge( $dbDomain, $userId ) { |
| 483 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 484 |
$key = $cache->makeGlobalKey( 'user', 'id', $dbDomain, $userId ); |
| 485 |
$cache->delete( $key ); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
protected function getCacheKey( WANObjectCache $cache ) { |
| 494 |
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); |
| 495 |
|
| 496 |
return $cache->makeGlobalKey( 'user', 'id', |
| 497 |
$lbFactory->getLocalDomainID(), $this->mId ); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
protected function loadFromCache() { |
| 507 |
global $wgFullyInitialised; |
| 508 |
|
| 509 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 510 |
$data = $cache->getWithSetCallback( |
| 511 |
$this->getCacheKey( $cache ), |
| 512 |
$cache::TTL_HOUR, |
| 513 |
function ( $oldValue, &$ttl, array &$setOpts ) use ( $cache, $wgFullyInitialised ) { |
| 514 |
$setOpts += Database::getCacheSetOptions( |
| 515 |
MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase() |
| 516 |
); |
| 517 |
wfDebug( "User: cache miss for user {$this->mId}" ); |
| 518 |
|
| 519 |
$this->loadFromDatabase( IDBAccessObject::READ_NORMAL ); |
| 520 |
|
| 521 |
$data = []; |
| 522 |
foreach ( self::$mCacheVars as $name ) { |
| 523 |
$data[$name] = $this->$name; |
| 524 |
} |
| 525 |
|
| 526 |
$ttl = $cache->adaptiveTTL( |
| 527 |
(int)wfTimestamp( TS_UNIX, $this->mTouched ), |
| 528 |
$ttl |
| 529 |
); |
| 530 |
|
| 531 |
if ( $wgFullyInitialised ) { |
| 532 |
$groupMemberships = MediaWikiServices::getInstance() |
| 533 |
->getUserGroupManager() |
| 534 |
->getUserGroupMemberships( $this, $this->queryFlagsUsed ); |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
foreach ( $groupMemberships as $ugm ) { |
| 539 |
if ( $ugm->getExpiry() ) { |
| 540 |
$secondsUntilExpiry = |
| 541 |
(int)wfTimestamp( TS_UNIX, $ugm->getExpiry() ) - time(); |
| 542 |
|
| 543 |
if ( $secondsUntilExpiry > 0 && $secondsUntilExpiry < $ttl ) { |
| 544 |
$ttl = $secondsUntilExpiry; |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
return $data; |
| 551 |
}, |
| 552 |
[ 'pcTTL' => $cache::TTL_PROC_LONG, 'version' => self::VERSION ] |
| 553 |
); |
| 554 |
|
| 555 |
|
| 556 |
foreach ( self::$mCacheVars as $name ) { |
| 557 |
$this->$name = $data[$name]; |
| 558 |
} |
| 559 |
|
| 560 |
return true; |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
public static function newFromName( $name, $validate = 'valid' ) { |
| 591 |
|
| 592 |
$validationLevels = [ |
| 593 |
'valid' => UserRigorOptions::RIGOR_VALID, |
| 594 |
'usable' => UserRigorOptions::RIGOR_USABLE, |
| 595 |
'creatable' => UserRigorOptions::RIGOR_CREATABLE |
| 596 |
]; |
| 597 |
if ( $validate === true ) { |
| 598 |
$validate = 'valid'; |
| 599 |
} |
| 600 |
if ( $validate === false ) { |
| 601 |
$validation = UserRigorOptions::RIGOR_NONE; |
| 602 |
} elseif ( array_key_exists( $validate, $validationLevels ) ) { |
| 603 |
$validation = $validationLevels[ $validate ]; |
| 604 |
} else { |
| 605 |
|
| 606 |
|
| 607 |
$validation = $validate; |
| 608 |
} |
| 609 |
|
| 610 |
return MediaWikiServices::getInstance()->getUserFactory() |
| 611 |
->newFromName( (string)$name, $validation ) ?? false; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
public static function newFromId( $id ) { |
| 625 |
return MediaWikiServices::getInstance() |
| 626 |
->getUserFactory() |
| 627 |
->newFromId( (int)$id ); |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
public static function newFromActorId( $id ) { |
| 642 |
return MediaWikiServices::getInstance() |
| 643 |
->getUserFactory() |
| 644 |
->newFromActorId( (int)$id ); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
public static function newFromIdentity( UserIdentity $identity ) { |
| 661 |
|
| 662 |
|
| 663 |
if ( $identity instanceof User ) { |
| 664 |
return $identity; |
| 665 |
} |
| 666 |
|
| 667 |
return MediaWikiServices::getInstance() |
| 668 |
->getUserFactory() |
| 669 |
->newFromUserIdentity( $identity ); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
public static function newFromAnyId( $userId, $userName, $actorId, $dbDomain = false ) { |
| 691 |
return MediaWikiServices::getInstance() |
| 692 |
->getUserFactory() |
| 693 |
->newFromAnyId( $userId, $userName, $actorId, $dbDomain ); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
public static function newFromConfirmationCode( $code, $flags = IDBAccessObject::READ_NORMAL ) { |
| 712 |
return MediaWikiServices::getInstance() |
| 713 |
->getUserFactory() |
| 714 |
->newFromConfirmationCode( (string)$code, $flags ); |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
public static function newFromSession( ?WebRequest $request = null ) { |
| 725 |
$user = new User; |
| 726 |
$user->mFrom = 'session'; |
| 727 |
$user->mRequest = $request; |
| 728 |
return $user; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
public static function newFromRow( $row, $data = null ) { |
| 747 |
$user = new User; |
| 748 |
$user->loadFromRow( $row, $data ); |
| 749 |
return $user; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
public static function newSystemUser( $name, $options = [] ) { |
| 797 |
$options += [ |
| 798 |
'validate' => UserRigorOptions::RIGOR_VALID, |
| 799 |
'create' => true, |
| 800 |
'steal' => false, |
| 801 |
]; |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
$validationLevels = [ |
| 806 |
'valid' => UserRigorOptions::RIGOR_VALID, |
| 807 |
'usable' => UserRigorOptions::RIGOR_USABLE, |
| 808 |
'creatable' => UserRigorOptions::RIGOR_CREATABLE |
| 809 |
]; |
| 810 |
$validate = $options['validate']; |
| 811 |
|
| 812 |
|
| 813 |
if ( $validate === false ) { |
| 814 |
$validation = UserRigorOptions::RIGOR_NONE; |
| 815 |
} elseif ( array_key_exists( $validate, $validationLevels ) ) { |
| 816 |
$validation = $validationLevels[ $validate ]; |
| 817 |
} else { |
| 818 |
|
| 819 |
|
| 820 |
$validation = $validate; |
| 821 |
} |
| 822 |
|
| 823 |
if ( $validation !== UserRigorOptions::RIGOR_VALID ) { |
| 824 |
wfDeprecatedMsg( |
| 825 |
__METHOD__ . ' options["validation"] parameter must be omitted or set to "valid".', |
| 826 |
'1.36' |
| 827 |
); |
| 828 |
} |
| 829 |
$services = MediaWikiServices::getInstance(); |
| 830 |
$userNameUtils = $services->getUserNameUtils(); |
| 831 |
|
| 832 |
$name = $userNameUtils->getCanonical( (string)$name, $validation ); |
| 833 |
if ( $name === false ) { |
| 834 |
return null; |
| 835 |
} |
| 836 |
|
| 837 |
$dbProvider = $services->getDBLoadBalancerFactory(); |
| 838 |
$dbr = $dbProvider->getReplicaDatabase(); |
| 839 |
|
| 840 |
$userQuery = self::newQueryBuilder( $dbr ) |
| 841 |
->where( [ 'user_name' => $name ] ) |
| 842 |
->caller( __METHOD__ ); |
| 843 |
$row = $userQuery->fetchRow(); |
| 844 |
if ( !$row ) { |
| 845 |
|
| 846 |
$userQuery->connection( $dbProvider->getPrimaryDatabase() ); |
| 847 |
|
| 848 |
$userQuery->forUpdate(); |
| 849 |
$row = $userQuery->fetchRow(); |
| 850 |
} |
| 851 |
|
| 852 |
if ( !$row ) { |
| 853 |
|
| 854 |
|
| 855 |
if ( !$options['create'] ) { |
| 856 |
|
| 857 |
return null; |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
return self::insertNewUser( static function ( UserIdentity $actor, IDatabase $dbw ) { |
| 863 |
return MediaWikiServices::getInstance()->getActorStore() |
| 864 |
->acquireSystemActorId( $actor, $dbw ); |
| 865 |
}, $name, [ 'token' => self::INVALID_TOKEN ] ); |
| 866 |
} |
| 867 |
|
| 868 |
$user = self::newFromRow( $row ); |
| 869 |
|
| 870 |
if ( !$user->isSystemUser() ) { |
| 871 |
|
| 872 |
|
| 873 |
if ( !$options['steal'] ) { |
| 874 |
return null; |
| 875 |
} |
| 876 |
|
| 877 |
$services->getAuthManager()->revokeAccessForUser( $name ); |
| 878 |
|
| 879 |
$user->invalidateEmail(); |
| 880 |
$user->mToken = self::INVALID_TOKEN; |
| 881 |
$user->saveSettings(); |
| 882 |
SessionManager::singleton()->preventSessionsForUser( $user->getName() ); |
| 883 |
} |
| 884 |
|
| 885 |
return $user; |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
public static function whoIs( $id ) { |
| 898 |
return MediaWikiServices::getInstance()->getUserCache() |
| 899 |
->getProp( $id, 'name' ); |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
public static function whoIsReal( $id ) { |
| 910 |
return MediaWikiServices::getInstance()->getUserCache() |
| 911 |
->getProp( $id, 'real_name' ); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
public static function findUsersByGroup( $groups, $limit = 5000, $after = null ) { |
| 925 |
if ( $groups === [] ) { |
| 926 |
return UserArrayFromResult::newFromIDs( [] ); |
| 927 |
} |
| 928 |
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 929 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 930 |
->select( 'ug_user' ) |
| 931 |
->distinct() |
| 932 |
->from( 'user_groups' ) |
| 933 |
->where( [ 'ug_group' => array_unique( (array)$groups ) ] ) |
| 934 |
->orderBy( 'ug_user' ) |
| 935 |
->limit( min( 5000, $limit ) ); |
| 936 |
|
| 937 |
if ( $after !== null ) { |
| 938 |
$queryBuilder->andWhere( $dbr->expr( 'ug_user', '>', (int)$after ) ); |
| 939 |
} |
| 940 |
|
| 941 |
$ids = $queryBuilder->caller( __METHOD__ )->fetchFieldValues() ?: []; |
| 942 |
return UserArray::newFromIDs( $ids ); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
public function isValidPassword( $password ) { |
| 952 |
|
| 953 |
return $this->checkPasswordValidity( $password )->isGood(); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
public function checkPasswordValidity( $password ) { |
| 978 |
$passwordPolicy = MediaWikiServices::getInstance()->getMainConfig() |
| 979 |
->get( MainConfigNames::PasswordPolicy ); |
| 980 |
|
| 981 |
$upp = new UserPasswordPolicy( |
| 982 |
$passwordPolicy['policies'], |
| 983 |
$passwordPolicy['checks'] |
| 984 |
); |
| 985 |
|
| 986 |
$status = Status::newGood( [] ); |
| 987 |
$result = false; |
| 988 |
|
| 989 |
if ( !$this->getHookRunner()->onIsValidPassword( $password, $result, $this ) ) { |
| 990 |
$status->error( $result ); |
| 991 |
return $status; |
| 992 |
} |
| 993 |
|
| 994 |
if ( $result === false ) { |
| 995 |
$status->merge( $upp->checkUserPassword( $this, $password ), true ); |
| 996 |
return $status; |
| 997 |
} |
| 998 |
|
| 999 |
if ( $result === true ) { |
| 1000 |
return $status; |
| 1001 |
} |
| 1002 |
|
| 1003 |
$status->error( $result ); |
| 1004 |
return $status; |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
public function loadDefaults( $name = false, $actorId = null ) { |
| 1017 |
$this->mId = 0; |
| 1018 |
$this->mName = $name; |
| 1019 |
$this->mActorId = $actorId; |
| 1020 |
$this->mRealName = ''; |
| 1021 |
$this->mEmail = ''; |
| 1022 |
$this->isTemp = null; |
| 1023 |
|
| 1024 |
$loggedOut = $this->mRequest && !defined( 'MW_NO_SESSION' ) |
| 1025 |
? $this->mRequest->getSession()->getLoggedOutTimestamp() : 0; |
| 1026 |
if ( $loggedOut !== 0 ) { |
| 1027 |
$this->mTouched = wfTimestamp( TS_MW, $loggedOut ); |
| 1028 |
} else { |
| 1029 |
$this->mTouched = '1'; |
| 1030 |
} |
| 1031 |
|
| 1032 |
$this->mToken = null; |
| 1033 |
$this->mEmailAuthenticated = null; |
| 1034 |
$this->mEmailToken = ''; |
| 1035 |
$this->mEmailTokenExpires = null; |
| 1036 |
$this->mRegistration = wfTimestamp( TS_MW ); |
| 1037 |
|
| 1038 |
$this->getHookRunner()->onUserLoadDefaults( $this, $name ); |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
public function isItemLoaded( $item, $all = 'all' ) { |
| 1054 |
return ( $this->mLoadedItems === true && $all === 'all' ) || |
| 1055 |
( isset( $this->mLoadedItems[$item] ) && $this->mLoadedItems[$item] === true ); |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
public function setItemLoaded( $item ) { |
| 1066 |
if ( is_array( $this->mLoadedItems ) ) { |
| 1067 |
$this->mLoadedItems[$item] = true; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
private function loadFromSession() { |
| 1077 |
|
| 1078 |
|
| 1079 |
$session = $this->getRequest()->getSession(); |
| 1080 |
$user = $session->getUser(); |
| 1081 |
if ( $user->isRegistered() ) { |
| 1082 |
$this->loadFromUserObject( $user ); |
| 1083 |
|
| 1084 |
|
| 1085 |
$session->set( 'wsUserID', $this->getId() ); |
| 1086 |
$session->set( 'wsUserName', $this->getName() ); |
| 1087 |
$session->set( 'wsToken', $this->getToken() ); |
| 1088 |
|
| 1089 |
return true; |
| 1090 |
} |
| 1091 |
|
| 1092 |
return false; |
| 1093 |
} |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
public function loadFromDatabase( $flags = IDBAccessObject::READ_LATEST ) { |
| 1103 |
|
| 1104 |
$this->mId = intval( $this->mId ); |
| 1105 |
|
| 1106 |
if ( !$this->mId ) { |
| 1107 |
|
| 1108 |
$this->loadDefaults(); |
| 1109 |
return false; |
| 1110 |
} |
| 1111 |
|
| 1112 |
$db = DBAccessObjectUtils::getDBFromRecency( |
| 1113 |
MediaWikiServices::getInstance()->getDBLoadBalancerFactory(), |
| 1114 |
$flags |
| 1115 |
); |
| 1116 |
$row = self::newQueryBuilder( $db ) |
| 1117 |
->where( [ 'user_id' => $this->mId ] ) |
| 1118 |
->recency( $flags ) |
| 1119 |
->caller( __METHOD__ ) |
| 1120 |
->fetchRow(); |
| 1121 |
|
| 1122 |
$this->queryFlagsUsed = $flags; |
| 1123 |
|
| 1124 |
if ( $row !== false ) { |
| 1125 |
|
| 1126 |
$this->loadFromRow( $row ); |
| 1127 |
return true; |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
$this->mId = 0; |
| 1132 |
$this->loadDefaults( 'Unknown user' ); |
| 1133 |
|
| 1134 |
return false; |
| 1135 |
} |
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
protected function loadFromRow( $row, $data = null ) { |
| 1149 |
if ( !is_object( $row ) ) { |
| 1150 |
throw new InvalidArgumentException( '$row must be an object' ); |
| 1151 |
} |
| 1152 |
|
| 1153 |
$all = true; |
| 1154 |
|
| 1155 |
if ( isset( $row->actor_id ) ) { |
| 1156 |
$this->mActorId = (int)$row->actor_id; |
| 1157 |
if ( $this->mActorId !== 0 ) { |
| 1158 |
$this->mFrom = 'actor'; |
| 1159 |
} |
| 1160 |
$this->setItemLoaded( 'actor' ); |
| 1161 |
} else { |
| 1162 |
$all = false; |
| 1163 |
} |
| 1164 |
|
| 1165 |
if ( isset( $row->user_name ) && $row->user_name !== '' ) { |
| 1166 |
$this->mName = $row->user_name; |
| 1167 |
$this->mFrom = 'name'; |
| 1168 |
$this->setItemLoaded( 'name' ); |
| 1169 |
} else { |
| 1170 |
$all = false; |
| 1171 |
} |
| 1172 |
|
| 1173 |
if ( isset( $row->user_real_name ) ) { |
| 1174 |
$this->mRealName = $row->user_real_name; |
| 1175 |
$this->setItemLoaded( 'realname' ); |
| 1176 |
} else { |
| 1177 |
$all = false; |
| 1178 |
} |
| 1179 |
|
| 1180 |
if ( isset( $row->user_id ) ) { |
| 1181 |
$this->mId = intval( $row->user_id ); |
| 1182 |
if ( $this->mId !== 0 ) { |
| 1183 |
$this->mFrom = 'id'; |
| 1184 |
} |
| 1185 |
$this->setItemLoaded( 'id' ); |
| 1186 |
} else { |
| 1187 |
$all = false; |
| 1188 |
} |
| 1189 |
|
| 1190 |
if ( isset( $row->user_editcount ) ) { |
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
if ( $this->mId !== null && $this->mId !== 0 ) { |
| 1196 |
MediaWikiServices::getInstance() |
| 1197 |
->getUserEditTracker() |
| 1198 |
->setCachedUserEditCount( $this, (int)$row->user_editcount ); |
| 1199 |
} |
| 1200 |
} else { |
| 1201 |
$all = false; |
| 1202 |
} |
| 1203 |
|
| 1204 |
if ( isset( $row->user_touched ) ) { |
| 1205 |
$this->mTouched = wfTimestamp( TS_MW, $row->user_touched ); |
| 1206 |
} else { |
| 1207 |
$all = false; |
| 1208 |
} |
| 1209 |
|
| 1210 |
if ( isset( $row->user_token ) ) { |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
$this->mToken = rtrim( $row->user_token, " \0" ); |
| 1215 |
if ( $this->mToken === '' ) { |
| 1216 |
$this->mToken = null; |
| 1217 |
} |
| 1218 |
} else { |
| 1219 |
$all = false; |
| 1220 |
} |
| 1221 |
|
| 1222 |
if ( isset( $row->user_email ) ) { |
| 1223 |
$this->mEmail = $row->user_email; |
| 1224 |
$this->mEmailAuthenticated = wfTimestampOrNull( TS_MW, $row->user_email_authenticated ); |
| 1225 |
$this->mEmailToken = $row->user_email_token; |
| 1226 |
$this->mEmailTokenExpires = wfTimestampOrNull( TS_MW, $row->user_email_token_expires ); |
| 1227 |
$this->mRegistration = wfTimestampOrNull( TS_MW, $row->user_registration ); |
| 1228 |
} else { |
| 1229 |
$all = false; |
| 1230 |
} |
| 1231 |
|
| 1232 |
if ( $all ) { |
| 1233 |
$this->mLoadedItems = true; |
| 1234 |
} |
| 1235 |
|
| 1236 |
if ( is_array( $data ) ) { |
| 1237 |
|
| 1238 |
if ( isset( $data['user_groups'] ) && is_array( $data['user_groups'] ) ) { |
| 1239 |
MediaWikiServices::getInstance() |
| 1240 |
->getUserGroupManager() |
| 1241 |
->loadGroupMembershipsFromArray( |
| 1242 |
$this, |
| 1243 |
$data['user_groups'], |
| 1244 |
$this->queryFlagsUsed |
| 1245 |
); |
| 1246 |
} |
| 1247 |
} |
| 1248 |
} |
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
protected function loadFromUserObject( $user ) { |
| 1256 |
$user->load(); |
| 1257 |
foreach ( self::$mCacheVars as $var ) { |
| 1258 |
$this->$var = $user->$var; |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
protected function makeUpdateConditions( IReadableDatabase $db, array $conditions ) { |
| 1272 |
if ( $this->mTouched ) { |
| 1273 |
|
| 1274 |
$conditions['user_touched'] = $db->timestamp( $this->mTouched ); |
| 1275 |
} |
| 1276 |
|
| 1277 |
return $conditions; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
public function checkAndSetTouched() { |
| 1291 |
$this->load(); |
| 1292 |
|
| 1293 |
if ( !$this->mId ) { |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
|
| 1297 |
|
| 1298 |
$newTouched = $this->newTouchedTimestamp(); |
| 1299 |
|
| 1300 |
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 1301 |
$dbw->newUpdateQueryBuilder() |
| 1302 |
->update( 'user' ) |
| 1303 |
->set( [ 'user_touched' => $dbw->timestamp( $newTouched ) ] ) |
| 1304 |
->where( $this->makeUpdateConditions( $dbw, [ |
| 1305 |
'user_id' => $this->mId, |
| 1306 |
] ) ) |
| 1307 |
->caller( __METHOD__ )->execute(); |
| 1308 |
$success = ( $dbw->affectedRows() > 0 ); |
| 1309 |
|
| 1310 |
if ( $success ) { |
| 1311 |
$this->mTouched = $newTouched; |
| 1312 |
$this->clearSharedCache( 'changed' ); |
| 1313 |
} else { |
| 1314 |
|
| 1315 |
$this->clearSharedCache( 'refresh' ); |
| 1316 |
} |
| 1317 |
|
| 1318 |
return $success; |
| 1319 |
} |
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
public function clearInstanceCache( $reloadFrom = false ) { |
| 1329 |
global $wgFullyInitialised; |
| 1330 |
|
| 1331 |
$this->mDatePreference = null; |
| 1332 |
$this->mHash = false; |
| 1333 |
$this->mThisAsAuthority = null; |
| 1334 |
|
| 1335 |
if ( $wgFullyInitialised && $this->mFrom ) { |
| 1336 |
$services = MediaWikiServices::getInstance(); |
| 1337 |
|
| 1338 |
if ( $services->peekService( 'PermissionManager' ) ) { |
| 1339 |
$services->getPermissionManager()->invalidateUsersRightsCache( $this ); |
| 1340 |
} |
| 1341 |
|
| 1342 |
if ( $services->peekService( 'UserOptionsManager' ) ) { |
| 1343 |
$services->getUserOptionsManager()->clearUserOptionsCache( $this ); |
| 1344 |
} |
| 1345 |
|
| 1346 |
if ( $services->peekService( 'TalkPageNotificationManager' ) ) { |
| 1347 |
$services->getTalkPageNotificationManager()->clearInstanceCache( $this ); |
| 1348 |
} |
| 1349 |
|
| 1350 |
if ( $services->peekService( 'UserGroupManager' ) ) { |
| 1351 |
$services->getUserGroupManager()->clearCache( $this ); |
| 1352 |
} |
| 1353 |
|
| 1354 |
if ( $services->peekService( 'UserEditTracker' ) ) { |
| 1355 |
$services->getUserEditTracker()->clearUserEditCache( $this ); |
| 1356 |
} |
| 1357 |
|
| 1358 |
if ( $services->peekService( 'BlockManager' ) ) { |
| 1359 |
$services->getBlockManager()->clearUserCache( $this ); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
if ( $reloadFrom ) { |
| 1364 |
if ( in_array( $reloadFrom, [ 'name', 'id', 'actor' ] ) ) { |
| 1365 |
$this->mLoadedItems = [ $reloadFrom => true ]; |
| 1366 |
} else { |
| 1367 |
$this->mLoadedItems = []; |
| 1368 |
} |
| 1369 |
$this->mFrom = $reloadFrom; |
| 1370 |
} |
| 1371 |
} |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
public function isPingLimitable() { |
| 1379 |
$limiter = MediaWikiServices::getInstance()->getRateLimiter(); |
| 1380 |
$subject = $this->toRateLimitSubject(); |
| 1381 |
return !$limiter->isExempt( $subject ); |
| 1382 |
} |
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
public function pingLimiter( $action = 'edit', $incrBy = 1 ) { |
| 1400 |
return $this->getThisAsAuthority()->limit( $action, $incrBy, null ); |
| 1401 |
} |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
public function toRateLimitSubject(): RateLimitSubject { |
| 1408 |
$flags = [ |
| 1409 |
'exempt' => $this->isAllowed( 'noratelimit' ), |
| 1410 |
'newbie' => $this->isNewbie(), |
| 1411 |
]; |
| 1412 |
|
| 1413 |
return new RateLimitSubject( $this, $this->getRequest()->getIP(), $flags ); |
| 1414 |
} |
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
public function getBlock( |
| 1429 |
$freshness = IDBAccessObject::READ_NORMAL, |
| 1430 |
$disableIpBlockExemptChecking = false |
| 1431 |
): ?Block { |
| 1432 |
if ( is_bool( $freshness ) ) { |
| 1433 |
$fromReplica = $freshness; |
| 1434 |
} else { |
| 1435 |
$fromReplica = ( $freshness !== IDBAccessObject::READ_LATEST ); |
| 1436 |
} |
| 1437 |
|
| 1438 |
if ( $disableIpBlockExemptChecking ) { |
| 1439 |
$isExempt = false; |
| 1440 |
} else { |
| 1441 |
$isExempt = $this->isAllowed( 'ipblock-exempt' ); |
| 1442 |
} |
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
$request = null; |
| 1452 |
if ( !$isExempt && $this->isGlobalSessionUser() ) { |
| 1453 |
|
| 1454 |
$request = $this->getRequest(); |
| 1455 |
} |
| 1456 |
|
| 1457 |
return MediaWikiServices::getInstance()->getBlockManager()->getBlock( |
| 1458 |
$this, |
| 1459 |
$request, |
| 1460 |
$fromReplica, |
| 1461 |
); |
| 1462 |
} |
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
public function isBlockedGlobally( $ip = '' ) { |
| 1474 |
wfDeprecated( __METHOD__, '1.40' ); |
| 1475 |
return $this->getGlobalBlock( $ip ) instanceof AbstractBlock; |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
public function getGlobalBlock( $ip = '' ) { |
| 1488 |
if ( $this->mGlobalBlock !== null ) { |
| 1489 |
return $this->mGlobalBlock ?: null; |
| 1490 |
} |
| 1491 |
|
| 1492 |
if ( IPUtils::isIPAddress( $this->getName() ) ) { |
| 1493 |
$ip = $this->getName(); |
| 1494 |
} elseif ( !$ip ) { |
| 1495 |
$ip = $this->getRequest()->getIP(); |
| 1496 |
} |
| 1497 |
$blocked = false; |
| 1498 |
$block = null; |
| 1499 |
$this->getHookRunner()->onUserIsBlockedGlobally( $this, $ip, $blocked, $block ); |
| 1500 |
|
| 1501 |
if ( $blocked && $block === null ) { |
| 1502 |
|
| 1503 |
$block = new SystemBlock( [ |
| 1504 |
'address' => $ip, |
| 1505 |
'systemBlock' => 'global-block' |
| 1506 |
] ); |
| 1507 |
} |
| 1508 |
|
| 1509 |
$this->mGlobalBlock = $blocked ? $block : false; |
| 1510 |
return $this->mGlobalBlock ?: null; |
| 1511 |
} |
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
public function isLocked() { |
| 1519 |
if ( $this->mLocked !== null ) { |
| 1520 |
return $this->mLocked; |
| 1521 |
} |
| 1522 |
|
| 1523 |
$this->mLocked = false; |
| 1524 |
$this->getHookRunner()->onUserIsLocked( $this, $this->mLocked ); |
| 1525 |
return $this->mLocked; |
| 1526 |
} |
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
public function isHidden() { |
| 1534 |
$block = $this->getBlock(); |
| 1535 |
return $block ? $block->getHideName() : false; |
| 1536 |
} |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
public function getId( $wikiId = self::LOCAL ): int { |
| 1544 |
$this->assertWiki( $wikiId ); |
| 1545 |
if ( $this->mId === null && $this->mName !== null ) { |
| 1546 |
$userNameUtils = MediaWikiServices::getInstance()->getUserNameUtils(); |
| 1547 |
if ( $userNameUtils->isIP( $this->mName ) || ExternalUserNames::isExternal( $this->mName ) ) { |
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
return 0; |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
if ( !$this->isItemLoaded( 'id' ) ) { |
| 1556 |
|
| 1557 |
$this->load(); |
| 1558 |
} |
| 1559 |
|
| 1560 |
return (int)$this->mId; |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
public function setId( $v ) { |
| 1568 |
$this->mId = $v; |
| 1569 |
$this->clearInstanceCache( 'id' ); |
| 1570 |
} |
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
public function getName(): string { |
| 1577 |
if ( $this->isItemLoaded( 'name', 'only' ) ) { |
| 1578 |
|
| 1579 |
return $this->mName; |
| 1580 |
} |
| 1581 |
|
| 1582 |
$this->load(); |
| 1583 |
if ( $this->mName === false ) { |
| 1584 |
|
| 1585 |
$this->mName = IPUtils::sanitizeIP( $this->getRequest()->getIP() ); |
| 1586 |
} |
| 1587 |
|
| 1588 |
return $this->mName; |
| 1589 |
} |
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
public function setName( $str ) { |
| 1605 |
$this->load(); |
| 1606 |
$this->mName = $str; |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
public function getActorId( $dbwOrWikiId = self::LOCAL ): int { |
| 1623 |
if ( $dbwOrWikiId ) { |
| 1624 |
wfDeprecatedMsg( 'Passing a parameter to getActorId() is deprecated', '1.36' ); |
| 1625 |
} |
| 1626 |
|
| 1627 |
if ( is_string( $dbwOrWikiId ) ) { |
| 1628 |
$this->assertWiki( $dbwOrWikiId ); |
| 1629 |
} |
| 1630 |
|
| 1631 |
if ( !$this->isItemLoaded( 'actor' ) ) { |
| 1632 |
$this->load(); |
| 1633 |
} |
| 1634 |
|
| 1635 |
if ( !$this->mActorId && $dbwOrWikiId instanceof IDatabase ) { |
| 1636 |
MediaWikiServices::getInstance() |
| 1637 |
->getActorStoreFactory() |
| 1638 |
->getActorNormalization( $dbwOrWikiId->getDomainID() ) |
| 1639 |
->acquireActorId( $this, $dbwOrWikiId ); |
| 1640 |
|
| 1641 |
Assert::postcondition( |
| 1642 |
$this->mActorId !== null, |
| 1643 |
"Failed to acquire actor ID for user id {$this->mId} name {$this->mName}" |
| 1644 |
); |
| 1645 |
} |
| 1646 |
|
| 1647 |
return (int)$this->mActorId; |
| 1648 |
} |
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
public function setActorId( int $actorId ) { |
| 1660 |
$this->mActorId = $actorId; |
| 1661 |
$this->setItemLoaded( 'actor' ); |
| 1662 |
} |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
public function getTitleKey(): string { |
| 1669 |
return str_replace( ' ', '_', $this->getName() ); |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
private function newTouchedTimestamp() { |
| 1679 |
$time = (int)ConvertibleTimestamp::now( TS_UNIX ); |
| 1680 |
if ( $this->mTouched ) { |
| 1681 |
$time = max( $time, (int)ConvertibleTimestamp::convert( TS_UNIX, $this->mTouched ) + 1 ); |
| 1682 |
} |
| 1683 |
|
| 1684 |
return ConvertibleTimestamp::convert( TS_MW, $time ); |
| 1685 |
} |
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
public function clearSharedCache( $mode = 'refresh' ) { |
| 1698 |
if ( !$this->getId() ) { |
| 1699 |
return; |
| 1700 |
} |
| 1701 |
|
| 1702 |
$dbProvider = MediaWikiServices::getInstance()->getConnectionProvider(); |
| 1703 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 1704 |
$key = $this->getCacheKey( $cache ); |
| 1705 |
|
| 1706 |
if ( $mode === 'refresh' ) { |
| 1707 |
$cache->delete( $key, 1 ); |
| 1708 |
} else { |
| 1709 |
$dbProvider->getPrimaryDatabase()->onTransactionPreCommitOrIdle( |
| 1710 |
static function () use ( $cache, $key ) { |
| 1711 |
$cache->delete( $key ); |
| 1712 |
}, |
| 1713 |
__METHOD__ |
| 1714 |
); |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
public function invalidateCache() { |
| 1724 |
$this->touch(); |
| 1725 |
$this->clearSharedCache( 'changed' ); |
| 1726 |
} |
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
public function touch() { |
| 1741 |
$id = $this->getId(); |
| 1742 |
if ( $id ) { |
| 1743 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 1744 |
$key = $cache->makeKey( 'user-quicktouched', 'id', $id ); |
| 1745 |
$cache->touchCheckKey( $key ); |
| 1746 |
$this->mQuickTouched = null; |
| 1747 |
} |
| 1748 |
} |
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
public function validateCache( $timestamp ) { |
| 1756 |
return ( $timestamp >= $this->getTouched() ); |
| 1757 |
} |
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
public function getTouched() { |
| 1768 |
$this->load(); |
| 1769 |
|
| 1770 |
if ( $this->mId ) { |
| 1771 |
if ( $this->mQuickTouched === null ) { |
| 1772 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 1773 |
$key = $cache->makeKey( 'user-quicktouched', 'id', $this->mId ); |
| 1774 |
|
| 1775 |
$this->mQuickTouched = wfTimestamp( TS_MW, $cache->getCheckKeyTime( $key ) ); |
| 1776 |
} |
| 1777 |
|
| 1778 |
return max( $this->mTouched, $this->mQuickTouched ); |
| 1779 |
} |
| 1780 |
|
| 1781 |
return $this->mTouched; |
| 1782 |
} |
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
public function getDBTouched() { |
| 1790 |
$this->load(); |
| 1791 |
|
| 1792 |
return $this->mTouched; |
| 1793 |
} |
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
public function changeAuthenticationData( array $data ) { |
| 1808 |
$manager = MediaWikiServices::getInstance()->getAuthManager(); |
| 1809 |
$reqs = $manager->getAuthenticationRequests( AuthManager::ACTION_CHANGE, $this ); |
| 1810 |
$reqs = AuthenticationRequest::loadRequestsFromSubmission( $reqs, $data ); |
| 1811 |
|
| 1812 |
$status = Status::newGood( 'ignored' ); |
| 1813 |
foreach ( $reqs as $req ) { |
| 1814 |
$status->merge( $manager->allowsAuthenticationDataChange( $req ), true ); |
| 1815 |
} |
| 1816 |
if ( $status->getValue() === 'ignored' ) { |
| 1817 |
$status->warning( 'authenticationdatachange-ignored' ); |
| 1818 |
} |
| 1819 |
|
| 1820 |
if ( $status->isGood() ) { |
| 1821 |
foreach ( $reqs as $req ) { |
| 1822 |
$manager->changeAuthenticationData( $req ); |
| 1823 |
} |
| 1824 |
} |
| 1825 |
return $status; |
| 1826 |
} |
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
public function getToken( $forceCreation = true ) { |
| 1835 |
$authenticationTokenVersion = MediaWikiServices::getInstance() |
| 1836 |
->getMainConfig()->get( MainConfigNames::AuthenticationTokenVersion ); |
| 1837 |
|
| 1838 |
$this->load(); |
| 1839 |
if ( !$this->mToken && $forceCreation ) { |
| 1840 |
$this->setToken(); |
| 1841 |
} |
| 1842 |
|
| 1843 |
if ( !$this->mToken ) { |
| 1844 |
|
| 1845 |
return null; |
| 1846 |
} |
| 1847 |
|
| 1848 |
if ( $this->mToken === self::INVALID_TOKEN ) { |
| 1849 |
|
| 1850 |
|
| 1851 |
return MWCryptRand::generateHex( self::TOKEN_LENGTH ); |
| 1852 |
} |
| 1853 |
|
| 1854 |
if ( $authenticationTokenVersion === null ) { |
| 1855 |
|
| 1856 |
return $this->mToken; |
| 1857 |
} |
| 1858 |
|
| 1859 |
|
| 1860 |
$ret = MWCryptHash::hmac( $authenticationTokenVersion, $this->mToken, false ); |
| 1861 |
|
| 1862 |
|
| 1863 |
$len = max( 32, self::TOKEN_LENGTH ); |
| 1864 |
if ( strlen( $ret ) < $len ) { |
| 1865 |
|
| 1866 |
throw new \UnexpectedValueException( 'Hmac returned less than 128 bits' ); |
| 1867 |
} |
| 1868 |
|
| 1869 |
return substr( $ret, -$len ); |
| 1870 |
} |
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
public function setToken( $token = false ) { |
| 1879 |
$this->load(); |
| 1880 |
if ( $this->mToken === self::INVALID_TOKEN ) { |
| 1881 |
LoggerFactory::getInstance( 'session' ) |
| 1882 |
->debug( __METHOD__ . ": Ignoring attempt to set token for system user \"$this\"" ); |
| 1883 |
} elseif ( !$token ) { |
| 1884 |
$this->mToken = MWCryptRand::generateHex( self::TOKEN_LENGTH ); |
| 1885 |
} else { |
| 1886 |
$this->mToken = $token; |
| 1887 |
} |
| 1888 |
} |
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
public function getEmail(): string { |
| 1895 |
$this->load(); |
| 1896 |
$email = $this->mEmail; |
| 1897 |
$this->getHookRunner()->onUserGetEmail( $this, $email ); |
| 1898 |
|
| 1899 |
$this->mEmail = is_string( $email ) ? $email : ''; |
| 1900 |
return $this->mEmail; |
| 1901 |
} |
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
public function getEmailAuthenticationTimestamp() { |
| 1908 |
$this->load(); |
| 1909 |
$this->getHookRunner()->onUserGetEmailAuthenticationTimestamp( |
| 1910 |
$this, $this->mEmailAuthenticated ); |
| 1911 |
return $this->mEmailAuthenticated; |
| 1912 |
} |
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
public function setEmail( string $str ) { |
| 1919 |
$this->load(); |
| 1920 |
if ( $str == $this->getEmail() ) { |
| 1921 |
return; |
| 1922 |
} |
| 1923 |
$this->invalidateEmail(); |
| 1924 |
$this->mEmail = $str; |
| 1925 |
$this->getHookRunner()->onUserSetEmail( $this, $this->mEmail ); |
| 1926 |
} |
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
public function setEmailWithConfirmation( string $str ) { |
| 1936 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 1937 |
$enableEmail = $config->get( MainConfigNames::EnableEmail ); |
| 1938 |
|
| 1939 |
if ( !$enableEmail ) { |
| 1940 |
return Status::newFatal( 'emaildisabled' ); |
| 1941 |
} |
| 1942 |
|
| 1943 |
$oldaddr = $this->getEmail(); |
| 1944 |
if ( $str === $oldaddr ) { |
| 1945 |
return Status::newGood( true ); |
| 1946 |
} |
| 1947 |
|
| 1948 |
$type = $oldaddr != '' ? 'changed' : 'set'; |
| 1949 |
$notificationResult = null; |
| 1950 |
|
| 1951 |
$emailAuthentication = $config->get( MainConfigNames::EmailAuthentication ); |
| 1952 |
|
| 1953 |
if ( $emailAuthentication && $type === 'changed' && $this->isEmailConfirmed() ) { |
| 1954 |
|
| 1955 |
|
| 1956 |
$change = $str != '' ? 'changed' : 'removed'; |
| 1957 |
$notificationResult = $this->sendMail( |
| 1958 |
wfMessage( 'notificationemail_subject_' . $change )->text(), |
| 1959 |
wfMessage( 'notificationemail_body_' . $change, |
| 1960 |
$this->getRequest()->getIP(), |
| 1961 |
$this->getName(), |
| 1962 |
$str )->text() |
| 1963 |
); |
| 1964 |
} |
| 1965 |
|
| 1966 |
$this->setEmail( $str ); |
| 1967 |
|
| 1968 |
if ( $str !== '' && $emailAuthentication ) { |
| 1969 |
|
| 1970 |
$result = $this->sendConfirmationMail( $type ); |
| 1971 |
|
| 1972 |
if ( $notificationResult !== null ) { |
| 1973 |
$result->merge( $notificationResult ); |
| 1974 |
} |
| 1975 |
|
| 1976 |
if ( $result->isGood() ) { |
| 1977 |
|
| 1978 |
$result->value = 'eauth'; |
| 1979 |
} |
| 1980 |
} else { |
| 1981 |
$result = Status::newGood( true ); |
| 1982 |
} |
| 1983 |
|
| 1984 |
return $result; |
| 1985 |
} |
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
public function getRealName(): string { |
| 1992 |
if ( !$this->isItemLoaded( 'realname' ) ) { |
| 1993 |
$this->load(); |
| 1994 |
} |
| 1995 |
|
| 1996 |
return $this->mRealName; |
| 1997 |
} |
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
public function setRealName( string $str ) { |
| 2004 |
$this->load(); |
| 2005 |
$this->mRealName = $str; |
| 2006 |
} |
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
public function getTokenFromOption( $oname ) { |
| 2019 |
$hiddenPrefs = |
| 2020 |
MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HiddenPrefs ); |
| 2021 |
|
| 2022 |
$id = $this->getId(); |
| 2023 |
if ( !$id || in_array( $oname, $hiddenPrefs ) ) { |
| 2024 |
return false; |
| 2025 |
} |
| 2026 |
|
| 2027 |
$userOptionsLookup = MediaWikiServices::getInstance() |
| 2028 |
->getUserOptionsLookup(); |
| 2029 |
$token = $userOptionsLookup->getOption( $this, (string)$oname ); |
| 2030 |
if ( !$token ) { |
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
$token = hash_hmac( 'sha1', "$oname:$id", $this->getToken() ); |
| 2035 |
} |
| 2036 |
|
| 2037 |
return $token; |
| 2038 |
} |
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
public function resetTokenFromOption( $oname ) { |
| 2050 |
$hiddenPrefs = |
| 2051 |
MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::HiddenPrefs ); |
| 2052 |
if ( in_array( $oname, $hiddenPrefs ) ) { |
| 2053 |
return false; |
| 2054 |
} |
| 2055 |
|
| 2056 |
$token = MWCryptRand::generateHex( 40 ); |
| 2057 |
MediaWikiServices::getInstance() |
| 2058 |
->getUserOptionsManager() |
| 2059 |
->setOption( $this, $oname, $token ); |
| 2060 |
return $token; |
| 2061 |
} |
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
public function getDatePreference() { |
| 2068 |
|
| 2069 |
if ( $this->mDatePreference === null ) { |
| 2070 |
global $wgLang; |
| 2071 |
$userOptionsLookup = MediaWikiServices::getInstance() |
| 2072 |
->getUserOptionsLookup(); |
| 2073 |
$value = $userOptionsLookup->getOption( $this, 'date' ) ?? 'default'; |
| 2074 |
$map = $wgLang->getDatePreferenceMigrationMap(); |
| 2075 |
if ( isset( $map[$value] ) ) { |
| 2076 |
$value = $map[$value]; |
| 2077 |
} |
| 2078 |
$this->mDatePreference = $value; |
| 2079 |
} |
| 2080 |
return $this->mDatePreference; |
| 2081 |
} |
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
public function requiresHTTPS() { |
| 2090 |
if ( !$this->isRegistered() ) { |
| 2091 |
return false; |
| 2092 |
} |
| 2093 |
|
| 2094 |
$services = MediaWikiServices::getInstance(); |
| 2095 |
$config = $services->getMainConfig(); |
| 2096 |
if ( $config->get( MainConfigNames::ForceHTTPS ) ) { |
| 2097 |
return true; |
| 2098 |
} |
| 2099 |
if ( !$config->get( MainConfigNames::SecureLogin ) ) { |
| 2100 |
return false; |
| 2101 |
} |
| 2102 |
return $services->getUserOptionsLookup() |
| 2103 |
->getBoolOption( $this, 'prefershttps' ); |
| 2104 |
} |
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
public function getEditCount() { |
| 2111 |
return MediaWikiServices::getInstance() |
| 2112 |
->getUserEditTracker() |
| 2113 |
->getUserEditCount( $this ); |
| 2114 |
} |
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
public function isRegistered(): bool { |
| 2125 |
return $this->getId() != 0; |
| 2126 |
} |
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
public function isAnon() { |
| 2133 |
return !$this->isRegistered(); |
| 2134 |
} |
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
public function isBot() { |
| 2141 |
$userGroupManager = MediaWikiServices::getInstance()->getUserGroupManager(); |
| 2142 |
if ( in_array( 'bot', $userGroupManager->getUserGroups( $this ) ) |
| 2143 |
&& $this->isAllowed( 'bot' ) |
| 2144 |
) { |
| 2145 |
return true; |
| 2146 |
} |
| 2147 |
|
| 2148 |
$isBot = false; |
| 2149 |
$this->getHookRunner()->onUserIsBot( $this, $isBot ); |
| 2150 |
|
| 2151 |
return $isBot; |
| 2152 |
} |
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
public function isSystemUser() { |
| 2164 |
$this->load(); |
| 2165 |
if ( $this->getEmail() || $this->mToken !== self::INVALID_TOKEN || |
| 2166 |
MediaWikiServices::getInstance()->getAuthManager()->userCanAuthenticate( $this->mName ) |
| 2167 |
) { |
| 2168 |
return false; |
| 2169 |
} |
| 2170 |
return true; |
| 2171 |
} |
| 2172 |
|
| 2173 |
public function isAllowedAny( ...$permissions ): bool { |
| 2174 |
return $this->getThisAsAuthority()->isAllowedAny( ...$permissions ); |
| 2175 |
} |
| 2176 |
|
| 2177 |
public function isAllowedAll( ...$permissions ): bool { |
| 2178 |
return $this->getThisAsAuthority()->isAllowedAll( ...$permissions ); |
| 2179 |
} |
| 2180 |
|
| 2181 |
public function isAllowed( string $permission, ?PermissionStatus $status = null ): bool { |
| 2182 |
return $this->getThisAsAuthority()->isAllowed( $permission, $status ); |
| 2183 |
} |
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
|
| 2189 |
public function useRCPatrol() { |
| 2190 |
$useRCPatrol = |
| 2191 |
MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseRCPatrol ); |
| 2192 |
return $useRCPatrol && $this->isAllowedAny( 'patrol', 'patrolmarks' ); |
| 2193 |
} |
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
public function useNPPatrol() { |
| 2200 |
$useRCPatrol = |
| 2201 |
MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseRCPatrol ); |
| 2202 |
$useNPPatrol = |
| 2203 |
MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseNPPatrol ); |
| 2204 |
return ( |
| 2205 |
( $useRCPatrol || $useNPPatrol ) |
| 2206 |
&& ( $this->isAllowedAny( 'patrol', 'patrolmarks' ) ) |
| 2207 |
); |
| 2208 |
} |
| 2209 |
|
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
public function useFilePatrol() { |
| 2215 |
$useRCPatrol = |
| 2216 |
MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UseRCPatrol ); |
| 2217 |
$useFilePatrol = MediaWikiServices::getInstance()->getMainConfig() |
| 2218 |
->get( MainConfigNames::UseFilePatrol ); |
| 2219 |
return ( |
| 2220 |
( $useRCPatrol || $useFilePatrol ) |
| 2221 |
&& ( $this->isAllowedAny( 'patrol', 'patrolmarks' ) ) |
| 2222 |
); |
| 2223 |
} |
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
public function getRequest(): WebRequest { |
| 2231 |
return $this->mRequest ?? RequestContext::getMain()->getRequest(); |
| 2232 |
} |
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
public function getExperienceLevel() { |
| 2240 |
$mainConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 2241 |
$learnerEdits = $mainConfig->get( MainConfigNames::LearnerEdits ); |
| 2242 |
$experiencedUserEdits = $mainConfig->get( MainConfigNames::ExperiencedUserEdits ); |
| 2243 |
$learnerMemberSince = $mainConfig->get( MainConfigNames::LearnerMemberSince ); |
| 2244 |
$experiencedUserMemberSince = |
| 2245 |
$mainConfig->get( MainConfigNames::ExperiencedUserMemberSince ); |
| 2246 |
if ( $this->isAnon() ) { |
| 2247 |
return false; |
| 2248 |
} |
| 2249 |
|
| 2250 |
$editCount = $this->getEditCount(); |
| 2251 |
$registration = $this->getRegistration(); |
| 2252 |
$now = time(); |
| 2253 |
$learnerRegistration = wfTimestamp( TS_MW, $now - $learnerMemberSince * 86400 ); |
| 2254 |
$experiencedRegistration = wfTimestamp( TS_MW, $now - $experiencedUserMemberSince * 86400 ); |
| 2255 |
if ( $registration === null ) { |
| 2256 |
|
| 2257 |
|
| 2258 |
$registration = $experiencedRegistration; |
| 2259 |
} |
| 2260 |
|
| 2261 |
if ( $editCount < $learnerEdits || |
| 2262 |
$registration > $learnerRegistration ) { |
| 2263 |
return 'newcomer'; |
| 2264 |
} |
| 2265 |
|
| 2266 |
if ( $editCount > $experiencedUserEdits && |
| 2267 |
$registration <= $experiencedRegistration |
| 2268 |
) { |
| 2269 |
return 'experienced'; |
| 2270 |
} |
| 2271 |
|
| 2272 |
return 'learner'; |
| 2273 |
} |
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
public function setCookies( $request = null, $secure = null, $rememberMe = false ) { |
| 2284 |
$this->load(); |
| 2285 |
if ( $this->mId == 0 ) { |
| 2286 |
return; |
| 2287 |
} |
| 2288 |
|
| 2289 |
$session = $this->getRequest()->getSession(); |
| 2290 |
if ( $request && $session->getRequest() !== $request ) { |
| 2291 |
$session = $session->sessionWithRequest( $request ); |
| 2292 |
} |
| 2293 |
$delay = $session->delaySave(); |
| 2294 |
|
| 2295 |
if ( !$session->getUser()->equals( $this ) ) { |
| 2296 |
if ( !$session->canSetUser() ) { |
| 2297 |
LoggerFactory::getInstance( 'session' ) |
| 2298 |
->warning( __METHOD__ . |
| 2299 |
": Cannot save user \"$this\" to a user " . |
| 2300 |
"\"{$session->getUser()}\"'s immutable session" |
| 2301 |
); |
| 2302 |
return; |
| 2303 |
} |
| 2304 |
$session->setUser( $this ); |
| 2305 |
} |
| 2306 |
|
| 2307 |
$session->setRememberUser( $rememberMe ); |
| 2308 |
if ( $secure !== null ) { |
| 2309 |
$session->setForceHTTPS( $secure ); |
| 2310 |
} |
| 2311 |
|
| 2312 |
$session->persist(); |
| 2313 |
|
| 2314 |
ScopedCallback::consume( $delay ); |
| 2315 |
} |
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
public function logout() { |
| 2321 |
if ( $this->getHookRunner()->onUserLogout( $this ) ) { |
| 2322 |
$this->doLogout(); |
| 2323 |
} |
| 2324 |
} |
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
public function doLogout() { |
| 2331 |
$session = $this->getRequest()->getSession(); |
| 2332 |
if ( !$session->canSetUser() ) { |
| 2333 |
LoggerFactory::getInstance( 'session' ) |
| 2334 |
->warning( __METHOD__ . ": Cannot log out of an immutable session" ); |
| 2335 |
$error = 'immutable'; |
| 2336 |
} elseif ( !$session->getUser()->equals( $this ) ) { |
| 2337 |
LoggerFactory::getInstance( 'session' ) |
| 2338 |
->warning( __METHOD__ . |
| 2339 |
": Cannot log user \"$this\" out of a user \"{$session->getUser()}\"'s session" |
| 2340 |
); |
| 2341 |
|
| 2342 |
$this->clearInstanceCache( 'defaults' ); |
| 2343 |
$error = 'wronguser'; |
| 2344 |
} else { |
| 2345 |
$this->clearInstanceCache( 'defaults' ); |
| 2346 |
$delay = $session->delaySave(); |
| 2347 |
$session->unpersist(); |
| 2348 |
$session->setLoggedOutTimestamp( time() ); |
| 2349 |
$session->setUser( new User ); |
| 2350 |
$session->set( 'wsUserID', 0 ); |
| 2351 |
$session->resetAllTokens(); |
| 2352 |
ScopedCallback::consume( $delay ); |
| 2353 |
$error = false; |
| 2354 |
} |
| 2355 |
LoggerFactory::getInstance( 'authevents' )->info( 'Logout', [ |
| 2356 |
'event' => 'logout', |
| 2357 |
'successful' => $error === false, |
| 2358 |
'status' => $error ?: 'success', |
| 2359 |
] ); |
| 2360 |
} |
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
public function saveSettings() { |
| 2367 |
if ( MediaWikiServices::getInstance()->getReadOnlyMode()->isReadOnly() ) { |
| 2368 |
|
| 2369 |
|
| 2370 |
MWExceptionHandler::logException( new DBExpectedError( |
| 2371 |
null, |
| 2372 |
"Could not update user with ID '{$this->mId}'; DB is read-only." |
| 2373 |
) ); |
| 2374 |
return; |
| 2375 |
} |
| 2376 |
|
| 2377 |
$this->load(); |
| 2378 |
if ( $this->mId == 0 ) { |
| 2379 |
return; |
| 2380 |
} |
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
|
| 2385 |
$newTouched = $this->newTouchedTimestamp(); |
| 2386 |
|
| 2387 |
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 2388 |
$dbw->doAtomicSection( __METHOD__, function ( IDatabase $dbw, $fname ) use ( $newTouched ) { |
| 2389 |
$dbw->newUpdateQueryBuilder() |
| 2390 |
->update( 'user' ) |
| 2391 |
->set( [ |
| 2392 |
'user_name' => $this->mName, |
| 2393 |
'user_real_name' => $this->mRealName, |
| 2394 |
'user_email' => $this->mEmail, |
| 2395 |
'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated ), |
| 2396 |
'user_touched' => $dbw->timestamp( $newTouched ), |
| 2397 |
'user_token' => strval( $this->mToken ), |
| 2398 |
'user_email_token' => $this->mEmailToken, |
| 2399 |
'user_email_token_expires' => $dbw->timestampOrNull( $this->mEmailTokenExpires ), |
| 2400 |
] ) |
| 2401 |
->where( $this->makeUpdateConditions( $dbw, [ |
| 2402 |
'user_id' => $this->mId, |
| 2403 |
] ) ) |
| 2404 |
->caller( $fname )->execute(); |
| 2405 |
|
| 2406 |
if ( !$dbw->affectedRows() ) { |
| 2407 |
|
| 2408 |
$this->clearSharedCache( 'refresh' ); |
| 2409 |
|
| 2410 |
$from = ( $this->queryFlagsUsed & IDBAccessObject::READ_LATEST ) ? 'primary' : 'replica'; |
| 2411 |
LoggerFactory::getInstance( 'preferences' )->warning( |
| 2412 |
"CAS update failed on user_touched for user ID '{user_id}' ({db_flag} read)", |
| 2413 |
[ 'user_id' => $this->mId, 'db_flag' => $from ] |
| 2414 |
); |
| 2415 |
throw new RuntimeException( "CAS update failed on user_touched. " . |
| 2416 |
"The version of the user to be saved is older than the current version." |
| 2417 |
); |
| 2418 |
} |
| 2419 |
|
| 2420 |
$dbw->newUpdateQueryBuilder() |
| 2421 |
->update( 'actor' ) |
| 2422 |
->set( [ 'actor_name' => $this->mName ] ) |
| 2423 |
->where( [ 'actor_user' => $this->mId ] ) |
| 2424 |
->caller( $fname )->execute(); |
| 2425 |
MediaWikiServices::getInstance()->getActorStore()->deleteUserIdentityFromCache( $this ); |
| 2426 |
} ); |
| 2427 |
|
| 2428 |
$this->mTouched = $newTouched; |
| 2429 |
if ( $this->isNamed() ) { |
| 2430 |
MediaWikiServices::getInstance()->getUserOptionsManager()->saveOptionsInternal( $this ); |
| 2431 |
} |
| 2432 |
|
| 2433 |
$this->getHookRunner()->onUserSaveSettings( $this ); |
| 2434 |
$this->clearSharedCache( 'changed' ); |
| 2435 |
$hcu = MediaWikiServices::getInstance()->getHtmlCacheUpdater(); |
| 2436 |
$hcu->purgeTitleUrls( $this->getUserPage(), $hcu::PURGE_INTENT_TXROUND_REFLECTED ); |
| 2437 |
} |
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
public function idForName( $flags = IDBAccessObject::READ_NORMAL ) { |
| 2446 |
$s = trim( $this->getName() ); |
| 2447 |
if ( $s === '' ) { |
| 2448 |
return 0; |
| 2449 |
} |
| 2450 |
|
| 2451 |
$db = DBAccessObjectUtils::getDBFromRecency( |
| 2452 |
MediaWikiServices::getInstance()->getDBLoadBalancerFactory(), |
| 2453 |
$flags |
| 2454 |
); |
| 2455 |
$id = $db->newSelectQueryBuilder() |
| 2456 |
->select( 'user_id' ) |
| 2457 |
->from( 'user' ) |
| 2458 |
->where( [ 'user_name' => $s ] ) |
| 2459 |
->recency( $flags ) |
| 2460 |
->caller( __METHOD__ )->fetchField(); |
| 2461 |
|
| 2462 |
return (int)$id; |
| 2463 |
} |
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
|
| 2477 |
|
| 2478 |
public static function createNew( $name, $params = [] ) { |
| 2479 |
return self::insertNewUser( static function ( UserIdentity $actor, IDatabase $dbw ) { |
| 2480 |
return MediaWikiServices::getInstance()->getActorStore()->createNewActor( $actor, $dbw ); |
| 2481 |
}, $name, $params ); |
| 2482 |
} |
| 2483 |
|
| 2484 |
|
| 2485 |
|
| 2486 |
|
| 2487 |
|
| 2488 |
|
| 2489 |
|
| 2490 |
|
| 2491 |
private static function insertNewUser( callable $insertActor, $name, $params = [] ) { |
| 2492 |
foreach ( [ 'password', 'newpassword', 'newpass_time', 'password_expires' ] as $field ) { |
| 2493 |
if ( isset( $params[$field] ) ) { |
| 2494 |
wfDeprecated( __METHOD__ . " with param '$field'", '1.27' ); |
| 2495 |
unset( $params[$field] ); |
| 2496 |
} |
| 2497 |
} |
| 2498 |
|
| 2499 |
$user = new User; |
| 2500 |
$user->load(); |
| 2501 |
$user->setToken(); |
| 2502 |
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 2503 |
|
| 2504 |
$noPass = PasswordFactory::newInvalidPassword()->toString(); |
| 2505 |
|
| 2506 |
$fields = [ |
| 2507 |
'user_name' => $name, |
| 2508 |
'user_password' => $noPass, |
| 2509 |
'user_newpassword' => $noPass, |
| 2510 |
'user_email' => $user->mEmail, |
| 2511 |
'user_email_authenticated' => $dbw->timestampOrNull( $user->mEmailAuthenticated ), |
| 2512 |
'user_real_name' => $user->mRealName, |
| 2513 |
'user_token' => strval( $user->mToken ), |
| 2514 |
'user_registration' => $dbw->timestamp( $user->mRegistration ), |
| 2515 |
'user_editcount' => 0, |
| 2516 |
'user_touched' => $dbw->timestamp( $user->newTouchedTimestamp() ), |
| 2517 |
]; |
| 2518 |
foreach ( $params as $name => $value ) { |
| 2519 |
$fields["user_$name"] = $value; |
| 2520 |
} |
| 2521 |
|
| 2522 |
return $dbw->doAtomicSection( __METHOD__, static function ( IDatabase $dbw, $fname ) |
| 2523 |
use ( $fields, $insertActor ) |
| 2524 |
{ |
| 2525 |
$dbw->newInsertQueryBuilder() |
| 2526 |
->insertInto( 'user' ) |
| 2527 |
->ignore() |
| 2528 |
->row( $fields ) |
| 2529 |
->caller( $fname )->execute(); |
| 2530 |
if ( $dbw->affectedRows() ) { |
| 2531 |
$newUser = self::newFromId( $dbw->insertId() ); |
| 2532 |
$newUser->mName = $fields['user_name']; |
| 2533 |
|
| 2534 |
|
| 2535 |
$newUser->mActorId = $insertActor( |
| 2536 |
new UserIdentityValue( $newUser->mId, $newUser->mName ), |
| 2537 |
$dbw |
| 2538 |
); |
| 2539 |
|
| 2540 |
$newUser->load( IDBAccessObject::READ_LATEST ); |
| 2541 |
} else { |
| 2542 |
$newUser = null; |
| 2543 |
} |
| 2544 |
return $newUser; |
| 2545 |
} ); |
| 2546 |
} |
| 2547 |
|
| 2548 |
|
| 2549 |
|
| 2550 |
|
| 2551 |
|
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
|
| 2562 |
|
| 2563 |
|
| 2564 |
|
| 2565 |
|
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
|
| 2571 |
|
| 2572 |
|
| 2573 |
public function addToDatabase() { |
| 2574 |
$this->load(); |
| 2575 |
if ( !$this->mToken ) { |
| 2576 |
$this->setToken(); |
| 2577 |
} |
| 2578 |
|
| 2579 |
if ( !is_string( $this->mName ) ) { |
| 2580 |
throw new RuntimeException( "User name field is not set." ); |
| 2581 |
} |
| 2582 |
|
| 2583 |
$this->mTouched = $this->newTouchedTimestamp(); |
| 2584 |
|
| 2585 |
$dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase(); |
| 2586 |
$status = $dbw->doAtomicSection( __METHOD__, function ( IDatabase $dbw, $fname ) { |
| 2587 |
$noPass = PasswordFactory::newInvalidPassword()->toString(); |
| 2588 |
$dbw->newInsertQueryBuilder() |
| 2589 |
->insertInto( 'user' ) |
| 2590 |
->ignore() |
| 2591 |
->row( [ |
| 2592 |
'user_name' => $this->mName, |
| 2593 |
'user_password' => $noPass, |
| 2594 |
'user_newpassword' => $noPass, |
| 2595 |
'user_email' => $this->mEmail, |
| 2596 |
'user_email_authenticated' => $dbw->timestampOrNull( $this->mEmailAuthenticated ), |
| 2597 |
'user_real_name' => $this->mRealName, |
| 2598 |
'user_token' => strval( $this->mToken ), |
| 2599 |
'user_registration' => $dbw->timestamp( $this->mRegistration ), |
| 2600 |
'user_editcount' => 0, |
| 2601 |
'user_touched' => $dbw->timestamp( $this->mTouched ), |
| 2602 |
'user_is_temp' => $this->isTemp(), |
| 2603 |
] ) |
| 2604 |
->caller( $fname )->execute(); |
| 2605 |
if ( !$dbw->affectedRows() ) { |
| 2606 |
|
| 2607 |
$this->mId = $dbw->newSelectQueryBuilder() |
| 2608 |
->select( 'user_id' ) |
| 2609 |
->lockInShareMode() |
| 2610 |
->from( 'user' ) |
| 2611 |
->where( [ 'user_name' => $this->mName ] ) |
| 2612 |
->caller( $fname )->fetchField(); |
| 2613 |
$loaded = false; |
| 2614 |
if ( $this->mId && $this->loadFromDatabase( IDBAccessObject::READ_LOCKING ) ) { |
| 2615 |
$loaded = true; |
| 2616 |
} |
| 2617 |
if ( !$loaded ) { |
| 2618 |
throw new RuntimeException( $fname . ": hit a key conflict attempting " . |
| 2619 |
"to insert user '{$this->mName}' row, but it was not present in select!" ); |
| 2620 |
} |
| 2621 |
return Status::newFatal( 'userexists' ); |
| 2622 |
} |
| 2623 |
$this->mId = $dbw->insertId(); |
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
$this->mActorId = MediaWikiServices::getInstance() |
| 2628 |
->getActorNormalization() |
| 2629 |
->acquireActorId( new UserIdentityValue( $this->mId, $this->mName ), $dbw ); |
| 2630 |
return Status::newGood(); |
| 2631 |
} ); |
| 2632 |
if ( !$status->isGood() ) { |
| 2633 |
return $status; |
| 2634 |
} |
| 2635 |
|
| 2636 |
|
| 2637 |
$this->clearInstanceCache(); |
| 2638 |
|
| 2639 |
if ( $this->isNamed() ) { |
| 2640 |
MediaWikiServices::getInstance()->getUserOptionsManager()->saveOptions( $this ); |
| 2641 |
} |
| 2642 |
return Status::newGood(); |
| 2643 |
} |
| 2644 |
|
| 2645 |
|
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
public function spreadAnyEditBlock() { |
| 2652 |
if ( !$this->isRegistered() ) { |
| 2653 |
return false; |
| 2654 |
} |
| 2655 |
|
| 2656 |
$blockWasSpread = false; |
| 2657 |
$this->getHookRunner()->onSpreadAnyEditBlock( $this, $blockWasSpread ); |
| 2658 |
|
| 2659 |
if ( $this->getBlock() ) { |
| 2660 |
$blockWasSpread = $blockWasSpread || $this->spreadBlock(); |
| 2661 |
} |
| 2662 |
|
| 2663 |
return $blockWasSpread; |
| 2664 |
} |
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
protected function spreadBlock() { |
| 2672 |
wfDebug( __METHOD__ . "()" ); |
| 2673 |
$this->load(); |
| 2674 |
if ( $this->mId == 0 ) { |
| 2675 |
return false; |
| 2676 |
} |
| 2677 |
|
| 2678 |
$blockStore = MediaWikiServices::getInstance()->getDatabaseBlockStore(); |
| 2679 |
|
| 2680 |
$userblock = $blockStore->newFromTarget( $this->getName() ); |
| 2681 |
if ( !$userblock ) { |
| 2682 |
return false; |
| 2683 |
} |
| 2684 |
|
| 2685 |
return (bool)$blockStore->doAutoblock( $userblock, $this->getRequest()->getIP() ); |
| 2686 |
} |
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
public function isBlockedFromEmailuser() { |
| 2696 |
wfDeprecated( __METHOD__, '1.41' ); |
| 2697 |
$block = $this->getBlock(); |
| 2698 |
return $block && $block->appliesToRight( 'sendemail' ); |
| 2699 |
} |
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 2706 |
|
| 2707 |
public function isBlockedFromUpload() { |
| 2708 |
$block = $this->getBlock(); |
| 2709 |
return $block && $block->appliesToRight( 'upload' ); |
| 2710 |
} |
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
public function isAllowedToCreateAccount() { |
| 2717 |
return $this->getThisAsAuthority()->isDefinitelyAllowed( 'createaccount' ); |
| 2718 |
} |
| 2719 |
|
| 2720 |
|
| 2721 |
|
| 2722 |
|
| 2723 |
|
| 2724 |
|
| 2725 |
public function getUserPage() { |
| 2726 |
return Title::makeTitle( NS_USER, $this->getName() ); |
| 2727 |
} |
| 2728 |
|
| 2729 |
|
| 2730 |
|
| 2731 |
|
| 2732 |
|
| 2733 |
|
| 2734 |
public function getTalkPage() { |
| 2735 |
$title = $this->getUserPage(); |
| 2736 |
return $title->getTalkPage(); |
| 2737 |
} |
| 2738 |
|
| 2739 |
|
| 2740 |
|
| 2741 |
|
| 2742 |
|
| 2743 |
|
| 2744 |
|
| 2745 |
|
| 2746 |
public function isNewbie() { |
| 2747 |
|
| 2748 |
return !$this->isAllowed( 'autoconfirmed' ); |
| 2749 |
} |
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
|
| 2763 |
public function getEditTokenObject( $salt = '', $request = null ) { |
| 2764 |
if ( $this->isAnon() ) { |
| 2765 |
return new LoggedOutEditToken(); |
| 2766 |
} |
| 2767 |
|
| 2768 |
if ( !$request ) { |
| 2769 |
$request = $this->getRequest(); |
| 2770 |
} |
| 2771 |
return $request->getSession()->getToken( $salt ); |
| 2772 |
} |
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
public function getEditToken( $salt = '', $request = null ) { |
| 2789 |
return $this->getEditTokenObject( $salt, $request )->toString(); |
| 2790 |
} |
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
public function matchEditToken( $val, $salt = '', $request = null, $maxage = null ) { |
| 2806 |
return $this->getEditTokenObject( $salt, $request )->match( $val, $maxage ); |
| 2807 |
} |
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 2815 |
|
| 2816 |
public function sendConfirmationMail( $type = 'created' ) { |
| 2817 |
global $wgLang; |
| 2818 |
$expiration = null; |
| 2819 |
$token = $this->confirmationToken( $expiration ); |
| 2820 |
$url = $this->confirmationTokenUrl( $token ); |
| 2821 |
$invalidateURL = $this->invalidationTokenUrl( $token ); |
| 2822 |
$this->saveSettings(); |
| 2823 |
|
| 2824 |
if ( $type == 'created' || $type === false ) { |
| 2825 |
$message = 'confirmemail_body'; |
| 2826 |
$type = 'created'; |
| 2827 |
} elseif ( $type === true ) { |
| 2828 |
$message = 'confirmemail_body_changed'; |
| 2829 |
$type = 'changed'; |
| 2830 |
} else { |
| 2831 |
|
| 2832 |
$message = 'confirmemail_body_' . $type; |
| 2833 |
} |
| 2834 |
|
| 2835 |
$mail = [ |
| 2836 |
'subject' => wfMessage( 'confirmemail_subject' )->text(), |
| 2837 |
'body' => wfMessage( $message, |
| 2838 |
$this->getRequest()->getIP(), |
| 2839 |
$this->getName(), |
| 2840 |
$url, |
| 2841 |
$wgLang->userTimeAndDate( $expiration, $this ), |
| 2842 |
$invalidateURL, |
| 2843 |
$wgLang->userDate( $expiration, $this ), |
| 2844 |
$wgLang->userTime( $expiration, $this ) )->text(), |
| 2845 |
'from' => null, |
| 2846 |
'replyTo' => null, |
| 2847 |
]; |
| 2848 |
$info = [ |
| 2849 |
'type' => $type, |
| 2850 |
'ip' => $this->getRequest()->getIP(), |
| 2851 |
'confirmURL' => $url, |
| 2852 |
'invalidateURL' => $invalidateURL, |
| 2853 |
'expiration' => $expiration |
| 2854 |
]; |
| 2855 |
|
| 2856 |
$this->getHookRunner()->onUserSendConfirmationMail( $this, $mail, $info ); |
| 2857 |
return $this->sendMail( $mail['subject'], $mail['body'], $mail['from'], $mail['replyTo'] ); |
| 2858 |
} |
| 2859 |
|
| 2860 |
|
| 2861 |
|
| 2862 |
|
| 2863 |
|
| 2864 |
|
| 2865 |
|
| 2866 |
|
| 2867 |
|
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
public function sendMail( $subject, $body, $from = null, $replyto = null ) { |
| 2872 |
$passwordSender = MediaWikiServices::getInstance()->getMainConfig() |
| 2873 |
->get( MainConfigNames::PasswordSender ); |
| 2874 |
|
| 2875 |
if ( $from instanceof User ) { |
| 2876 |
$sender = MailAddress::newFromUser( $from ); |
| 2877 |
} else { |
| 2878 |
$sender = new MailAddress( $passwordSender, |
| 2879 |
wfMessage( 'emailsender' )->inContentLanguage()->text() ); |
| 2880 |
} |
| 2881 |
$to = MailAddress::newFromUser( $this ); |
| 2882 |
|
| 2883 |
if ( is_array( $body ) ) { |
| 2884 |
$bodyText = $body['text'] ?? ''; |
| 2885 |
$bodyHtml = $body['html'] ?? null; |
| 2886 |
} else { |
| 2887 |
$bodyText = $body; |
| 2888 |
$bodyHtml = null; |
| 2889 |
} |
| 2890 |
|
| 2891 |
return Status::wrap( MediaWikiServices::getInstance()->getEmailer() |
| 2892 |
->send( |
| 2893 |
[ $to ], |
| 2894 |
$sender, |
| 2895 |
$subject, |
| 2896 |
$bodyText, |
| 2897 |
$bodyHtml, |
| 2898 |
[ 'replyTo' => $replyto ] |
| 2899 |
) ); |
| 2900 |
} |
| 2901 |
|
| 2902 |
|
| 2903 |
|
| 2904 |
|
| 2905 |
|
| 2906 |
|
| 2907 |
|
| 2908 |
|
| 2909 |
|
| 2910 |
|
| 2911 |
|
| 2912 |
protected function confirmationToken( &$expiration ) { |
| 2913 |
$userEmailConfirmationTokenExpiry = MediaWikiServices::getInstance() |
| 2914 |
->getMainConfig()->get( MainConfigNames::UserEmailConfirmationTokenExpiry ); |
| 2915 |
$now = time(); |
| 2916 |
$expires = $now + $userEmailConfirmationTokenExpiry; |
| 2917 |
$expiration = wfTimestamp( TS_MW, $expires ); |
| 2918 |
$this->load(); |
| 2919 |
$token = MWCryptRand::generateHex( 32 ); |
| 2920 |
$hash = md5( $token ); |
| 2921 |
$this->mEmailToken = $hash; |
| 2922 |
$this->mEmailTokenExpires = $expiration; |
| 2923 |
return $token; |
| 2924 |
} |
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
|
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
public static function isWellFormedConfirmationToken( string $token ): bool { |
| 2933 |
return preg_match( '/^[a-f0-9]{32}$/', $token ); |
| 2934 |
} |
| 2935 |
|
| 2936 |
|
| 2937 |
|
| 2938 |
|
| 2939 |
|
| 2940 |
|
| 2941 |
protected function confirmationTokenUrl( $token ) { |
| 2942 |
return $this->getTokenUrl( 'ConfirmEmail', $token ); |
| 2943 |
} |
| 2944 |
|
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
|
| 2950 |
protected function invalidationTokenUrl( $token ) { |
| 2951 |
return $this->getTokenUrl( 'InvalidateEmail', $token ); |
| 2952 |
} |
| 2953 |
|
| 2954 |
|
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
|
| 2964 |
|
| 2965 |
|
| 2966 |
|
| 2967 |
|
| 2968 |
protected function getTokenUrl( $page, $token ) { |
| 2969 |
|
| 2970 |
$title = Title::makeTitle( NS_MAIN, "Special:$page/$token" ); |
| 2971 |
return $title->getCanonicalURL(); |
| 2972 |
} |
| 2973 |
|
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
|
| 2978 |
|
| 2979 |
|
| 2980 |
|
| 2981 |
public function confirmEmail() { |
| 2982 |
|
| 2983 |
|
| 2984 |
if ( !$this->isEmailConfirmed() ) { |
| 2985 |
$this->setEmailAuthenticationTimestamp( wfTimestampNow() ); |
| 2986 |
$this->getHookRunner()->onConfirmEmailComplete( $this ); |
| 2987 |
} |
| 2988 |
return true; |
| 2989 |
} |
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
|
| 2998 |
public function invalidateEmail() { |
| 2999 |
$this->load(); |
| 3000 |
$this->mEmailToken = null; |
| 3001 |
$this->mEmailTokenExpires = null; |
| 3002 |
$this->setEmailAuthenticationTimestamp( null ); |
| 3003 |
$this->mEmail = ''; |
| 3004 |
$this->getHookRunner()->onInvalidateEmailComplete( $this ); |
| 3005 |
return true; |
| 3006 |
} |
| 3007 |
|
| 3008 |
|
| 3009 |
|
| 3010 |
|
| 3011 |
|
| 3012 |
public function setEmailAuthenticationTimestamp( $timestamp ) { |
| 3013 |
$this->load(); |
| 3014 |
$this->mEmailAuthenticated = $timestamp; |
| 3015 |
$this->getHookRunner()->onUserSetEmailAuthenticationTimestamp( |
| 3016 |
$this, $this->mEmailAuthenticated ); |
| 3017 |
} |
| 3018 |
|
| 3019 |
|
| 3020 |
|
| 3021 |
|
| 3022 |
|
| 3023 |
|
| 3024 |
|
| 3025 |
|
| 3026 |
public function canSendEmail() { |
| 3027 |
wfDeprecated( __METHOD__, '1.41' ); |
| 3028 |
$permError = MediaWikiServices::getInstance()->getEmailUserFactory() |
| 3029 |
->newEmailUser( $this->getThisAsAuthority() ) |
| 3030 |
->canSend(); |
| 3031 |
return $permError->isGood(); |
| 3032 |
} |
| 3033 |
|
| 3034 |
|
| 3035 |
|
| 3036 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
public function canReceiveEmail() { |
| 3040 |
$userOptionsLookup = MediaWikiServices::getInstance() |
| 3041 |
->getUserOptionsLookup(); |
| 3042 |
return $this->isEmailConfirmed() && !$userOptionsLookup->getOption( $this, 'disablemail' ); |
| 3043 |
} |
| 3044 |
|
| 3045 |
|
| 3046 |
|
| 3047 |
|
| 3048 |
|
| 3049 |
|
| 3050 |
|
| 3051 |
|
| 3052 |
|
| 3053 |
|
| 3054 |
|
| 3055 |
public function isEmailConfirmed(): bool { |
| 3056 |
$emailAuthentication = MediaWikiServices::getInstance()->getMainConfig() |
| 3057 |
->get( MainConfigNames::EmailAuthentication ); |
| 3058 |
$this->load(); |
| 3059 |
$confirmed = true; |
| 3060 |
if ( $this->getHookRunner()->onEmailConfirmed( $this, $confirmed ) ) { |
| 3061 |
return !$this->isAnon() && |
| 3062 |
Sanitizer::validateEmail( $this->getEmail() ) && |
| 3063 |
( !$emailAuthentication || $this->getEmailAuthenticationTimestamp() ); |
| 3064 |
} |
| 3065 |
|
| 3066 |
return $confirmed; |
| 3067 |
} |
| 3068 |
|
| 3069 |
|
| 3070 |
|
| 3071 |
|
| 3072 |
|
| 3073 |
public function isEmailConfirmationPending() { |
| 3074 |
$emailAuthentication = MediaWikiServices::getInstance()->getMainConfig() |
| 3075 |
->get( MainConfigNames::EmailAuthentication ); |
| 3076 |
return $emailAuthentication && |
| 3077 |
!$this->isEmailConfirmed() && |
| 3078 |
$this->mEmailToken && |
| 3079 |
$this->mEmailTokenExpires > wfTimestamp(); |
| 3080 |
} |
| 3081 |
|
| 3082 |
|
| 3083 |
|
| 3084 |
|
| 3085 |
|
| 3086 |
|
| 3087 |
|
| 3088 |
|
| 3089 |
public function getRegistration() { |
| 3090 |
if ( $this->isAnon() ) { |
| 3091 |
return false; |
| 3092 |
} |
| 3093 |
$this->load(); |
| 3094 |
return $this->mRegistration; |
| 3095 |
} |
| 3096 |
|
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
|
| 3104 |
public static function getRightDescription( $right ) { |
| 3105 |
$key = "right-$right"; |
| 3106 |
$msg = wfMessage( $key ); |
| 3107 |
return $msg->isDisabled() ? $right : $msg->text(); |
| 3108 |
} |
| 3109 |
|
| 3110 |
|
| 3111 |
|
| 3112 |
|
| 3113 |
|
| 3114 |
|
| 3115 |
|
| 3116 |
|
| 3117 |
public static function getRightDescriptionHtml( $right ) { |
| 3118 |
return wfMessage( "right-$right" )->parse(); |
| 3119 |
} |
| 3120 |
|
| 3121 |
|
| 3122 |
|
| 3123 |
|
| 3124 |
|
| 3125 |
|
| 3126 |
|
| 3127 |
|
| 3128 |
|
| 3129 |
|
| 3130 |
|
| 3131 |
|
| 3132 |
|
| 3133 |
|
| 3134 |
public static function getQueryInfo() { |
| 3135 |
$ret = [ |
| 3136 |
'tables' => [ 'user', 'user_actor' => 'actor' ], |
| 3137 |
'fields' => [ |
| 3138 |
'user_id', |
| 3139 |
'user_name', |
| 3140 |
'user_real_name', |
| 3141 |
'user_email', |
| 3142 |
'user_touched', |
| 3143 |
'user_token', |
| 3144 |
'user_email_authenticated', |
| 3145 |
'user_email_token', |
| 3146 |
'user_email_token_expires', |
| 3147 |
'user_registration', |
| 3148 |
'user_editcount', |
| 3149 |
'user_actor.actor_id', |
| 3150 |
], |
| 3151 |
'joins' => [ |
| 3152 |
'user_actor' => [ 'JOIN', 'user_actor.actor_user = user_id' ], |
| 3153 |
], |
| 3154 |
]; |
| 3155 |
|
| 3156 |
return $ret; |
| 3157 |
} |
| 3158 |
|
| 3159 |
|
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
public static function newQueryBuilder( IReadableDatabase $db ) { |
| 3169 |
return $db->newSelectQueryBuilder() |
| 3170 |
->select( [ |
| 3171 |
'user_id', |
| 3172 |
'user_name', |
| 3173 |
'user_real_name', |
| 3174 |
'user_email', |
| 3175 |
'user_touched', |
| 3176 |
'user_token', |
| 3177 |
'user_email_authenticated', |
| 3178 |
'user_email_token', |
| 3179 |
'user_email_token_expires', |
| 3180 |
'user_registration', |
| 3181 |
'user_editcount', |
| 3182 |
'user_actor.actor_id', |
| 3183 |
] ) |
| 3184 |
->from( 'user' ) |
| 3185 |
->join( 'actor', 'user_actor', 'user_actor.actor_user = user_id' ); |
| 3186 |
} |
| 3187 |
|
| 3188 |
|
| 3189 |
|
| 3190 |
|
| 3191 |
|
| 3192 |
|
| 3193 |
|
| 3194 |
|
| 3195 |
|
| 3196 |
|
| 3197 |
|
| 3198 |
public static function newFatalPermissionDeniedStatus( $permission ) { |
| 3199 |
return Status::wrap( MediaWikiServices::getInstance()->getPermissionManager() |
| 3200 |
->newFatalPermissionDeniedStatus( |
| 3201 |
$permission, |
| 3202 |
RequestContext::getMain() |
| 3203 |
) ); |
| 3204 |
} |
| 3205 |
|
| 3206 |
|
| 3207 |
|
| 3208 |
|
| 3209 |
|
| 3210 |
|
| 3211 |
|
| 3212 |
|
| 3213 |
|
| 3214 |
|
| 3215 |
public function getInstanceForUpdate() { |
| 3216 |
if ( !$this->getId() ) { |
| 3217 |
return null; |
| 3218 |
} |
| 3219 |
|
| 3220 |
$user = self::newFromId( $this->getId() ); |
| 3221 |
if ( !$user->loadFromId( IDBAccessObject::READ_EXCLUSIVE ) ) { |
| 3222 |
return null; |
| 3223 |
} |
| 3224 |
|
| 3225 |
return $user; |
| 3226 |
} |
| 3227 |
|
| 3228 |
|
| 3229 |
|
| 3230 |
|
| 3231 |
|
| 3232 |
|
| 3233 |
|
| 3234 |
|
| 3235 |
public function equals( ?UserIdentity $user ): bool { |
| 3236 |
if ( !$user ) { |
| 3237 |
return false; |
| 3238 |
} |
| 3239 |
|
| 3240 |
return $this->getName() === $user->getName(); |
| 3241 |
} |
| 3242 |
|
| 3243 |
|
| 3244 |
|
| 3245 |
|
| 3246 |
|
| 3247 |
|
| 3248 |
public function getUser(): UserIdentity { |
| 3249 |
return $this; |
| 3250 |
} |
| 3251 |
|
| 3252 |
|
| 3253 |
|
| 3254 |
|
| 3255 |
|
| 3256 |
|
| 3257 |
|
| 3258 |
|
| 3259 |
public function probablyCan( |
| 3260 |
string $action, |
| 3261 |
PageIdentity $target, |
| 3262 |
?PermissionStatus $status = null |
| 3263 |
): bool { |
| 3264 |
return $this->getThisAsAuthority()->probablyCan( $action, $target, $status ); |
| 3265 |
} |
| 3266 |
|
| 3267 |
|
| 3268 |
|
| 3269 |
|
| 3270 |
|
| 3271 |
|
| 3272 |
|
| 3273 |
|
| 3274 |
public function definitelyCan( |
| 3275 |
string $action, |
| 3276 |
PageIdentity $target, |
| 3277 |
?PermissionStatus $status = null |
| 3278 |
): bool { |
| 3279 |
return $this->getThisAsAuthority()->definitelyCan( $action, $target, $status ); |
| 3280 |
} |
| 3281 |
|
| 3282 |
|
| 3283 |
|
| 3284 |
|
| 3285 |
|
| 3286 |
|
| 3287 |
|
| 3288 |
|
| 3289 |
|
| 3290 |
public function isDefinitelyAllowed( string $action, ?PermissionStatus $status = null ): bool { |
| 3291 |
return $this->getThisAsAuthority()->isDefinitelyAllowed( $action, $status ); |
| 3292 |
} |
| 3293 |
|
| 3294 |
|
| 3295 |
|
| 3296 |
|
| 3297 |
|
| 3298 |
|
| 3299 |
|
| 3300 |
|
| 3301 |
|
| 3302 |
public function authorizeAction( string $action, ?PermissionStatus $status = null ): bool { |
| 3303 |
return $this->getThisAsAuthority()->authorizeAction( $action, $status ); |
| 3304 |
} |
| 3305 |
|
| 3306 |
|
| 3307 |
|
| 3308 |
|
| 3309 |
|
| 3310 |
|
| 3311 |
|
| 3312 |
|
| 3313 |
public function authorizeRead( |
| 3314 |
string $action, |
| 3315 |
PageIdentity $target, |
| 3316 |
?PermissionStatus $status = null |
| 3317 |
): bool { |
| 3318 |
return $this->getThisAsAuthority()->authorizeRead( $action, $target, $status ); |
| 3319 |
} |
| 3320 |
|
| 3321 |
|
| 3322 |
|
| 3323 |
|
| 3324 |
|
| 3325 |
|
| 3326 |
|
| 3327 |
|
| 3328 |
public function authorizeWrite( |
| 3329 |
string $action, PageIdentity $target, |
| 3330 |
?PermissionStatus $status = null |
| 3331 |
): bool { |
| 3332 |
return $this->getThisAsAuthority()->authorizeWrite( $action, $target, $status ); |
| 3333 |
} |
| 3334 |
|
| 3335 |
|
| 3336 |
|
| 3337 |
|
| 3338 |
|
| 3339 |
|
| 3340 |
private function getThisAsAuthority(): UserAuthority { |
| 3341 |
if ( !$this->mThisAsAuthority ) { |
| 3342 |
|
| 3343 |
|
| 3344 |
|
| 3345 |
|
| 3346 |
|
| 3347 |
|
| 3348 |
$request = $this->getRequest(); |
| 3349 |
$uiContext = RequestContext::getMain(); |
| 3350 |
|
| 3351 |
$services = MediaWikiServices::getInstance(); |
| 3352 |
$this->mThisAsAuthority = new UserAuthority( |
| 3353 |
$this, |
| 3354 |
$request, |
| 3355 |
$uiContext, |
| 3356 |
$services->getPermissionManager(), |
| 3357 |
$services->getRateLimiter(), |
| 3358 |
$services->getFormatterFactory()->getBlockErrorFormatter( $uiContext ) |
| 3359 |
); |
| 3360 |
} |
| 3361 |
|
| 3362 |
return $this->mThisAsAuthority; |
| 3363 |
} |
| 3364 |
|
| 3365 |
|
| 3366 |
|
| 3367 |
|
| 3368 |
|
| 3369 |
private function isGlobalSessionUser(): bool { |
| 3370 |
|
| 3371 |
|
| 3372 |
$sessionUser = RequestContext::getMain()->getUser(); |
| 3373 |
$globalUserName = $sessionUser->isSafeToLoad() |
| 3374 |
? $sessionUser->getName() |
| 3375 |
: IPUtils::sanitizeIP( $sessionUser->getRequest()->getIP() ); |
| 3376 |
|
| 3377 |
return $this->getName() === $globalUserName; |
| 3378 |
} |
| 3379 |
|
| 3380 |
|
| 3381 |
|
| 3382 |
|
| 3383 |
|
| 3384 |
|
| 3385 |
public function isTemp(): bool { |
| 3386 |
if ( $this->isTemp === null ) { |
| 3387 |
$this->isTemp = MediaWikiServices::getInstance()->getUserIdentityUtils() |
| 3388 |
->isTemp( $this ); |
| 3389 |
} |
| 3390 |
return $this->isTemp; |
| 3391 |
} |
| 3392 |
|
| 3393 |
|
| 3394 |
|
| 3395 |
|
| 3396 |
|
| 3397 |
|
| 3398 |
|
| 3399 |
public function isNamed(): bool { |
| 3400 |
return $this->isRegistered() && !$this->isTemp(); |
| 3401 |
} |
| 3402 |
} |
| 3403 |
|
| 3404 |
|
| 3405 |
class_alias( User::class, 'User' ); |