Debian Codegraph @

includes/Permissions/PermissionStatus.php

raw symbols SCIP
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 ErrorPageError;
24 use MediaWiki\Block\Block;
25 use PermissionsError;
26 use StatusValue;
27 use ThrottledError;
28 use UserBlockedError;
29
30 /**
31 * A StatusValue for permission errors.
32 *
33 * @todo Add compat code for PermissionManager::getPermissionErrors
34 * and additional info about user blocks.
35 *
36 * @unstable
37 * @since 1.36
38 */
39 class PermissionStatus extends StatusValue {
40
41 /** @var ?Block */
42 private $block = null;
43
44 /** @var bool */
45 private $rateLimitExceeded = false;
46
47 /** @var ?string */
48 private $permission = null;
49
50 /**
51 * Returns the user block that contributed to permissions being denied,
52 * if such a block was provided via setBlock().
53 *
54 * This is intended to be used to provide additional information to the user that
55 * allows them to determine the reason for them being denied an action.
56 *
57 * @since 1.37
58 *
59 * @return ?Block
60 */
61 public function getBlock(): ?Block {
62 return $this->block;
63 }
64
65 /**
66 * Returns true when permissions were denied because the user is blocked.
67 *
68 * @since 1.41
69 *
70 * @return bool
71 */
72 public function isBlocked(): bool {
73 return $this->block !== null;
74 }
75
76 /**
77 * @since 1.37
78 * @internal
79 * @param Block $block
80 */
81 public function setBlock( Block $block ): void {
82 $this->block = $block;
83 $this->setOK( false );
84 }
85
86 /**
87 * @return static
88 */
89 public static function newEmpty() {
90 return new static();
91 }
92
93 /**
94 * Returns this permission status in legacy error array format.
95 *
96 * @deprecated since 1.43
97 * @see PermissionManager::getPermissionErrors()
98 *
99 * @return array[]
100 */
101 public function toLegacyErrorArray(): array {
102 return $this->getStatusArray();
103 }
104
105 /**
106 * Call this to indicate that the user is over the rate limit for some action.
107 * @since 1.41
108 * @internal
109 * Will cause isRateLimited() to return true.
110 */
111 public function setRateLimitExceeded() {
112 $this->rateLimitExceeded = true;
113 $this->fatal( 'actionthrottledtext' );
114 }
115
116 /**
117 * Whether the user is over the rate limit for some action.
118 * @since 1.41
119 * @return bool True if setRateLimitExceeded() was called.
120 */
121 public function isRateLimitExceeded(): bool {
122 return $this->rateLimitExceeded;
123 }
124
125 /**
126 * Sets the name of the permission that is being checked.
127 *
128 * @since 1.41
129 * @internal
130 * Will cause isRateLimited() to return true.
131 */
132 public function setPermission( string $permission ) {
133 $this->permission = $permission;
134 }
135
136 /**
137 * Returns the name of the permission that was being checked.
138 *
139 * @return string|null The permission, if known
140 * @since 1.41
141 */
142 public function getPermission(): ?string {
143 return $this->permission;
144 }
145
146 /**
147 * Will throw an appropriate ErrorPageError if isOK() returns false.
148 * If isOK() returns true, this method does nothing.
149 *
150 * This is a convenience method for use in user interaction code,
151 * such as subclasses of SpecialPage.
152 *
153 * @unstable Introduced in 1.41, but unstable since the future of ErrorPageError is unclear (T281935).
154 *
155 * @throws ErrorPageError
156 * @return void
157 */
158 public function throwErrorPageError(): void {
159 if ( $this->isOK() ) {
160 return;
161 }
162
163 $block = $this->getBlock();
164 if ( $block ) {
165 throw new UserBlockedError( $block );
166 }
167
168 if ( $this->isRateLimitExceeded() ) {
169 throw new ThrottledError();
170 }
171
172 $messages = $this->getStatusArray( 'error' );
173
174 throw new PermissionsError( $this->permission, $messages );
175 }
176
177 }