4 * Created on Sep 11, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
33 class ApiChangeRights
extends ApiBase
{
35 public function __construct($main, $action) {
36 parent
:: __construct($main, $action);
39 public function execute() {
40 global $wgUser, $wgRequest;
41 $this->requestWriteMode();
44 $this->dieUsage('The wiki is in read-only mode', 'readonly');
45 $params = $this->extractRequestParams();
47 $ur = new UserrightsForm($wgRequest);
48 $allowed = $ur->changeableGroups();
51 if(is_null($params['user']))
52 $this->dieUsage('The user parameter must be set', 'nouser');
54 $uName = User
::getCanonicalName($params['user']);
55 $u = User
::newFromName($uName);
57 $this->dieUsage("Invalid username ``{$params['user']}''", 'invaliduser');
58 if($u->getId() == 0) // Anon or non-existent
59 $this->dieUsage("User ``{$params['user']}'' doesn't exist", 'nosuchuser');
61 $curgroups = $u->getGroups();
63 if($params['listgroups'])
65 $res['user'] = $uName;
66 $res['allowedgroups'] = $allowed;
67 $res['ingroups'] = $curgroups;
68 $this->getResult()->setIndexedTagName($res['ingroups'], 'group');
69 $this->getResult()->setIndexedTagName($res['allowedgroups']['add'], 'group');
70 $this->getResult()->setIndexedTagName($res['allowedgroups']['remove'], 'group');
73 if($params['gettoken'])
75 $res['changerightstoken'] = $wgUser->editToken($uName);
76 $this->getResult()->addValue(null, $this->getModuleName(), $res);
80 if(empty($params['addto']) && empty($params['rmfrom']))
81 $this->dieUsage('At least one of the addto and rmfrom parameters must be set', 'noaddrm');
82 if(is_null($params['token']))
83 $this->dieUsage('The token parameter must be set', 'notoken');
84 if(!$wgUser->matchEditToken($params['token'], $uName))
85 $this->dieUsage('Invalid token', 'badtoken');
87 if(!$wgUser->isAllowed('userrights'))
88 $this->dieUsage('You don\'t have permission to change users\' rights', 'permissiondenied');
90 // First let's remove redundant groups and check permissions while we're at it
91 if(is_null($params['addto']))
92 $params['addto'] = array();
94 foreach($params['addto'] as $g)
96 if(!in_array($g, $allowed['add']))
97 $this->dieUsage("You don't have permission to add to group ``$g''", 'cantadd');
98 if(!in_array($g, $curgroups))
102 if(is_null($params['rmfrom']))
103 $params['rmfrom'] = array();
105 foreach($params['rmfrom'] as $g)
107 if(!in_array($g, $allowed['remove']))
108 $this->dieUsage("You don't have permission to remove from group ``$g''", 'cantremove');
109 if(in_array($g, $curgroups))
112 $dbw = wfGetDb(DB_MASTER
);
114 $ur->doSaveUserGroups($u, $rmfrom, $addto, $params['reason']);
116 $res['user'] = $uName;
117 $res['addedto'] = $addto;
118 $res['removedfrom'] = $rmfrom;
119 $res['reason'] = $params['reason'];
121 $this->getResult()->setIndexedTagName($res['addedto'], 'group');
122 $this->getResult()->setIndexedTagName($res['removedfrom'], 'group');
123 $this->getResult()->addValue(null, $this->getModuleName(), $res);
126 protected function getAllowedParams() {
131 'listgroups' => false,
133 ApiBase
:: PARAM_ISMULTI
=> true,
136 ApiBase
:: PARAM_ISMULTI
=> true,
142 protected function getParamDescription() {
144 'user' => 'The user you want to add to or remove from groups.',
145 'token' => 'A changerights token previously obtained through the gettoken parameter.',
146 'gettoken' => 'Output a token. Note that the user parameter still has to be set.',
147 'listgroups' => 'List the groups the user is in, and the ones you can add them to and remove them from.',
148 'addto' => 'Pipe-separated list of groups to add this user to',
149 'rmfrom' => 'Pipe-separated list of groups to remove this user from',
150 'reason' => 'Reason for change (optional)'
154 protected function getDescription() {
156 'Add or remove a user from certain groups.'
160 protected function getExamples() {
162 'api.php?action=changerights&user=Bob&gettoken&listgroups',
163 'api.php?action=changerights&user=Bob&token=123ABC&addto=sysop&reason=Promoting%20per%20RFA'
167 public function getVersion() {
168 return __CLASS__
. ': $Id$';