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