Update git submodules
[mediawiki.git] / includes / api / ApiUserrights.php
blob6851772041055825a4f7acdbb30a1f8edeb90e31
1 <?php
3 /**
4 * API userrights module
6 * Copyright © 2009 Roan Kattouw <roan.kattouw@gmail.com>
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.
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.
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
23 * @file
26 use MediaWiki\MainConfigNames;
27 use MediaWiki\ParamValidator\TypeDef\UserDef;
28 use MediaWiki\Permissions\Authority;
29 use MediaWiki\Specials\SpecialUserRights;
30 use MediaWiki\Title\Title;
31 use MediaWiki\User\UserGroupManager;
32 use MediaWiki\User\UserIdentity;
33 use MediaWiki\User\UserOptionsLookup;
34 use MediaWiki\Watchlist\WatchlistManager;
35 use Wikimedia\ParamValidator\ParamValidator;
36 use Wikimedia\ParamValidator\TypeDef\ExpiryDef;
38 /**
39 * @ingroup API
41 class ApiUserrights extends ApiBase {
43 use ApiWatchlistTrait;
45 /** @var UserIdentity|null */
46 private $mUser = null;
48 private UserGroupManager $userGroupManager;
49 private WatchedItemStoreInterface $watchedItemStore;
51 /**
52 * @param ApiMain $mainModule
53 * @param string $moduleName
54 * @param UserGroupManager $userGroupManager
55 * @param WatchedItemStoreInterface $watchedItemStore
56 * @param WatchlistManager $watchlistManager
57 * @param UserOptionsLookup $userOptionsLookup
59 public function __construct(
60 ApiMain $mainModule,
61 $moduleName,
62 UserGroupManager $userGroupManager,
63 WatchedItemStoreInterface $watchedItemStore,
64 WatchlistManager $watchlistManager,
65 UserOptionsLookup $userOptionsLookup
66 ) {
67 parent::__construct( $mainModule, $moduleName );
68 $this->userGroupManager = $userGroupManager;
69 $this->watchedItemStore = $watchedItemStore;
71 // Variables needed in ApiWatchlistTrait trait
72 $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
73 $this->watchlistMaxDuration =
74 $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
75 $this->watchlistManager = $watchlistManager;
76 $this->userOptionsLookup = $userOptionsLookup;
79 public function execute() {
80 $pUser = $this->getUser();
82 // Deny if the user is blocked and doesn't have the full 'userrights' permission.
83 // This matches what Special:UserRights does for the web UI.
84 if ( !$this->getAuthority()->isAllowed( 'userrights' ) ) {
85 $block = $pUser->getBlock( Authority::READ_LATEST );
86 if ( $block && $block->isSitewide() ) {
87 $this->dieBlocked( $block );
91 $params = $this->extractRequestParams();
93 // Figure out expiry times from the input
94 $expiry = (array)$params['expiry'];
95 $add = (array)$params['add'];
96 if ( !$add ) {
97 $expiry = [];
98 } elseif ( count( $expiry ) !== count( $add ) ) {
99 if ( count( $expiry ) === 1 ) {
100 $expiry = array_fill( 0, count( $add ), $expiry[0] );
101 } else {
102 $this->dieWithError( [
103 'apierror-toofewexpiries',
104 count( $expiry ),
105 count( $add )
106 ] );
110 // Validate the expiries
111 $groupExpiries = [];
112 foreach ( $expiry as $index => $expiryValue ) {
113 $group = $add[$index];
114 $groupExpiries[$group] = SpecialUserRights::expiryToTimestamp( $expiryValue );
116 if ( $groupExpiries[$group] === false ) {
117 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiryValue ) ] );
120 // not allowed to have things expiring in the past
121 if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
122 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiryValue ) ] );
126 $user = $this->getUrUser( $params );
128 $tags = $params['tags'];
130 // Check if user can add tags
131 if ( $tags !== null ) {
132 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $this->getAuthority() );
133 if ( !$ableToTag->isOK() ) {
134 $this->dieStatus( $ableToTag );
138 $form = new SpecialUserRights();
139 $form->setContext( $this->getContext() );
140 $r = [];
141 $r['user'] = $user->getName();
142 $r['userid'] = $user->getId( $user->getWikiId() );
143 [ $r['added'], $r['removed'] ] = $form->doSaveUserGroups(
144 // Don't pass null to doSaveUserGroups() for array params, cast to empty array
145 $user, $add, (array)$params['remove'],
146 $params['reason'], (array)$tags, $groupExpiries
149 $watchlistExpiry = $this->getExpiryFromParams( $params );
150 $watchuser = $params['watchuser'];
151 $userPage = Title::makeTitle( NS_USER, $user->getName() );
152 if ( $watchuser && $user->getWikiId() === UserIdentity::LOCAL ) {
153 $this->setWatch( 'watch', $userPage, $this->getUser(), null, $watchlistExpiry );
154 } else {
155 $watchuser = false;
156 $watchlistExpiry = null;
158 $r['watchuser'] = $watchuser;
159 if ( $watchlistExpiry !== null ) {
160 $r['watchlistexpiry'] = $this->getWatchlistExpiry(
161 $this->watchedItemStore,
162 $userPage,
163 $this->getUser()
167 $result = $this->getResult();
168 ApiResult::setIndexedTagName( $r['added'], 'group' );
169 ApiResult::setIndexedTagName( $r['removed'], 'group' );
170 $result->addValue( null, $this->getModuleName(), $r );
174 * @param array $params
175 * @return UserIdentity
177 private function getUrUser( array $params ) {
178 if ( $this->mUser !== null ) {
179 return $this->mUser;
182 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
184 $user = $params['user'] ?? '#' . $params['userid'];
186 $form = new SpecialUserRights();
187 $form->setContext( $this->getContext() );
188 $status = $form->fetchUser( $user );
189 if ( !$status->isOK() ) {
190 $this->dieStatus( $status );
193 $this->mUser = $status->value;
195 return $status->value;
198 public function mustBePosted() {
199 return true;
202 public function isWriteMode() {
203 return true;
206 public function getAllowedParams( $flags = 0 ) {
207 $allGroups = $this->userGroupManager->listAllGroups();
209 if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
210 sort( $allGroups );
213 $params = [
214 'user' => [
215 ParamValidator::PARAM_TYPE => 'user',
216 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'id' ],
218 'userid' => [
219 ParamValidator::PARAM_TYPE => 'integer',
220 ParamValidator::PARAM_DEPRECATED => true,
222 'add' => [
223 ParamValidator::PARAM_TYPE => $allGroups,
224 ParamValidator::PARAM_ISMULTI => true
226 'expiry' => [
227 ParamValidator::PARAM_ISMULTI => true,
228 ParamValidator::PARAM_ALLOW_DUPLICATES => true,
229 ParamValidator::PARAM_DEFAULT => 'infinite',
231 'remove' => [
232 ParamValidator::PARAM_TYPE => $allGroups,
233 ParamValidator::PARAM_ISMULTI => true
235 'reason' => [
236 ParamValidator::PARAM_DEFAULT => ''
238 'token' => [
239 // Standard definition automatically inserted
240 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
242 'tags' => [
243 ParamValidator::PARAM_TYPE => 'tags',
244 ParamValidator::PARAM_ISMULTI => true
246 'watchuser' => false,
249 // Params appear in the docs in the order they are defined,
250 // which is why this is here and not at the bottom.
251 // @todo Find better way to support insertion at arbitrary position
252 if ( $this->watchlistExpiryEnabled ) {
253 $params += [
254 'watchlistexpiry' => [
255 ParamValidator::PARAM_TYPE => 'expiry',
256 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
257 ExpiryDef::PARAM_USE_MAX => true,
262 return $params;
265 public function needsToken() {
266 return 'userrights';
269 protected function getWebUITokenSalt( array $params ) {
270 return $this->getUrUser( $params )->getName();
273 protected function getExamplesMessages() {
274 return [
275 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
276 => 'apihelp-userrights-example-user',
277 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
278 => 'apihelp-userrights-example-userid',
279 'action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC'
280 => 'apihelp-userrights-example-expiry',
284 public function getHelpUrls() {
285 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';