Debian Codegraph @

includes/api/ApiUserrights.php

raw symbols SCIP
1 <?php
2
3 /**
4 * API userrights module
5 *
6 * Copyright © 2009 Roan Kattouw <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 namespace MediaWiki\Api;
27
28 use ChangeTags;
29 use MediaWiki\MainConfigNames;
30 use MediaWiki\ParamValidator\TypeDef\UserDef;
31 use MediaWiki\Specials\SpecialUserRights;
32 use MediaWiki\Title\Title;
33 use MediaWiki\User\Options\UserOptionsLookup;
34 use MediaWiki\User\UserGroupManager;
35 use MediaWiki\User\UserIdentity;
36 use MediaWiki\Watchlist\WatchedItemStoreInterface;
37 use MediaWiki\Watchlist\WatchlistManager;
38 use MediaWiki\WikiMap\WikiMap;
39 use Wikimedia\ParamValidator\ParamValidator;
40 use Wikimedia\ParamValidator\TypeDef\ExpiryDef;
41 use Wikimedia\Rdbms\IDBAccessObject;
42
43 /**
44 * @ingroup API
45 */
46 class ApiUserrights extends ApiBase {
47
48 use ApiWatchlistTrait;
49
50 /** @var UserIdentity|null */
51 private $mUser = null;
52
53 private UserGroupManager $userGroupManager;
54 private WatchedItemStoreInterface $watchedItemStore;
55
56 public function __construct(
57 ApiMain $mainModule,
58 string $moduleName,
59 UserGroupManager $userGroupManager,
60 WatchedItemStoreInterface $watchedItemStore,
61 WatchlistManager $watchlistManager,
62 UserOptionsLookup $userOptionsLookup
63 ) {
64 parent::__construct( $mainModule, $moduleName );
65 $this->userGroupManager = $userGroupManager;
66 $this->watchedItemStore = $watchedItemStore;
67
68 // Variables needed in ApiWatchlistTrait trait
69 $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
70 $this->watchlistMaxDuration =
71 $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
72 $this->watchlistManager = $watchlistManager;
73 $this->userOptionsLookup = $userOptionsLookup;
74 }
75
76 public function execute() {
77 $pUser = $this->getUser();
78
79 // Deny if the user is blocked and doesn't have the full 'userrights' permission.
80 // This matches what Special:UserRights does for the web UI.
81 if ( !$this->getAuthority()->isAllowed( 'userrights' ) ) {
82 $block = $pUser->getBlock( IDBAccessObject::READ_LATEST );
83 if ( $block && $block->isSitewide() ) {
84 $this->dieBlocked( $block );
85 }
86 }
87
88 $params = $this->extractRequestParams();
89
90 // Figure out expiry times from the input
91 $expiry = (array)$params['expiry'];
92 $add = (array)$params['add'];
93 if ( !$add ) {
94 $expiry = [];
95 } elseif ( count( $expiry ) !== count( $add ) ) {
96 if ( count( $expiry ) === 1 ) {
97 $expiry = array_fill( 0, count( $add ), $expiry[0] );
98 } else {
99 $this->dieWithError( [
100 'apierror-toofewexpiries',
101 count( $expiry ),
102 count( $add )
103 ] );
104 }
105 }
106
107 // Validate the expiries
108 $groupExpiries = [];
109 foreach ( $expiry as $index => $expiryValue ) {
110 $group = $add[$index];
111 $groupExpiries[$group] = SpecialUserRights::expiryToTimestamp( $expiryValue );
112
113 if ( $groupExpiries[$group] === false ) {
114 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiryValue ) ] );
115 }
116
117 // not allowed to have things expiring in the past
118 if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
119 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiryValue ) ] );
120 }
121 }
122
123 $user = $this->getUrUser( $params );
124
125 $tags = $params['tags'];
126
127 // Check if user can add tags
128 if ( $tags !== null ) {
129 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
130 if ( !$ableToTag->isOK() ) {
131 $this->dieStatus( $ableToTag );
132 }
133 }
134
135 $form = new SpecialUserRights();
136 $form->setContext( $this->getContext() );
137 $r = [];
138 $r['user'] = $user->getName();
139 $r['userid'] = $user->getId( $user->getWikiId() );
140 [ $r['added'], $r['removed'] ] = $form->doSaveUserGroups(
141 $user,
142 $add,
143 // Don't pass null to doSaveUserGroups() for array params, cast to empty array
144 (array)$params['remove'],
145 $params['reason'],
146 (array)$tags,
147 $groupExpiries
148 );
149
150 $watchlistExpiry = $this->getExpiryFromParams( $params );
151 $watchuser = $params['watchuser'];
152 $userPage = Title::makeTitle( NS_USER, $user->getName() );
153 if ( $watchuser && $user->getWikiId() === UserIdentity::LOCAL ) {
154 $this->setWatch( 'watch', $userPage, $this->getUser(), null, $watchlistExpiry );
155 } else {
156 $watchuser = false;
157 $watchlistExpiry = null;
158 }
159 $r['watchuser'] = $watchuser;
160 if ( $watchlistExpiry !== null ) {
161 $r['watchlistexpiry'] = $this->getWatchlistExpiry(
162 $this->watchedItemStore,
163 $userPage,
164 $this->getUser()
165 );
166 }
167
168 $result = $this->getResult();
169 ApiResult::setIndexedTagName( $r['added'], 'group' );
170 ApiResult::setIndexedTagName( $r['removed'], 'group' );
171 $result->addValue( null, $this->getModuleName(), $r );
172 }
173
174 /**
175 * @param array $params
176 * @return UserIdentity
177 */
178 private function getUrUser( array $params ) {
179 if ( $this->mUser !== null ) {
180 return $this->mUser;
181 }
182
183 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
184
185 $userDesignator = $params['user'] ?? '#' . $params['userid'];
186
187 // T422085: Check userrights-interwiki permission BEFORE looking up the user,
188 // to prevent user enumeration on private/remote wikis.
189 // This mirrors the fix applied to SpecialUserRights in Gerrit change I15306e63.
190 if ( isset( $params['user'] ) ) {
191 $interwikiDelimiter = $this->getConfig()->get( MainConfigNames::UserrightsInterwikiDelimiter );
192 if ( str_contains( $userDesignator, $interwikiDelimiter ) ) {
193 $targetParts = explode( $interwikiDelimiter, $userDesignator );
194 $remoteWikiId = $targetParts[1] ?? '';
195 if (
196 $remoteWikiId !== '' &&
197 !WikiMap::isCurrentWikiId( $remoteWikiId ) &&
198 !$this->getAuthority()->isAllowed( 'userrights-interwiki' )
199 ) {
200 $this->dieWithError( 'apierror-permissiondenied-userrights-interwiki', 'permissiondenied' );
201 }
202 }
203 }
204
205 $form = new SpecialUserRights();
206 $form->setContext( $this->getContext() );
207 $status = $form->fetchUser( $userDesignator );
208 if ( !$status->isOK() ) {
209 $this->dieStatus( $status );
210 }
211
212 $this->mUser = $status->value;
213
214 return $status->value;
215 }
216
217 public function mustBePosted() {
218 return true;
219 }
220
221 public function isWriteMode() {
222 return true;
223 }
224
225 public function getAllowedParams( $flags = 0 ) {
226 $allGroups = $this->userGroupManager->listAllGroups();
227
228 if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
229 sort( $allGroups );
230 }
231
232 $params = [
233 'user' => [
234 ParamValidator::PARAM_TYPE => 'user',
235 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
236 ],
237 'userid' => [
238 ParamValidator::PARAM_TYPE => 'integer',
239 ParamValidator::PARAM_DEPRECATED => true,
240 ],
241 'add' => [
242 ParamValidator::PARAM_TYPE => $allGroups,
243 ParamValidator::PARAM_ISMULTI => true
244 ],
245 'expiry' => [
246 ParamValidator::PARAM_ISMULTI => true,
247 ParamValidator::PARAM_ALLOW_DUPLICATES => true,
248 ParamValidator::PARAM_DEFAULT => 'infinite',
249 ],
250 'remove' => [
251 ParamValidator::PARAM_TYPE => $allGroups,
252 ParamValidator::PARAM_ISMULTI => true
253 ],
254 'reason' => [
255 ParamValidator::PARAM_DEFAULT => ''
256 ],
257 'token' => [
258 // Standard definition automatically inserted
259 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
260 ],
261 'tags' => [
262 ParamValidator::PARAM_TYPE => 'tags',
263 ParamValidator::PARAM_ISMULTI => true
264 ],
265 'watchuser' => false,
266 ];
267
268 // Params appear in the docs in the order they are defined,
269 // which is why this is here and not at the bottom.
270 // @todo Find better way to support insertion at arbitrary position
271 if ( $this->watchlistExpiryEnabled ) {
272 $params += [
273 'watchlistexpiry' => [
274 ParamValidator::PARAM_TYPE => 'expiry',
275 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
276 ExpiryDef::PARAM_USE_MAX => true,
277 ]
278 ];
279 }
280
281 return $params;
282 }
283
284 public function needsToken() {
285 return 'userrights';
286 }
287
288 protected function getWebUITokenSalt( array $params ) {
289 return $this->getUrUser( $params )->getName();
290 }
291
292 protected function getExamplesMessages() {
293 return [
294 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
295 => 'apihelp-userrights-example-user',
296 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
297 => 'apihelp-userrights-example-userid',
298 'action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC'
299 => 'apihelp-userrights-example-expiry',
300 ];
301 }
302
303 public function getHelpUrls() {
304 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';
305 }
306 }
307
308 /** @deprecated class alias since 1.43 */
309 class_alias( ApiUserrights::class, 'ApiUserrights' );