MediaWiki master
AuthorizationConstraint.php
Go to the documentation of this file.
1<?php
8
13
21
22 public function __construct(
23 private readonly Authority $performer,
24 private readonly PageIdentity $target,
25 private readonly bool $new,
26 ) {
27 }
28
29 public function checkConstraint(): EditPageStatus {
30 $status = PermissionStatus::newEmpty();
31
32 if ( $this->new && !$this->performer->authorizeWrite( 'create', $this->target, $status ) ) {
33 return $this->castPermissionStatus( $status );
34 }
35
36 if ( !$this->performer->authorizeWrite( 'edit', $this->target, $status ) ) {
37 return $this->castPermissionStatus( $status );
38 }
39
40 return $this->castPermissionStatus( $status );
41 }
42
43 private function castPermissionStatus( PermissionStatus $status ): EditPageStatus {
44 if ( $status->isGood() ) {
45 return EditPageStatus::cast( $status );
46 }
47
48 // Report the most specific errors first
49 if ( $status->isBlocked() ) {
50 $value = self::AS_BLOCKED_PAGE_FOR_USER;
51 } elseif ( $status->isRateLimitExceeded() ) {
52 $value = self::AS_RATE_LIMITED;
53 } elseif ( $status->getPermission() === 'create' ) {
54 $value = self::AS_NO_CREATE_PERMISSION;
55 } elseif ( !$this->performer->isRegistered() ) {
56 $value = self::AS_READ_ONLY_PAGE_ANON;
57 } else {
58 $value = self::AS_READ_ONLY_PAGE_LOGGED;
59 }
60
61 return EditPageStatus::cast( $status )
62 ->setErrorFunction( $status->throwErrorPageError( ... ) )
63 ->setResult( false, $value );
64 }
65
66}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
Verify authorization to edit the page (user rights, rate limits, blocks).
__construct(private readonly Authority $performer, private readonly PageIdentity $target, private readonly bool $new,)
Abstract class for all constraints that can prevent edits.
Status returned by edit constraints and other page editing checks.
A StatusValue for permission errors.
static cast(StatusValue $sv)
Succinct helper method to wrap a StatusValue in some other specific subclass.
Interface for objects (potentially) representing an editable wiki page.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:23