Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / exception / PermissionsError.php
blob35fb57ec51f13272c16762ead67cdfca4b6e2b21
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Context\RequestContext;
22 use MediaWiki\Debug\DeprecationHelper;
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Permissions\PermissionStatus;
26 /**
27 * Show an error when a user tries to do something they do not have the necessary
28 * permissions for.
30 * @newable
31 * @since 1.18
32 * @ingroup Exception
34 class PermissionsError extends ErrorPageError {
36 use DeprecationHelper;
38 private ?string $permission;
39 private PermissionStatus $status;
41 /**
42 * @stable to call
44 * @param string|null $permission A permission name or null if unknown
45 * @param PermissionStatus|array $status PermissionStatus containing an array of errors,
46 * or an error array like in PermissionManager::getPermissionErrors();
47 * must not be empty if $permission is null
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();
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 );
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 );
79 } elseif ( !( $status instanceof PermissionStatus ) ) {
80 throw new \InvalidArgumentException( __METHOD__ .
81 ': $status must be PermissionStatus or array, got ' . get_debug_type( $status ) );
84 if ( $permission === null && $status->isGood() ) {
85 throw new \InvalidArgumentException( __METHOD__ .
86 ': $permission and $status cannot both be empty' );
89 $this->permission = $permission;
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() );
98 $this->status = $status;
100 // Give the parent class something to work with
101 parent::__construct( 'permissionserrors', $status->getMessages()[0] );
104 public function report( $action = self::SEND_OUTPUT ) {
105 global $wgOut;
107 $wgOut->showPermissionStatus( $this->status, $this->permission );
108 if ( $action === self::SEND_OUTPUT ) {
109 $wgOut->output();