Mixture of things.
[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 © 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 private $mUser = null;
40
41 public function execute() {
42 $params = $this->extractRequestParams();
43
44 $user = $this->getUser();
45
46 $form = new UserrightsPage;
47 $r['user'] = $user->getName();
48 list( $r['added'], $r['removed'] ) =
49 $form->doSaveUserGroups(
50 $user, (array)$params['add'],
51 (array)$params['remove'], $params['reason'] );
52
53 $this->getResult()->setIndexedTagName( $r['added'], 'group' );
54 $this->getResult()->setIndexedTagName( $r['removed'], 'group' );
55 $this->getResult()->addValue( null, $this->getModuleName(), $r );
56 }
57
58 private function getUser() {
59 if ( $this->mUser !== null ) {
60 return $this->mUser;
61 }
62
63 $params = $this->extractRequestParams();
64 if ( is_null( $params['user'] ) ) {
65 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
66 }
67
68 $form = new UserrightsPage;
69 $status = $form->fetchUser( $params['user'] );
70 if ( !$status->isOK() ) {
71 $errors = $status->getErrorsArray();
72 $this->dieUsageMsg( $errors[0] );
73 } else {
74 $user = $status->value;
75 }
76
77 $this->mUser = $user;
78 return $user;
79 }
80
81 public function mustBePosted() {
82 return true;
83 }
84
85 public function isWriteMode() {
86 return true;
87 }
88
89 public function getAllowedParams() {
90 return array (
91 'user' => null,
92 'add' => array(
93 ApiBase::PARAM_TYPE => User::getAllGroups(),
94 ApiBase::PARAM_ISMULTI => true
95 ),
96 'remove' => array(
97 ApiBase::PARAM_TYPE => User::getAllGroups(),
98 ApiBase::PARAM_ISMULTI => true
99 ),
100 'token' => null,
101 'reason' => array(
102 ApiBase::PARAM_DFLT => ''
103 )
104 );
105 }
106
107 public function getParamDescription() {
108 return array(
109 'user' => 'User name',
110 'add' => 'Add the user to these groups',
111 'remove' => 'Remove the user from these groups',
112 'token' => 'A userrights token previously retrieved through list=users',
113 'reason' => 'Reason for the change',
114 );
115 }
116
117 public function getDescription() {
118 return array(
119 'Add/remove a user to/from groups',
120 );
121 }
122
123 public function getPossibleErrors() {
124 return array_merge( parent::getPossibleErrors(), array(
125 array( 'missingparam', 'user' ),
126 ) );
127 }
128
129 public function getTokenSalt() {
130 return $this->getUser()->getName();
131 }
132
133 protected function getExamples() {
134 return array(
135 'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
136 );
137 }
138
139 public function getVersion() {
140 return __CLASS__ . ': $Id$';
141 }
142 }