* Refactoring ApiQueryImageInfo to use new File::loadHistory() interface. No change...
[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 * API module that facilitates the changing of user rights. The API eqivalent of
32 * Special:Userrights. Requires API write mode to be enabled.
33 *
34 * @addtogroup API
35 */
36 class ApiChangeRights extends ApiBase {
37
38 public function __construct($main, $action) {
39 parent :: __construct($main, $action);
40 }
41
42 public function execute() {
43 global $wgUser, $wgRequest;
44 $this->getMain()->requestWriteMode();
45
46 if(wfReadOnly())
47 $this->dieUsage('The wiki is in read-only mode', 'readonly');
48 $params = $this->extractRequestParams();
49
50 $ur = new UserrightsPage($wgRequest);
51 $allowed = $ur->changeableGroups();
52 $res = array();
53
54 $u = $ur->fetchUser_real($params['user']);
55 if(is_array($u))
56 switch($u[0])
57 {
58 case UserrightsPage::FETCHUSER_NO_INTERWIKI:
59 $this->dieUsage("You don't have permission to change users' rights on other wikis", 'nointerwiki');
60 case UserrightsPage::FETCHUSER_NO_DATABASE:
61 $this->dieUsage("Database ``{$u[1]}'' does not exist or is not local", 'nosuchdatabase');
62 case UserrightsPage::FETCHUSER_NO_USER:
63 $this->dieUsage("You specified an empty username, or none at all", 'emptyuser');
64 case UserrightsPage::FETCHUSER_NOSUCH_USERID:
65 $this->dieUsage("There is no user with ID ``{$u[1]}''", 'nosuchuserid');
66 case UserrightsPage::FETCHUSER_NOSUCH_USERNAME:
67 $this->dieUsage("There is no user with username ``{$u[1]}''", 'nosuchusername');
68 default:
69 $this->dieDebug(__METHOD__, "UserrightsPage::fetchUser_real() returned an unknown error ({$u[0]})");
70 }
71
72 $curgroups = $u->getGroups();
73 if($params['listgroups'])
74 {
75 $res['user'] = $u->getName();
76 $res['allowedgroups'] = $allowed;
77 $res['ingroups'] = $curgroups;
78 $this->getResult()->setIndexedTagName($res['ingroups'], 'group');
79 $this->getResult()->setIndexedTagName($res['allowedgroups']['add'], 'group');
80 $this->getResult()->setIndexedTagName($res['allowedgroups']['remove'], 'group');
81 }
82 ;
83 if($params['gettoken'])
84 {
85 $res['changerightstoken'] = $wgUser->editToken($u->getName());
86 $this->getResult()->addValue(null, $this->getModuleName(), $res);
87 return;
88 }
89
90 if(empty($params['addto']) && empty($params['rmfrom']))
91 $this->dieUsage('At least one of the addto and rmfrom parameters must be set', 'noaddrm');
92 if(is_null($params['token']))
93 $this->dieUsage('The token parameter must be set', 'notoken');
94 if(!$wgUser->matchEditToken($params['token'], $u->getName()))
95 $this->dieUsage('Invalid token', 'badtoken');
96
97 $dbw = wfGetDb(DB_MASTER);
98 $dbw->begin();
99 $ur->saveUserGroups($u, $params['rmfrom'], $params['addto'], $params['reason']);
100 $dbw->commit();
101 $res['user'] = $u->getName();
102 $res['addedto'] = (array)$params['addto'];
103 $res['removedfrom'] = (array)$params['rmfrom'];
104 $res['reason'] = $params['reason'];
105
106 $this->getResult()->setIndexedTagName($res['addedto'], 'group');
107 $this->getResult()->setIndexedTagName($res['removedfrom'], 'group');
108 $this->getResult()->addValue(null, $this->getModuleName(), $res);
109 }
110
111 protected function getAllowedParams() {
112 return array (
113 'user' => null,
114 'token' => null,
115 'gettoken' => false,
116 'listgroups' => false,
117 'addto' => array(
118 ApiBase :: PARAM_ISMULTI => true,
119 ),
120 'rmfrom' => array(
121 ApiBase :: PARAM_ISMULTI => true,
122 ),
123 'reason' => ''
124 );
125 }
126
127 protected function getParamDescription() {
128 return array (
129 'user' => 'The user you want to add to or remove from groups.',
130 'token' => 'A changerights token previously obtained through the gettoken parameter.',
131 'gettoken' => 'Output a token. Note that the user parameter still has to be set.',
132 'listgroups' => 'List the groups the user is in, and the ones you can add them to and remove them from.',
133 'addto' => 'Pipe-separated list of groups to add this user to',
134 'rmfrom' => 'Pipe-separated list of groups to remove this user from',
135 'reason' => 'Reason for change (optional)'
136 );
137 }
138
139 protected function getDescription() {
140 return array(
141 'Add or remove a user from certain groups.'
142 );
143 }
144
145 protected function getExamples() {
146 return array (
147 'api.php?action=changerights&user=Bob&gettoken&listgroups',
148 'api.php?action=changerights&user=Bob&token=123ABC&addto=sysop&reason=Promoting%20per%20RFA'
149 );
150 }
151
152 public function getVersion() {
153 return __CLASS__ . ': $Id: ApiChangeRights.php 28216 2007-12-06 18:33:18Z vasilievvv $';
154 }
155 }