]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiUserrights.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiUserrights.php
1 <?php
2
3 /**
4  * API for MediaWiki 1.8+
5  *
6  * Created on Mar 24, 2009
7  *
8  * Copyright © 2009 Roan Kattouw <Firstname>.<Lastname>@home.nl
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  * http://www.gnu.org/copyleft/gpl.html
24  *
25  * @file
26  */
27
28 if ( !defined( 'MEDIAWIKI' ) ) {
29         // Eclipse helper - will be ignored in production
30         require_once( "ApiBase.php" );
31 }
32
33 /**
34  * @ingroup API
35  */
36 class ApiUserrights extends ApiBase {
37
38         public function __construct( $main, $action ) {
39                 parent::__construct( $main, $action );
40         }
41
42         private $mUser = null;
43
44         public function execute() {
45                 $params = $this->extractRequestParams();
46
47                 $user = $this->getUser();
48
49                 $form = new UserrightsPage;
50                 $r['user'] = $user->getName();
51                 list( $r['added'], $r['removed'] ) =
52                         $form->doSaveUserGroups(
53                                 $user, (array)$params['add'],
54                                 (array)$params['remove'], $params['reason'] );
55
56                 $this->getResult()->setIndexedTagName( $r['added'], 'group' );
57                 $this->getResult()->setIndexedTagName( $r['removed'], 'group' );
58                 $this->getResult()->addValue( null, $this->getModuleName(), $r );
59         }
60
61         /**
62          * @return User
63          */
64         private function getUser() {
65                 if ( $this->mUser !== null ) {
66                         return $this->mUser;
67                 }
68
69                 $params = $this->extractRequestParams();
70
71                 $form = new UserrightsPage;
72                 $status = $form->fetchUser( $params['user'] );
73                 if ( !$status->isOK() ) {
74                         $errors = $status->getErrorsArray();
75                         $this->dieUsageMsg( $errors[0] );
76                 } else {
77                         $user = $status->value;
78                 }
79
80                 $this->mUser = $user;
81                 return $user;
82         }
83
84         public function mustBePosted() {
85                 return true;
86         }
87
88         public function isWriteMode() {
89                 return true;
90         }
91
92         public function getAllowedParams() {
93                 return array (
94                         'user' => array(
95                                 ApiBase::PARAM_TYPE => 'string',
96                                 ApiBase::PARAM_REQUIRED => true
97                         ),
98                         'add' => array(
99                                 ApiBase::PARAM_TYPE => User::getAllGroups(),
100                                 ApiBase::PARAM_ISMULTI => true
101                         ),
102                         'remove' => array(
103                                 ApiBase::PARAM_TYPE => User::getAllGroups(),
104                                 ApiBase::PARAM_ISMULTI => true
105                         ),
106                         'token' => null,
107                         'reason' => array(
108                                 ApiBase::PARAM_DFLT => ''
109                         )
110                 );
111         }
112
113         public function getParamDescription() {
114                 return array(
115                         'user' => 'User name',
116                         'add' => 'Add the user to these groups',
117                         'remove' => 'Remove the user from these groups',
118                         'token' => 'A userrights token previously retrieved through list=users',
119                         'reason' => 'Reason for the change',
120                 );
121         }
122
123         public function getDescription() {
124                 return 'Add/remove a user to/from groups';
125         }
126
127         public function needsToken() {
128                 return true;
129         }
130
131         public function getTokenSalt() {
132                 return $this->getUser()->getName();
133         }
134
135         protected function getExamples() {
136                 return array(
137                         'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
138                 );
139         }
140
141         public function getVersion() {
142                 return __CLASS__ . ': $Id$';
143         }
144 }