Merge "Rewrite pref cleanup script"
[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->isBlocked() && !$pUser->isAllowed( 'userrights' ) ) {
55 $this->dieBlocked( $pUser->getBlock() );
56 }
57
58 $params = $this->extractRequestParams();
59
60 // Figure out expiry times from the input
61 // $params['expiry'] may not be set in subclasses
62 if ( isset( $params['expiry'] ) ) {
63 $expiry = (array)$params['expiry'];
64 } else {
65 $expiry = [ 'infinity' ];
66 }
67 $add = (array)$params['add'];
68 if ( count( $expiry ) !== count( $add ) ) {
69 if ( count( $expiry ) === 1 ) {
70 $expiry = array_fill( 0, count( $add ), $expiry[0] );
71 } else {
72 $this->dieWithError( [
73 'apierror-toofewexpiries',
74 count( $expiry ),
75 count( $add )
76 ] );
77 }
78 }
79
80 // Validate the expiries
81 $groupExpiries = [];
82 foreach ( $expiry as $index => $expiryValue ) {
83 $group = $add[$index];
84 $groupExpiries[$group] = UserrightsPage::expiryToTimestamp( $expiryValue );
85
86 if ( $groupExpiries[$group] === false ) {
87 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiryValue ) ] );
88 }
89
90 // not allowed to have things expiring in the past
91 if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
92 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiryValue ) ] );
93 }
94 }
95
96 $user = $this->getUrUser( $params );
97
98 $tags = $params['tags'];
99
100 // Check if user can add tags
101 if ( !is_null( $tags ) ) {
102 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $tags, $pUser );
103 if ( !$ableToTag->isOK() ) {
104 $this->dieStatus( $ableToTag );
105 }
106 }
107
108 $form = $this->getUserRightsPage();
109 $form->setContext( $this->getContext() );
110 $r['user'] = $user->getName();
111 $r['userid'] = $user->getId();
112 list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
113 $user, (array)$add, (array)$params['remove'],
114 $params['reason'], $tags, $groupExpiries
115 );
116
117 $result = $this->getResult();
118 ApiResult::setIndexedTagName( $r['added'], 'group' );
119 ApiResult::setIndexedTagName( $r['removed'], 'group' );
120 $result->addValue( null, $this->getModuleName(), $r );
121 }
122
123 /**
124 * @param array $params
125 * @return User
126 */
127 private function getUrUser( array $params ) {
128 if ( $this->mUser !== null ) {
129 return $this->mUser;
130 }
131
132 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
133
134 $user = isset( $params['user'] ) ? $params['user'] : '#' . $params['userid'];
135
136 $form = $this->getUserRightsPage();
137 $form->setContext( $this->getContext() );
138 $status = $form->fetchUser( $user );
139 if ( !$status->isOK() ) {
140 $this->dieStatus( $status );
141 }
142
143 $this->mUser = $status->value;
144
145 return $status->value;
146 }
147
148 public function mustBePosted() {
149 return true;
150 }
151
152 public function isWriteMode() {
153 return true;
154 }
155
156 public function getAllowedParams() {
157 $a = [
158 'user' => [
159 ApiBase::PARAM_TYPE => 'user',
160 ],
161 'userid' => [
162 ApiBase::PARAM_TYPE => 'integer',
163 ],
164 'add' => [
165 ApiBase::PARAM_TYPE => $this->getAllGroups(),
166 ApiBase::PARAM_ISMULTI => true
167 ],
168 'expiry' => [
169 ApiBase::PARAM_ISMULTI => true,
170 ApiBase::PARAM_ALLOW_DUPLICATES => true,
171 ApiBase::PARAM_DFLT => 'infinite',
172 ],
173 'remove' => [
174 ApiBase::PARAM_TYPE => $this->getAllGroups(),
175 ApiBase::PARAM_ISMULTI => true
176 ],
177 'reason' => [
178 ApiBase::PARAM_DFLT => ''
179 ],
180 'token' => [
181 // Standard definition automatically inserted
182 ApiBase::PARAM_HELP_MSG_APPEND => [ 'api-help-param-token-webui' ],
183 ],
184 'tags' => [
185 ApiBase::PARAM_TYPE => 'tags',
186 ApiBase::PARAM_ISMULTI => true
187 ],
188 ];
189 if ( !$this->getUserRightsPage()->canProcessExpiries() ) {
190 unset( $a['expiry'] );
191 }
192 return $a;
193 }
194
195 public function needsToken() {
196 return 'userrights';
197 }
198
199 protected function getWebUITokenSalt( array $params ) {
200 return $this->getUrUser( $params )->getName();
201 }
202
203 protected function getExamplesMessages() {
204 $a = [
205 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
206 => 'apihelp-userrights-example-user',
207 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
208 => 'apihelp-userrights-example-userid',
209 ];
210 if ( $this->getUserRightsPage()->canProcessExpiries() ) {
211 $a['action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC']
212 = 'apihelp-userrights-example-expiry';
213 }
214 return $a;
215 }
216
217 public function getHelpUrls() {
218 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';
219 }
220 }