put in r110285 again now that 1.19 branched
[lhc/web/wiklou.git] / includes / api / ApiUserrights.php
1 <?php
2
3 /**
4 *
5 *
6 * Created on Mar 24, 2009
7 *
8 * Copyright © 2009 Roan Kattouw <Firstname>.<Lastname>@gmail.com
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 /**
29 * @ingroup API
30 */
31 class ApiUserrights extends ApiBase {
32
33 public function __construct( $main, $action ) {
34 parent::__construct( $main, $action );
35 }
36
37 private $mUser = null;
38
39 public function execute() {
40 $params = $this->extractRequestParams();
41
42 $user = $this->getUrUser();
43
44 $form = new UserrightsPage;
45 $r['user'] = $user->getName();
46 list( $r['added'], $r['removed'] ) =
47 $form->doSaveUserGroups(
48 $user, (array)$params['add'],
49 (array)$params['remove'], $params['reason'] );
50
51 $result = $this->getResult();
52 $result->setIndexedTagName( $r['added'], 'group' );
53 $result->setIndexedTagName( $r['removed'], 'group' );
54 $result->addValue( null, $this->getModuleName(), $r );
55 }
56
57 /**
58 * @return User
59 */
60 private function getUrUser() {
61 if ( $this->mUser !== null ) {
62 return $this->mUser;
63 }
64
65 $params = $this->extractRequestParams();
66
67 $form = new UserrightsPage;
68 $status = $form->fetchUser( $params['user'] );
69 if ( !$status->isOK() ) {
70 $errors = $status->getErrorsArray();
71 $this->dieUsageMsg( $errors[0] );
72 } else {
73 $user = $status->value;
74 }
75
76 $this->mUser = $user;
77 return $user;
78 }
79
80 public function mustBePosted() {
81 return true;
82 }
83
84 public function isWriteMode() {
85 return true;
86 }
87
88 public function getAllowedParams() {
89 return array (
90 'user' => array(
91 ApiBase::PARAM_TYPE => 'string',
92 ApiBase::PARAM_REQUIRED => true
93 ),
94 'add' => array(
95 ApiBase::PARAM_TYPE => User::getAllGroups(),
96 ApiBase::PARAM_ISMULTI => true
97 ),
98 'remove' => array(
99 ApiBase::PARAM_TYPE => User::getAllGroups(),
100 ApiBase::PARAM_ISMULTI => true
101 ),
102 'token' => null,
103 'reason' => array(
104 ApiBase::PARAM_DFLT => ''
105 )
106 );
107 }
108
109 public function getParamDescription() {
110 return array(
111 'user' => 'User name',
112 'add' => 'Add the user to these groups',
113 'remove' => 'Remove the user from these groups',
114 'token' => 'A userrights token previously retrieved through list=users',
115 'reason' => 'Reason for the change',
116 );
117 }
118
119 public function getDescription() {
120 return 'Add/remove a user to/from groups';
121 }
122
123 public function needsToken() {
124 return true;
125 }
126
127 public function getTokenSalt() {
128 return $this->getUrUser()->getName();
129 }
130
131 public function getExamples() {
132 return array(
133 'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
134 );
135 }
136
137 public function getHelpUrls() {
138 return 'https://www.mediawiki.org/wiki/API:User_group_membership';
139 }
140
141 public function getVersion() {
142 return __CLASS__ . ': $Id$';
143 }
144 }