MediaWiki master
PermissionsError.php
Go to the documentation of this file.
1<?php
22use MediaWiki\Debug\DeprecationHelper;
25
35
36 use DeprecationHelper;
37
38 private ?string $permission;
39 private PermissionStatus $status;
40
50 public function __construct( ?string $permission, $status = [] ) {
51 $this->deprecatePublicProperty( 'permission', '1.43' );
52 $this->deprecatePublicPropertyFallback( 'errors', '1.43',
53 function () {
54 return $this->status->toLegacyErrorArray();
55 },
56 function ( $errors ) {
57 $this->status = PermissionStatus::newEmpty();
58 foreach ( $errors as $error ) {
59 if ( is_array( $error ) ) {
60 // @phan-suppress-next-line PhanParamTooFewUnpack
61 $this->status->fatal( ...$error );
62 } else {
63 $this->status->fatal( $error );
64 }
65 }
66 }
67 );
68
69 if ( is_array( $status ) ) {
70 $errors = $status;
71 $status = PermissionStatus::newEmpty();
72 foreach ( $errors as $error ) {
73 if ( is_array( $error ) ) {
74 // @phan-suppress-next-line PhanParamTooFewUnpack
75 $status->fatal( ...$error );
76 } else {
77 $status->fatal( $error );
78 }
79 }
80 } elseif ( !( $status instanceof PermissionStatus ) ) {
81 throw new \InvalidArgumentException( __METHOD__ .
82 ': $status must be PermissionStatus or array, got ' . get_debug_type( $status ) );
83 }
84
85 if ( $permission === null && $status->isGood() ) {
86 throw new \InvalidArgumentException( __METHOD__ .
87 ': $permission and $status cannot both be empty' );
88 }
89
90 $this->permission = $permission;
91
92 if ( $status->isGood() ) {
93 $status = MediaWikiServices::getInstance()
94 ->getPermissionManager()
95 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Null on permission is check when used here
96 ->newFatalPermissionDeniedStatus( $this->permission, RequestContext::getMain() );
97 }
98
99 $this->status = $status;
100
101 // Give the parent class something to work with
102 parent::__construct( 'permissionserrors', $status->getMessages()[0] );
103 }
104
105 public function report( $action = self::SEND_OUTPUT ) {
106 global $wgOut;
107
108 $wgOut->showPermissionStatus( $this->status, $this->permission );
109 if ( $action === self::SEND_OUTPUT ) {
110 $wgOut->output();
111 }
112 }
113}
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgOut
Definition Setup.php:540
An error page which can definitely be safely rendered using the OutputPage.
Group all the pieces relevant to the context of a request into one instance.
Service locator for MediaWiki core services.
A StatusValue for permission errors.
Show an error when a user tries to do something they do not have the necessary permissions for.
report( $action=self::SEND_OUTPUT)
__construct(?string $permission, $status=[])
getMessages(?string $type=null)
Returns a list of error messages, optionally only those of the given type.
fatal( $message,... $parameters)
Add an error and set OK to false, indicating that the operation as a whole was fatal.
isGood()
Returns whether the operation completed and didn't have any error or warnings.