Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.95% |
17 / 21 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
PermissionsError | |
80.95% |
17 / 21 |
|
0.00% |
0 / 2 |
9.56 | |
0.00% |
0 / 1 |
__construct | |
94.44% |
17 / 18 |
|
0.00% |
0 / 1 |
7.01 | |||
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 | use MediaWiki\Context\RequestContext; |
22 | use MediaWiki\MediaWikiServices; |
23 | use MediaWiki\Permissions\PermissionStatus; |
24 | use MediaWiki\User\UserGroupMembership; |
25 | |
26 | /** |
27 | * Show an error when a user tries to do something they do not have the necessary |
28 | * permissions for. |
29 | * |
30 | * @newable |
31 | * @since 1.18 |
32 | * @ingroup Exception |
33 | */ |
34 | class PermissionsError extends ErrorPageError { |
35 | public ?string $permission; |
36 | public array $errors; |
37 | |
38 | /** |
39 | * @stable to call |
40 | * |
41 | * @param string|null $permission A permission name or null if unknown |
42 | * @param array|PermissionStatus $errors Error message keys or [key, param...] arrays or |
43 | * PermissionStatus containing an array of errors; must not be empty if $permission is null |
44 | * @throws \InvalidArgumentException |
45 | */ |
46 | public function __construct( ?string $permission, $errors = [] ) { |
47 | if ( $errors instanceof PermissionStatus ) { |
48 | $errors = $errors->toLegacyErrorArray(); |
49 | } |
50 | |
51 | if ( $permission === null && !$errors ) { |
52 | throw new \InvalidArgumentException( __METHOD__ . |
53 | ': $permission and $errors cannot both be empty' ); |
54 | } |
55 | |
56 | $this->permission = $permission; |
57 | |
58 | if ( !count( $errors ) ) { |
59 | $groups = []; |
60 | foreach ( MediaWikiServices::getInstance() |
61 | ->getGroupPermissionsLookup() |
62 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Null on permission is check when used here |
63 | ->getGroupsWithPermission( $this->permission ) as $group |
64 | ) { |
65 | $groups[] = UserGroupMembership::getLinkWiki( $group, RequestContext::getMain() ); |
66 | } |
67 | |
68 | if ( $groups ) { |
69 | $errors[] = [ 'badaccess-groups', Message::listParam( $groups, 'comma' ), count( $groups ) ]; |
70 | } else { |
71 | $errors[] = [ 'badaccess-group0' ]; |
72 | } |
73 | } |
74 | |
75 | $this->errors = $errors; |
76 | |
77 | // Give the parent class something to work with |
78 | parent::__construct( 'permissionserrors', Message::newFromSpecifier( $errors[0] ) ); |
79 | } |
80 | |
81 | public function report( $action = self::SEND_OUTPUT ) { |
82 | global $wgOut; |
83 | |
84 | $wgOut->showPermissionsErrorPage( $this->errors, $this->permission ); |
85 | if ( $action === self::SEND_OUTPUT ) { |
86 | $wgOut->output(); |
87 | } |
88 | } |
89 | } |