Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.50% |
28 / 32 |
|
75.00% |
12 / 16 |
CRAP | |
0.00% |
0 / 1 |
SimpleAuthority | |
87.50% |
28 / 32 |
|
75.00% |
12 / 16 |
26.22 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getBlock | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isAllowed | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isAllowedAny | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
isAllowedAll | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
checkPermission | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
probablyCan | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
definitelyCan | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isDefinitelyAllowed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
authorizeAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
authorizeRead | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
authorizeWrite | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isRegistered | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isTemp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isNamed | |
0.00% |
0 / 1 |
|
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\Permissions; |
22 | |
23 | use InvalidArgumentException; |
24 | use MediaWiki\Block\Block; |
25 | use MediaWiki\Page\PageIdentity; |
26 | use MediaWiki\User\UserIdentity; |
27 | use Wikimedia\Rdbms\IDBAccessObject; |
28 | |
29 | /** |
30 | * Represents an authority that has a specific set of permissions |
31 | * which are specified explicitly. This is useful for testing, but |
32 | * may also be used to represent a fixed set of permissions to be |
33 | * used in some context, e.g. in an asynchronous job. |
34 | * |
35 | * @since 1.36 |
36 | * @newable |
37 | */ |
38 | class SimpleAuthority implements Authority { |
39 | |
40 | /** @var UserIdentity */ |
41 | private $actor; |
42 | |
43 | /** @var bool */ |
44 | private $isTemp; |
45 | |
46 | /** @var true[] permissions (stored in the keys, values are ignored) */ |
47 | private $permissions; |
48 | |
49 | /** |
50 | * @stable to call |
51 | * @param UserIdentity $actor |
52 | * @param string[] $permissions A list of permissions to grant to the actor |
53 | * @param bool $isTemp Whether the user is auto-created (since 1.39) |
54 | */ |
55 | public function __construct( |
56 | UserIdentity $actor, |
57 | array $permissions, |
58 | bool $isTemp = false |
59 | ) { |
60 | $this->actor = $actor; |
61 | $this->isTemp = $isTemp; |
62 | $this->permissions = array_fill_keys( $permissions, true ); |
63 | } |
64 | |
65 | /** @inheritDoc */ |
66 | public function getUser(): UserIdentity { |
67 | return $this->actor; |
68 | } |
69 | |
70 | /** @inheritDoc */ |
71 | public function getBlock( int $freshness = IDBAccessObject::READ_NORMAL ): ?Block { |
72 | return null; |
73 | } |
74 | |
75 | /** @inheritDoc */ |
76 | public function isAllowed( string $permission, ?PermissionStatus $status = null ): bool { |
77 | return isset( $this->permissions[ $permission ] ); |
78 | } |
79 | |
80 | /** @inheritDoc */ |
81 | public function isAllowedAny( ...$permissions ): bool { |
82 | if ( !$permissions ) { |
83 | throw new InvalidArgumentException( 'At least one permission must be specified' ); |
84 | } |
85 | |
86 | foreach ( $permissions as $perm ) { |
87 | if ( $this->isAllowed( $perm ) ) { |
88 | return true; |
89 | } |
90 | } |
91 | |
92 | return false; |
93 | } |
94 | |
95 | /** @inheritDoc */ |
96 | public function isAllowedAll( ...$permissions ): bool { |
97 | if ( !$permissions ) { |
98 | throw new InvalidArgumentException( 'At least one permission must be specified' ); |
99 | } |
100 | |
101 | foreach ( $permissions as $perm ) { |
102 | if ( !$this->isAllowed( $perm ) ) { |
103 | return false; |
104 | } |
105 | } |
106 | |
107 | return true; |
108 | } |
109 | |
110 | private function checkPermission( string $permission, ?PermissionStatus $status ): bool { |
111 | $ok = $this->isAllowed( $permission ); |
112 | |
113 | if ( !$ok && $status ) { |
114 | // TODO: use a message that at includes the permission name |
115 | $status->fatal( 'permissionserrors' ); |
116 | $status->setPermission( $permission ); |
117 | } |
118 | |
119 | return $ok; |
120 | } |
121 | |
122 | /** @inheritDoc */ |
123 | public function probablyCan( |
124 | string $action, |
125 | PageIdentity $target, |
126 | ?PermissionStatus $status = null |
127 | ): bool { |
128 | return $this->checkPermission( $action, $status ); |
129 | } |
130 | |
131 | /** @inheritDoc */ |
132 | public function definitelyCan( |
133 | string $action, |
134 | PageIdentity $target, |
135 | ?PermissionStatus $status = null |
136 | ): bool { |
137 | return $this->checkPermission( $action, $status ); |
138 | } |
139 | |
140 | /** @inheritDoc */ |
141 | public function isDefinitelyAllowed( string $action, ?PermissionStatus $status = null ): bool { |
142 | return $this->checkPermission( $action, $status ); |
143 | } |
144 | |
145 | /** @inheritDoc */ |
146 | public function authorizeAction( string $action, ?PermissionStatus $status = null ): bool { |
147 | return $this->checkPermission( $action, $status ); |
148 | } |
149 | |
150 | /** @inheritDoc */ |
151 | public function authorizeRead( |
152 | string $action, |
153 | PageIdentity $target, |
154 | ?PermissionStatus $status = null |
155 | ): bool { |
156 | return $this->checkPermission( $action, $status ); |
157 | } |
158 | |
159 | /** @inheritDoc */ |
160 | public function authorizeWrite( |
161 | string $action, |
162 | PageIdentity $target, |
163 | ?PermissionStatus $status = null |
164 | ): bool { |
165 | return $this->checkPermission( $action, $status ); |
166 | } |
167 | |
168 | public function isRegistered(): bool { |
169 | return $this->actor->isRegistered(); |
170 | } |
171 | |
172 | public function isTemp(): bool { |
173 | return $this->isTemp; |
174 | } |
175 | |
176 | public function isNamed(): bool { |
177 | return $this->isRegistered() && !$this->isTemp(); |
178 | } |
179 | } |