MediaWiki master
PermissionsError.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Exception;
22
24use MediaWiki\Debug\DeprecationHelper;
27
37
38 use DeprecationHelper;
39
40 private ?string $permission;
41 private PermissionStatus $status;
42
51 public function __construct( ?string $permission, $status = [] ) {
52 $this->deprecatePublicProperty( 'permission', '1.43' );
53 $this->deprecatePublicPropertyFallback( 'errors', '1.43',
54 function () {
55 return $this->status->toLegacyErrorArray();
56 },
57 function ( $errors ) {
58 $this->status = PermissionStatus::newEmpty();
59 foreach ( $errors as $error ) {
60 if ( is_array( $error ) ) {
61 // @phan-suppress-next-line PhanParamTooFewUnpack
62 $this->status->fatal( ...$error );
63 } else {
64 $this->status->fatal( $error );
65 }
66 }
67 }
68 );
69
70 if ( is_array( $status ) ) {
71 $errors = $status;
72 $status = PermissionStatus::newEmpty();
73 foreach ( $errors as $error ) {
74 if ( is_array( $error ) ) {
75 // @phan-suppress-next-line PhanParamTooFewUnpack
76 $status->fatal( ...$error );
77 } else {
78 $status->fatal( $error );
79 }
80 }
81 } elseif ( !( $status instanceof PermissionStatus ) ) {
82 throw new \InvalidArgumentException( __METHOD__ .
83 ': $status must be PermissionStatus or array, got ' . get_debug_type( $status ) );
84 }
85
86 if ( $permission === null && $status->isGood() ) {
87 throw new \InvalidArgumentException( __METHOD__ .
88 ': $permission and $status cannot both be empty' );
89 }
90
91 $this->permission = $permission;
92
93 if ( $status->isGood() ) {
95 ->getPermissionManager()
96 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Null on permission is check when used here
97 ->newFatalPermissionDeniedStatus( $this->permission, RequestContext::getMain() );
98 }
99
100 $this->status = $status;
101
102 // Give the parent class something to work with
103 parent::__construct( 'permissionserrors', $status->getMessages()[0] );
104 }
105
106 public function report( $action = self::SEND_OUTPUT ) {
107 global $wgOut;
108
109 $wgOut->showPermissionStatus( $this->status, $this->permission );
110 if ( $action === self::SEND_OUTPUT ) {
111 $wgOut->output();
112 }
113 }
114}
115
117class_alias( PermissionsError::class, 'PermissionsError' );
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgOut
Definition Setup.php:562
Group all the pieces relevant to the context of a request into one instance.
An error page which can definitely be safely rendered using the OutputPage.
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=[])
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
A StatusValue for permission errors.
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.