Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / api / ApiUserrights.php
1 <?php
2
3 /**
4 * API userrights module
5 *
6 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 /**
27 * @ingroup API
28 */
29 class ApiUserrights extends ApiBase {
30
31 private $mUser = null;
32
33 /**
34 * Get a UserrightsPage object, or subclass.
35 * @return UserrightsPage
36 */
37 protected function getUserRightsPage() {
38 return new UserrightsPage;
39 }
40
41 /**
42 * Get all available groups.
43 * @return array
44 */
45 protected function getAllGroups() {
46 return User::getAllGroups();
47 }
48
49 public function execute() {
50 $pUser = $this->getUser();
51
52 // Deny if the user is blocked and doesn't have the full 'userrights' permission.
53 // This matches what Special:UserRights does for the web UI.
54 if ( !$pUser->isAllowed( 'userrights' ) ) {
55 $block = $pUser->getBlock();
56 if ( $block && $block->isSitewide() ) {
57 $this->dieBlocked( $block );
58 }
59 }
60
61 $params = $this->extractRequestParams();
62
63 // Figure out expiry times from the input
64 // $params['expiry'] is not set in CentralAuth's ApiGlobalUserRights subclass
65 if ( isset( $params['expiry'] ) ) {
66 $expiry = (array)$params['expiry'];
67 } else {
68 $expiry = [ 'infinity' ];
69 }
70 $add = (array)$params['add'];
71 if ( !$add ) {
72 $expiry = [];
73 } elseif ( count( $expiry ) !== count( $add ) ) {
74 if ( count( $expiry ) === 1 ) {
75 $expiry = array_fill( 0, count( $add ), $expiry[0] );
76 } else {
77 $this->dieWithError( [
78 'apierror-toofewexpiries',
79 count( $expiry ),
80 count( $add )
81 ] );
82 }
83 }
84
85 // Validate the expiries
86 $groupExpiries = [];
87 foreach ( $expiry as $index => $expiryValue ) {
88 $group = $add[$index];
89 $groupExpiries[$group] = UserrightsPage::expiryToTimestamp( $expiryValue );
90
91 if ( $groupExpiries[$group] === false ) {
92 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiryValue ) ] );
93 }
94
95 // not allowed to have things expiring in the past
96 if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
97 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiryValue ) ] );
98 }
99 }
100
101 $user = $this->getUrUser( $params );
102
103 $tags = $params['tags'];
104
105 // Check if user can add tags
106 if ( $tags !== null ) {
107 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $pUser );
108 if ( !$ableToTag->isOK() ) {
109 $this->dieStatus( $ableToTag );
110 }
111 }
112
113 $form = $this->getUserRightsPage();
114 $form->setContext( $this->getContext() );
115 $r['user'] = $user->getName();
116 $r['userid'] = $user->getId();
117 list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
118 // Don't pass null to doSaveUserGroups() for array params, cast to empty array
119 $user, (array)$add, (array)$params['remove'],
120 $params['reason'], (array)$tags, $groupExpiries
121 );
122
123 $result = $this->getResult();
124 ApiResult::setIndexedTagName( $r['added'], 'group' );
125 ApiResult::setIndexedTagName( $r['removed'], 'group' );
126 $result->addValue( null, $this->getModuleName(), $r );
127 }
128
129 /**
130 * @param array $params
131 * @return User
132 */
133 private function getUrUser( array $params ) {
134 if ( $this->mUser !== null ) {
135 return $this->mUser;
136 }
137
138 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
139
140 $user = $params['user'] ?? '#' . $params['userid'];
141
142 $form = $this->getUserRightsPage();
143 $form->setContext( $this->getContext() );
144 $status = $form->fetchUser( $user );
145 if ( !$status->isOK() ) {
146 $this->dieStatus( $status );
147 }
148
149 $this->mUser = $status->value;
150
151 return $status->value;
152 }
153
154 public function mustBePosted() {
155 return true;
156 }
157
158 public function isWriteMode() {
159 return true;
160 }
161
162 public function getAllowedParams() {
163 $a = [
164 'user' => [
165 ApiBase::PARAM_TYPE => 'user',
166 ],
167 'userid' => [
168 ApiBase::PARAM_TYPE => 'integer',
169 ],
170 'add' => [
171 ApiBase::PARAM_TYPE => $this->getAllGroups(),
172 ApiBase::PARAM_ISMULTI => true
173 ],
174 'expiry' => [
175 ApiBase::PARAM_ISMULTI => true,
176 ApiBase::PARAM_ALLOW_DUPLICATES => true,
177 ApiBase::PARAM_DFLT => 'infinite',
178 ],
179 'remove' => [
180 ApiBase::PARAM_TYPE => $this->getAllGroups(),
181 ApiBase::PARAM_ISMULTI => true
182 ],
183 'reason' => [
184 ApiBase::PARAM_DFLT => ''
185 ],
186 'token' => [
187 // Standard definition automatically inserted
188 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
189 ],
190 'tags' => [
191 ApiBase::PARAM_TYPE => 'tags',
192 ApiBase::PARAM_ISMULTI => true
193 ],
194 ];
195 // CentralAuth's ApiGlobalUserRights subclass can't handle expiries
196 if ( !$this->getUserRightsPage()->canProcessExpiries() ) {
197 unset( $a['expiry'] );
198 }
199 return $a;
200 }
201
202 public function needsToken() {
203 return 'userrights';
204 }
205
206 protected function getWebUITokenSalt( array $params ) {
207 return $this->getUrUser( $params )->getName();
208 }
209
210 protected function getExamplesMessages() {
211 $a = [
212 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
213 => 'apihelp-userrights-example-user',
214 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
215 => 'apihelp-userrights-example-userid',
216 ];
217 if ( $this->getUserRightsPage()->canProcessExpiries() ) {
218 $a['action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC']
219 = 'apihelp-userrights-example-expiry';
220 }
221 return $a;
222 }
223
224 public function getHelpUrls() {
225 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';
226 }
227 }