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
49 public function __construct( ?string $permission, $status = [] ) {
50 $this->deprecatePublicProperty( 'permission', '1.43' );
51 $this->deprecatePublicPropertyFallback( 'errors', '1.43',
52 function () {
53 return $this->status->toLegacyErrorArray();
54 },
55 function ( $errors ) {
56 $this->status = PermissionStatus::newEmpty();
57 foreach ( $errors as $error ) {
58 if ( is_array( $error ) ) {
59 // @phan-suppress-next-line PhanParamTooFewUnpack
60 $this->status->fatal( ...$error );
61 } else {
62 $this->status->fatal( $error );
63 }
64 }
65 }
66 );
67
68 if ( is_array( $status ) ) {
69 $errors = $status;
70 $status = PermissionStatus::newEmpty();
71 foreach ( $errors as $error ) {
72 if ( is_array( $error ) ) {
73 // @phan-suppress-next-line PhanParamTooFewUnpack
74 $status->fatal( ...$error );
75 } else {
76 $status->fatal( $error );
77 }
78 }
79 } elseif ( !( $status instanceof PermissionStatus ) ) {
80 throw new \InvalidArgumentException( __METHOD__ .
81 ': $status must be PermissionStatus or array, got ' . get_debug_type( $status ) );
82 }
83
84 if ( $permission === null && $status->isGood() ) {
85 throw new \InvalidArgumentException( __METHOD__ .
86 ': $permission and $status cannot both be empty' );
87 }
88
89 $this->permission = $permission;
90
91 if ( $status->isGood() ) {
92 $status = MediaWikiServices::getInstance()
93 ->getPermissionManager()
94 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Null on permission is check when used here
95 ->newFatalPermissionDeniedStatus( $this->permission, RequestContext::getMain() );
96 }
97
98 $this->status = $status;
99
100 // Give the parent class something to work with
101 parent::__construct( 'permissionserrors', $status->getMessages()[0] );
102 }
103
104 public function report( $action = self::SEND_OUTPUT ) {
105 global $wgOut;
106
107 $wgOut->showPermissionStatus( $this->status, $this->permission );
108 if ( $action === self::SEND_OUTPUT ) {
109 $wgOut->output();
110 }
111 }
112}
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgOut
Definition Setup.php:572
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.