Added post-commit callback support to DB classes.
[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 $r['userid'] = $user->getId();
47 list( $r['added'], $r['removed'] ) =
48 $form->doSaveUserGroups(
49 $user, (array)$params['add'],
50 (array)$params['remove'], $params['reason'] );
51
52 $result = $this->getResult();
53 $result->setIndexedTagName( $r['added'], 'group' );
54 $result->setIndexedTagName( $r['removed'], 'group' );
55 $result->addValue( null, $this->getModuleName(), $r );
56 }
57
58 /**
59 * @return User
60 */
61 private function getUrUser() {
62 if ( $this->mUser !== null ) {
63 return $this->mUser;
64 }
65
66 $params = $this->extractRequestParams();
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' => array(
92 ApiBase::PARAM_TYPE => 'string',
93 ApiBase::PARAM_REQUIRED => true
94 ),
95 'add' => array(
96 ApiBase::PARAM_TYPE => User::getAllGroups(),
97 ApiBase::PARAM_ISMULTI => true
98 ),
99 'remove' => array(
100 ApiBase::PARAM_TYPE => User::getAllGroups(),
101 ApiBase::PARAM_ISMULTI => true
102 ),
103 'token' => array(
104 ApiBase::PARAM_TYPE => 'string',
105 ApiBase::PARAM_REQUIRED => true
106 ),
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->getUrUser()->getName();
133 }
134
135 public 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 getHelpUrls() {
142 return 'https://www.mediawiki.org/wiki/API:User_group_membership';
143 }
144
145 public function getVersion() {
146 return __CLASS__ . ': $Id$';
147 }
148 }