Merge "Set context when using UserrightsPage"
[mediawiki.git] / includes / api / ApiUserrights.php
blob870201e70363b6dc62f96c07d1ada8c308248caf
1 <?php
3 /**
6 * Created on Mar 24, 2009
8 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
28 /**
29 * @ingroup API
31 class ApiUserrights extends ApiBase {
33 private $mUser = null;
35 public function execute() {
36 $params = $this->extractRequestParams();
38 $user = $this->getUrUser();
40 $form = new UserrightsPage;
41 $form->setContext( $this->getContext() );
42 $r['user'] = $user->getName();
43 $r['userid'] = $user->getId();
44 list( $r['added'], $r['removed'] ) =
45 $form->doSaveUserGroups(
46 $user, (array)$params['add'],
47 (array)$params['remove'], $params['reason'] );
49 $result = $this->getResult();
50 $result->setIndexedTagName( $r['added'], 'group' );
51 $result->setIndexedTagName( $r['removed'], 'group' );
52 $result->addValue( null, $this->getModuleName(), $r );
55 /**
56 * @return User
58 private function getUrUser() {
59 if ( $this->mUser !== null ) {
60 return $this->mUser;
63 $params = $this->extractRequestParams();
65 $form = new UserrightsPage;
66 $form->setContext( $this->getContext() );
67 $status = $form->fetchUser( $params['user'] );
68 if ( !$status->isOK() ) {
69 $errors = $status->getErrorsArray();
70 $this->dieUsageMsg( $errors[0] );
71 } else {
72 $user = $status->value;
75 $this->mUser = $user;
76 return $user;
79 public function mustBePosted() {
80 return true;
83 public function isWriteMode() {
84 return true;
87 public function getAllowedParams() {
88 return array(
89 'user' => array(
90 ApiBase::PARAM_TYPE => 'string',
91 ApiBase::PARAM_REQUIRED => true
93 'add' => array(
94 ApiBase::PARAM_TYPE => User::getAllGroups(),
95 ApiBase::PARAM_ISMULTI => true
97 'remove' => array(
98 ApiBase::PARAM_TYPE => User::getAllGroups(),
99 ApiBase::PARAM_ISMULTI => true
101 'token' => array(
102 ApiBase::PARAM_TYPE => 'string',
103 ApiBase::PARAM_REQUIRED => true
105 'reason' => array(
106 ApiBase::PARAM_DFLT => ''
111 public function getParamDescription() {
112 return array(
113 'user' => 'User name',
114 'add' => 'Add the user to these groups',
115 'remove' => 'Remove the user from these groups',
116 'token' => 'A userrights token previously retrieved through list=users',
117 'reason' => 'Reason for the change',
121 public function getDescription() {
122 return 'Add/remove a user to/from groups';
125 public function needsToken() {
126 return true;
129 public function getTokenSalt() {
130 return $this->getUrUser()->getName();
133 public function getExamples() {
134 return array(
135 'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
139 public function getHelpUrls() {
140 return 'https://www.mediawiki.org/wiki/API:User_group_membership';