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