c3ceb3457bfb19764cd4d16c98f7dda727524e31
[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 private $mUser = null;
34
35 public function execute() {
36 $params = $this->extractRequestParams();
37
38 $user = $this->getUrUser( $params );
39
40 $form = new UserrightsPage;
41 $form->setContext( $this->getContext() );
42 $r['user'] = $user->getName();
43 $r['userid'] = $user->getId();
44 list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
45 $user, (array)$params['add'],
46 (array)$params['remove'], $params['reason']
47 );
48
49 $result = $this->getResult();
50 $result->setIndexedTagName( $r['added'], 'group' );
51 $result->setIndexedTagName( $r['removed'], 'group' );
52 $result->addValue( null, $this->getModuleName(), $r );
53 }
54
55 /**
56 * @param array $params
57 * @return User
58 */
59 private function getUrUser( array $params ) {
60 if ( $this->mUser !== null ) {
61 return $this->mUser;
62 }
63
64 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
65
66 $user = isset( $params['user'] ) ? $params['user'] : '#' . $params['userid'];
67
68 $form = new UserrightsPage;
69 $form->setContext( $this->getContext() );
70 $status = $form->fetchUser( $user );
71 if ( !$status->isOK() ) {
72 $this->dieStatus( $status );
73 }
74
75 $this->mUser = $status->value;
76
77 return $status->value;
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 ),
93 'userid' => array(
94 ApiBase::PARAM_TYPE => 'integer',
95 ),
96 'add' => array(
97 ApiBase::PARAM_TYPE => User::getAllGroups(),
98 ApiBase::PARAM_ISMULTI => true
99 ),
100 'remove' => array(
101 ApiBase::PARAM_TYPE => User::getAllGroups(),
102 ApiBase::PARAM_ISMULTI => true
103 ),
104 'reason' => array(
105 ApiBase::PARAM_DFLT => ''
106 )
107 );
108 }
109
110 public function getParamDescription() {
111 return array(
112 'user' => 'User name',
113 'userid' => 'User id',
114 'add' => 'Add the user to these groups',
115 'remove' => 'Remove the user from these groups',
116 'token' => array(
117 /* Standard description automatically prepended */
118 'For compatibility, the token used in the web UI is also accepted.'
119 ),
120 'reason' => 'Reason for the change',
121 );
122 }
123
124 public function getDescription() {
125 return 'Add/remove a user to/from groups.';
126 }
127
128 public function needsToken() {
129 return 'userrights';
130 }
131
132 protected function getWebUITokenSalt( array $params ) {
133 return $this->getUrUser( $params )->getName();
134 }
135
136 public function getExamples() {
137 return array(
138 'api.php?action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC',
139 'api.php?action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
140 );
141 }
142
143 public function getHelpUrls() {
144 return 'https://www.mediawiki.org/wiki/API:User_group_membership';
145 }
146 }