| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Permissions; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Block\Block; |
| 25 |
use MediaWiki\Block\BlockErrorFormatter; |
| 26 |
use MediaWiki\Context\IContextSource; |
| 27 |
use MediaWiki\Linker\LinkTarget; |
| 28 |
use MediaWiki\Page\PageIdentity; |
| 29 |
use MediaWiki\Request\WebRequest; |
| 30 |
use MediaWiki\Title\TitleValue; |
| 31 |
use MediaWiki\User\User; |
| 32 |
use MediaWiki\User\UserIdentity; |
| 33 |
use Wikimedia\Assert\Assert; |
| 34 |
use Wikimedia\DebugInfo\DebugInfoTrait; |
| 35 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
class UserAuthority implements Authority { |
| 52 |
|
| 53 |
use DebugInfoTrait; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
private $permissionManager; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private $rateLimiter; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private $actor; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private $userBlock = null; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
private $limitCache = []; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
private bool $useLimitCache; |
| 99 |
|
| 100 |
private WebRequest $request; |
| 101 |
private IContextSource $uiContext; |
| 102 |
private BlockErrorFormatter $blockErrorFormatter; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function __construct( |
| 113 |
User $user, |
| 114 |
WebRequest $request, |
| 115 |
IContextSource $uiContext, |
| 116 |
PermissionManager $permissionManager, |
| 117 |
RateLimiter $rateLimiter, |
| 118 |
BlockErrorFormatter $blockErrorFormatter |
| 119 |
) { |
| 120 |
$this->actor = $user; |
| 121 |
$this->request = $request; |
| 122 |
$this->uiContext = $uiContext; |
| 123 |
$this->permissionManager = $permissionManager; |
| 124 |
$this->rateLimiter = $rateLimiter; |
| 125 |
$this->blockErrorFormatter = $blockErrorFormatter; |
| 126 |
$this->useLimitCache = !defined( 'MW_PHPUNIT_TEST' ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function setUseLimitCache( bool $useLimitCache ) { |
| 134 |
$this->useLimitCache = $useLimitCache; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
public function getUser(): UserIdentity { |
| 139 |
return $this->actor; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
public function isAllowed( string $permission, ?PermissionStatus $status = null ): bool { |
| 144 |
return $this->internalAllowed( $permission, $status, false, null ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
public function isAllowedAny( ...$permissions ): bool { |
| 149 |
if ( !$permissions ) { |
| 150 |
throw new InvalidArgumentException( 'At least one permission must be specified' ); |
| 151 |
} |
| 152 |
|
| 153 |
return $this->permissionManager->userHasAnyRight( $this->actor, ...$permissions ); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
public function isAllowedAll( ...$permissions ): bool { |
| 158 |
if ( !$permissions ) { |
| 159 |
throw new InvalidArgumentException( 'At least one permission must be specified' ); |
| 160 |
} |
| 161 |
|
| 162 |
return $this->permissionManager->userHasAllRights( $this->actor, ...$permissions ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
public function probablyCan( |
| 167 |
string $action, |
| 168 |
PageIdentity $target, |
| 169 |
?PermissionStatus $status = null |
| 170 |
): bool { |
| 171 |
return $this->internalCan( |
| 172 |
PermissionManager::RIGOR_QUICK, |
| 173 |
$action, |
| 174 |
$target, |
| 175 |
$status, |
| 176 |
false |
| 177 |
); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
public function definitelyCan( |
| 182 |
string $action, |
| 183 |
PageIdentity $target, |
| 184 |
?PermissionStatus $status = null |
| 185 |
): bool { |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
return $this->internalCan( |
| 190 |
PermissionManager::RIGOR_FULL, |
| 191 |
$action, |
| 192 |
$target, |
| 193 |
$status, |
| 194 |
0 |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
public function isDefinitelyAllowed( string $action, ?PermissionStatus $status = null ): bool { |
| 200 |
$userBlock = $this->getApplicableBlock( PermissionManager::RIGOR_FULL, $action ); |
| 201 |
return $this->internalAllowed( $action, $status, 0, $userBlock ); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
public function authorizeAction( |
| 206 |
string $action, |
| 207 |
?PermissionStatus $status = null |
| 208 |
): bool { |
| 209 |
|
| 210 |
|
| 211 |
$userBlock = $this->getApplicableBlock( PermissionManager::RIGOR_SECURE, $action ); |
| 212 |
|
| 213 |
return $this->internalAllowed( |
| 214 |
$action, |
| 215 |
$status, |
| 216 |
1, |
| 217 |
$userBlock |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
public function authorizeRead( |
| 223 |
string $action, |
| 224 |
PageIdentity $target, |
| 225 |
?PermissionStatus $status = null |
| 226 |
): bool { |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
return $this->internalCan( |
| 233 |
PermissionManager::RIGOR_FULL, |
| 234 |
$action, |
| 235 |
$target, |
| 236 |
$status, |
| 237 |
1 |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
public function authorizeWrite( |
| 243 |
string $action, |
| 244 |
PageIdentity $target, |
| 245 |
?PermissionStatus $status = null |
| 246 |
): bool { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
return $this->internalCan( |
| 252 |
PermissionManager::RIGOR_SECURE, |
| 253 |
$action, |
| 254 |
$target, |
| 255 |
$status, |
| 256 |
1 |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
private function internalAllowed( |
| 273 |
string $action, |
| 274 |
?PermissionStatus $status, |
| 275 |
$limitRate, |
| 276 |
?Block $userBlock |
| 277 |
): bool { |
| 278 |
if ( $status ) { |
| 279 |
Assert::precondition( |
| 280 |
$status->isGood(), |
| 281 |
'The PermissionStatus passed as $status parameter must still be good' |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
if ( !$this->permissionManager->userHasRight( $this->actor, $action ) ) { |
| 286 |
if ( !$status ) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
$status->setPermission( $action ); |
| 291 |
$status->merge( |
| 292 |
$this->permissionManager->newFatalPermissionDeniedStatus( |
| 293 |
$action, |
| 294 |
$this->uiContext |
| 295 |
) |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
if ( $userBlock ) { |
| 300 |
if ( !$status ) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
$messages = $this->blockErrorFormatter->getMessages( |
| 305 |
$userBlock, |
| 306 |
$this->actor, |
| 307 |
$this->request->getIP() |
| 308 |
); |
| 309 |
|
| 310 |
$status->setPermission( $action ); |
| 311 |
foreach ( $messages as $message ) { |
| 312 |
$status->fatal( $message ); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
if ( $limitRate !== false ) { |
| 318 |
$isLimited = $this->limit( $action, $limitRate, $status ); |
| 319 |
if ( $isLimited && !$status ) { |
| 320 |
return false; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return !$status || $status->isOK(); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
private const BLOCK_CODES = [ |
| 329 |
'blockedtext', |
| 330 |
'blockedtext-partial', |
| 331 |
'autoblockedtext', |
| 332 |
'systemblockedtext', |
| 333 |
'blockedtext-composite', |
| 334 |
'blockedtext-tempuser', |
| 335 |
'autoblockedtext-tempuser', |
| 336 |
]; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
private function internalCan( |
| 349 |
string $rigor, |
| 350 |
string $action, |
| 351 |
PageIdentity $target, |
| 352 |
?PermissionStatus $status, |
| 353 |
$limitRate |
| 354 |
): bool { |
| 355 |
|
| 356 |
if ( $limitRate !== false ) { |
| 357 |
$isLimited = $this->limit( $action, $limitRate, $status ); |
| 358 |
if ( $isLimited && !$status ) { |
| 359 |
|
| 360 |
return false; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
if ( !( $target instanceof LinkTarget ) ) { |
| 365 |
|
| 366 |
$target = TitleValue::newFromPage( $target ); |
| 367 |
} |
| 368 |
|
| 369 |
if ( $status ) { |
| 370 |
$status->setPermission( $action ); |
| 371 |
|
| 372 |
$tempStatus = $this->permissionManager->getPermissionStatus( |
| 373 |
$action, |
| 374 |
$this->actor, |
| 375 |
$target, |
| 376 |
$rigor |
| 377 |
); |
| 378 |
|
| 379 |
if ( $tempStatus->isGood() ) { |
| 380 |
|
| 381 |
return $status->isOK(); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
foreach ( $tempStatus->getMessages() as $msg ) { |
| 389 |
$status->fatal( $msg ); |
| 390 |
} |
| 391 |
|
| 392 |
foreach ( self::BLOCK_CODES as $code ) { |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
if ( $tempStatus->hasMessage( $code ) ) { |
| 399 |
$block = $this->getBlock(); |
| 400 |
if ( $block ) { |
| 401 |
$status->setBlock( $block ); |
| 402 |
} |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
return $status->isOK(); |
| 408 |
} else { |
| 409 |
|
| 410 |
return $this->permissionManager->userCan( |
| 411 |
$action, |
| 412 |
$this->actor, |
| 413 |
$target, |
| 414 |
$rigor |
| 415 |
); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
public function limit( string $action, int $incrBy, ?PermissionStatus $status ): bool { |
| 432 |
$isLimited = null; |
| 433 |
|
| 434 |
if ( $this->useLimitCache && isset( $this->limitCache[ $action ] ) ) { |
| 435 |
|
| 436 |
$incrRemaining = $incrBy - $this->limitCache[ $action ][ 0 ]; |
| 437 |
|
| 438 |
|
| 439 |
if ( $incrRemaining < 1 ) { |
| 440 |
$isLimited = $this->limitCache[ $action ][ 1 ]; |
| 441 |
} |
| 442 |
} else { |
| 443 |
$incrRemaining = $incrBy; |
| 444 |
} |
| 445 |
|
| 446 |
if ( $isLimited === null ) { |
| 447 |
|
| 448 |
if ( $this->rateLimiter->isLimitable( $action ) ) { |
| 449 |
$isLimited = $this->rateLimiter->limit( |
| 450 |
$this->actor->toRateLimitSubject(), |
| 451 |
$action, |
| 452 |
$incrRemaining |
| 453 |
); |
| 454 |
} else { |
| 455 |
$isLimited = false; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
$this->limitCache[ $action ] = [ $incrBy, $isLimited ]; |
| 460 |
} |
| 461 |
|
| 462 |
if ( $isLimited && $status ) { |
| 463 |
$status->setRateLimitExceeded(); |
| 464 |
} |
| 465 |
|
| 466 |
return $isLimited; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
public function getBlock( int $freshness = IDBAccessObject::READ_NORMAL ): ?Block { |
| 471 |
|
| 472 |
if ( $this->userBlock === null || $freshness === IDBAccessObject::READ_LATEST ) { |
| 473 |
$this->userBlock = $this->actor->getBlock( $freshness ); |
| 474 |
|
| 475 |
|
| 476 |
$this->userBlock = $this->userBlock ?: false; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
return $this->userBlock ?: null; |
| 481 |
} |
| 482 |
|
| 483 |
private function getApplicableBlock( |
| 484 |
string $rigor, |
| 485 |
string $action, |
| 486 |
?PageIdentity $target = null |
| 487 |
): ?Block { |
| 488 |
|
| 489 |
|
| 490 |
return $this->permissionManager->getApplicableBlock( |
| 491 |
$action, |
| 492 |
$this->actor, |
| 493 |
$rigor, |
| 494 |
$target, |
| 495 |
$this->request |
| 496 |
); |
| 497 |
} |
| 498 |
|
| 499 |
public function isRegistered(): bool { |
| 500 |
return $this->actor->isRegistered(); |
| 501 |
} |
| 502 |
|
| 503 |
public function isTemp(): bool { |
| 504 |
return $this->actor->isTemp(); |
| 505 |
} |
| 506 |
|
| 507 |
public function isNamed(): bool { |
| 508 |
return $this->actor->isNamed(); |
| 509 |
} |
| 510 |
} |