Refactor redundant attrib dropping into new method
[lhc/web/wiklou.git] / includes / api / ApiUserrights.php
1 <?php
2
3 /*
4 * Created on Mar 24, 2009
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2009 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 /**
32 * @ingroup API
33 */
34 class ApiUserrights extends ApiBase {
35
36 public function __construct($main, $action) {
37 parent :: __construct($main, $action);
38 }
39
40 public function execute() {
41 global $wgUser;
42 $params = $this->extractRequestParams();
43 if(is_null($params['user']))
44 $this->dieUsageMsg(array('missingparam', 'user'));
45 if(is_null($params['token']))
46 $this->dieUsageMsg(array('missingparam', 'token'));
47
48 $form = new UserrightsPage;
49 $user = $form->fetchUser($params['user']);
50 if($user instanceof WikiErrorMsg)
51 $this->dieUsageMsg(array_merge(
52 (array)$user->getMessageKey(),
53 $user->getMessageArgs()));
54 if(!$wgUser->matchEditToken($params['token'], $user->getName()))
55 $this->dieUsageMsg(array('sessionfailure'));
56
57 $r['user'] = $user->getName();
58 list($r['added'], $r['removed']) =
59 $form->doSaveUserGroups(
60 $user, (array)$params['add'],
61 (array)$params['remove'], $params['reason']);
62
63 $this->getResult()->setIndexedTagName($r['added'], 'group');
64 $this->getResult()->setIndexedTagName($r['removed'], 'group');
65 $this->getResult()->addValue(null, $this->getModuleName(), $r);
66 }
67
68 public function mustBePosted() {
69 return true;
70 }
71
72 public function isWriteMode() {
73 return true;
74 }
75
76 public function getAllowedParams() {
77 return array (
78 'user' => null,
79 'add' => array(
80 ApiBase :: PARAM_TYPE => User::getAllGroups(),
81 ApiBase :: PARAM_ISMULTI => true
82 ),
83 'remove' => array(
84 ApiBase :: PARAM_TYPE => User::getAllGroups(),
85 ApiBase :: PARAM_ISMULTI => true
86 ),
87 'token' => null,
88 'reason' => array(
89 ApiBase :: PARAM_DFLT => ''
90 )
91 );
92 }
93
94 public function getParamDescription() {
95 return array (
96 'user' => 'User name',
97 'add' => 'Add the user to these groups',
98 'remove' => 'Remove the user from these groups',
99 'token' => 'A userrights token previously retrieved through list=users',
100 'reason' => 'Reason for the change',
101 );
102 }
103
104 public function getDescription() {
105 return array(
106 'Add/remove a user to/from groups',
107 );
108 }
109
110 protected function getExamples() {
111 return array (
112 'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
113 );
114 }
115
116 public function getVersion() {
117 return __CLASS__ . ': $Id$';
118 }
119 }