Removing double tabs inserted due to my editor's retarded tabsize=4 setting.
[lhc/web/wiklou.git] / includes / api / ApiChangeRights.php
1 <?php
2
3 /*
4 * Created on Sep 11, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
8 *
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.
13 *
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.
18 *
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
23 */
24
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
28 }
29
30 /**
31 * @addtogroup API
32 */
33 class ApiChangeRights extends ApiBase {
34
35 public function __construct($main, $action) {
36 parent :: __construct($main, $action);
37 }
38
39 public function execute() {
40 global $wgUser, $wgRequest;
41 $this->getMain()->requestWriteMode();
42
43 if(wfReadOnly())
44 $this->dieUsage('The wiki is in read-only mode', 'readonly');
45 $params = $this->extractRequestParams();
46
47 $ur = new UserrightsForm($wgRequest);
48 $allowed = $ur->changeableGroups();
49 $res = array();
50
51 if(is_null($params['user']))
52 $this->dieUsage('The user parameter must be set', 'nouser');
53
54 $uName = User::getCanonicalName($params['user']);
55 $u = User::newFromName($uName);
56 if(!$u)
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');
60
61 $curgroups = $u->getGroups();
62
63 if($params['listgroups'])
64 {
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');
71 }
72 ;
73 if($params['gettoken'])
74 {
75 $res['changerightstoken'] = $wgUser->editToken($uName);
76 $this->getResult()->addValue(null, $this->getModuleName(), $res);
77 return;
78 }
79
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');
86
87 if(!$wgUser->isAllowed('userrights'))
88 $this->dieUsage('You don\'t have permission to change users\' rights', 'permissiondenied');
89
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();
93 $addto = array();
94 foreach($params['addto'] as $g)
95 {
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))
99 $addto[] = $g;
100 }
101
102 if(is_null($params['rmfrom']))
103 $params['rmfrom'] = array();
104 $rmfrom = array();
105 foreach($params['rmfrom'] as $g)
106 {
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))
110 $rmfrom[] = $g;
111 }
112 $dbw = wfGetDb(DB_MASTER);
113 $dbw->begin();
114 $ur->doSaveUserGroups($u, $rmfrom, $addto, $params['reason']);
115 $dbw->commit();
116 $res['user'] = $uName;
117 $res['addedto'] = $addto;
118 $res['removedfrom'] = $rmfrom;
119 $res['reason'] = $params['reason'];
120
121 $this->getResult()->setIndexedTagName($res['addedto'], 'group');
122 $this->getResult()->setIndexedTagName($res['removedfrom'], 'group');
123 $this->getResult()->addValue(null, $this->getModuleName(), $res);
124 }
125
126 protected function getAllowedParams() {
127 return array (
128 'user' => null,
129 'token' => null,
130 'gettoken' => false,
131 'listgroups' => false,
132 'addto' => array(
133 ApiBase :: PARAM_ISMULTI => true,
134 ),
135 'rmfrom' => array(
136 ApiBase :: PARAM_ISMULTI => true,
137 ),
138 'reason' => ''
139 );
140 }
141
142 protected function getParamDescription() {
143 return array (
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)'
151 );
152 }
153
154 protected function getDescription() {
155 return array(
156 'Add or remove a user from certain groups.'
157 );
158 }
159
160 protected function getExamples() {
161 return array (
162 'api.php?action=changerights&user=Bob&gettoken&listgroups',
163 'api.php?action=changerights&user=Bob&token=123ABC&addto=sysop&reason=Promoting%20per%20RFA'
164 );
165 }
166
167 public function getVersion() {
168 return __CLASS__ . ': $Id$';
169 }
170 }