Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
84.21% |
32 / 38 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
PermissionsError | |
86.49% |
32 / 37 |
|
0.00% |
0 / 2 |
12.36 | |
0.00% |
0 / 1 |
__construct | |
94.12% |
32 / 34 |
|
0.00% |
0 / 1 |
10.02 | |||
report | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
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. |
7 | * |
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. |
12 | * |
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 |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Exception; |
22 | |
23 | use MediaWiki\Context\RequestContext; |
24 | use MediaWiki\Debug\DeprecationHelper; |
25 | use MediaWiki\MediaWikiServices; |
26 | use MediaWiki\Permissions\PermissionStatus; |
27 | |
28 | /** |
29 | * Show an error when a user tries to do something they do not have the necessary |
30 | * permissions for. |
31 | * |
32 | * @newable |
33 | * @since 1.18 |
34 | * @ingroup Exception |
35 | */ |
36 | class PermissionsError extends ErrorPageError { |
37 | |
38 | use DeprecationHelper; |
39 | |
40 | private ?string $permission; |
41 | private PermissionStatus $status; |
42 | |
43 | /** |
44 | * @stable to call |
45 | * |
46 | * @param string|null $permission A permission name or null if unknown |
47 | * @param PermissionStatus|array $status PermissionStatus containing an array of errors, |
48 | * or an error array like in PermissionManager::getPermissionErrors(); |
49 | * must not be empty if $permission is null |
50 | */ |
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() ) { |
94 | $status = MediaWikiServices::getInstance() |
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 | |
116 | /** @deprecated class alias since 1.44 */ |
117 | class_alias( PermissionsError::class, 'PermissionsError' ); |