4 * API userrights module
6 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@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
29 class ApiUserrights
extends ApiBase
{
31 private $mUser = null;
34 * Get a UserrightsPage object, or subclass.
35 * @return UserrightsPage
37 protected function getUserRightsPage() {
38 return new UserrightsPage
;
42 * Get all available groups.
45 protected function getAllGroups() {
46 return User
::getAllGroups();
49 public function execute() {
50 $pUser = $this->getUser();
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 // @TODO Should the user be blocked from changing user rights if they
56 // are partially blocked?
57 $block = $pUser->getBlock();
59 $this->dieBlocked( $block );
63 $params = $this->extractRequestParams();
65 // Figure out expiry times from the input
66 // $params['expiry'] is not set in CentralAuth's ApiGlobalUserRights subclass
67 if ( isset( $params['expiry'] ) ) {
68 $expiry = (array)$params['expiry'];
70 $expiry = [ 'infinity' ];
72 $add = (array)$params['add'];
75 } elseif ( count( $expiry ) !== count( $add ) ) {
76 if ( count( $expiry ) === 1 ) {
77 $expiry = array_fill( 0, count( $add ), $expiry[0] );
79 $this->dieWithError( [
80 'apierror-toofewexpiries',
87 // Validate the expiries
89 foreach ( $expiry as $index => $expiryValue ) {
90 $group = $add[$index];
91 $groupExpiries[$group] = UserrightsPage
::expiryToTimestamp( $expiryValue );
93 if ( $groupExpiries[$group] === false ) {
94 $this->dieWithError( [ 'apierror-invalidexpiry', wfEscapeWikiText( $expiryValue ) ] );
97 // not allowed to have things expiring in the past
98 if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
99 $this->dieWithError( [ 'apierror-pastexpiry', wfEscapeWikiText( $expiryValue ) ] );
103 $user = $this->getUrUser( $params );
105 $tags = $params['tags'];
107 // Check if user can add tags
108 if ( $tags !== null ) {
109 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $tags, $pUser );
110 if ( !$ableToTag->isOK() ) {
111 $this->dieStatus( $ableToTag );
115 $form = $this->getUserRightsPage();
116 $form->setContext( $this->getContext() );
117 $r['user'] = $user->getName();
118 $r['userid'] = $user->getId();
119 list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
120 // Don't pass null to doSaveUserGroups() for array params, cast to empty array
121 $user, (array)$add, (array)$params['remove'],
122 $params['reason'], (array)$tags, $groupExpiries
125 $result = $this->getResult();
126 ApiResult
::setIndexedTagName( $r['added'], 'group' );
127 ApiResult
::setIndexedTagName( $r['removed'], 'group' );
128 $result->addValue( null, $this->getModuleName(), $r );
132 * @param array $params
135 private function getUrUser( array $params ) {
136 if ( $this->mUser
!== null ) {
140 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
142 $user = $params['user'] ??
'#' . $params['userid'];
144 $form = $this->getUserRightsPage();
145 $form->setContext( $this->getContext() );
146 $status = $form->fetchUser( $user );
147 if ( !$status->isOK() ) {
148 $this->dieStatus( $status );
151 $this->mUser
= $status->value
;
153 return $status->value
;
156 public function mustBePosted() {
160 public function isWriteMode() {
164 public function getAllowedParams() {
167 ApiBase
::PARAM_TYPE
=> 'user',
170 ApiBase
::PARAM_TYPE
=> 'integer',
173 ApiBase
::PARAM_TYPE
=> $this->getAllGroups(),
174 ApiBase
::PARAM_ISMULTI
=> true
177 ApiBase
::PARAM_ISMULTI
=> true,
178 ApiBase
::PARAM_ALLOW_DUPLICATES
=> true,
179 ApiBase
::PARAM_DFLT
=> 'infinite',
182 ApiBase
::PARAM_TYPE
=> $this->getAllGroups(),
183 ApiBase
::PARAM_ISMULTI
=> true
186 ApiBase
::PARAM_DFLT
=> ''
189 // Standard definition automatically inserted
190 ApiBase
::PARAM_HELP_MSG_APPEND
=> [ 'api-help-param-token-webui' ],
193 ApiBase
::PARAM_TYPE
=> 'tags',
194 ApiBase
::PARAM_ISMULTI
=> true
197 // CentralAuth's ApiGlobalUserRights subclass can't handle expiries
198 if ( !$this->getUserRightsPage()->canProcessExpiries() ) {
199 unset( $a['expiry'] );
204 public function needsToken() {
208 protected function getWebUITokenSalt( array $params ) {
209 return $this->getUrUser( $params )->getName();
212 protected function getExamplesMessages() {
214 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
215 => 'apihelp-userrights-example-user',
216 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
217 => 'apihelp-userrights-example-userid',
219 if ( $this->getUserRightsPage()->canProcessExpiries() ) {
220 $a['action=userrights&user=SometimeSysop&add=sysop&expiry=1%20month&token=123ABC']
221 = 'apihelp-userrights-example-expiry';
226 public function getHelpUrls() {
227 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:User_group_membership';