Move initial connection checks into their own function.
[lhc/web/wiklou.git] / includes / SpecialUserrights.php
1 <?php
2
3 /**
4 * Special page to allow managing user group membership
5 *
6 * @addtogroup SpecialPage
7 * @todo This code is disgusting and needs a total rewrite
8 */
9
10 /** */
11 require_once( dirname(__FILE__) . '/HTMLForm.php');
12
13 /** Entry point */
14 function wfSpecialUserrights() {
15 global $wgRequest;
16 $form = new UserrightsForm($wgRequest);
17 $form->execute();
18 }
19
20 /**
21 * A class to manage user levels rights.
22 * @addtogroup SpecialPage
23 */
24 class UserrightsForm extends HTMLForm {
25 var $mPosted, $mRequest, $mSaveprefs;
26 /** Escaped local url name*/
27 var $action;
28
29 /** Constructor*/
30 public function __construct( &$request ) {
31 $this->mPosted = $request->wasPosted();
32 $this->mRequest =& $request;
33 $this->mName = 'userrights';
34
35 $titleObj = SpecialPage::getTitleFor( 'Userrights' );
36 $this->action = $titleObj->escapeLocalURL();
37 }
38
39 /**
40 * Manage forms to be shown according to posted data.
41 * Depending on the submit button used, call a form or a save function.
42 */
43 function execute() {
44 // show the general form
45 $this->switchForm();
46 if( $this->mPosted ) {
47 // show some more forms
48 if( $this->mRequest->getCheck( 'ssearchuser' ) ) {
49 $this->editUserGroupsForm( $this->mRequest->getVal( 'user-editname' ) );
50 }
51
52 // save settings
53 if( $this->mRequest->getCheck( 'saveusergroups' ) ) {
54 global $wgUser;
55 $username = $this->mRequest->getVal( 'user-editname' );
56 $reason = $this->mRequest->getVal( 'user-reason' );
57 if( $wgUser->matchEditToken( $this->mRequest->getVal( 'wpEditToken' ), $username ) ) {
58 $this->saveUserGroups( $username,
59 $this->mRequest->getArray( 'member' ),
60 $this->mRequest->getArray( 'available' ),
61 $reason );
62 }
63 }
64 }
65 }
66
67 /** Back-end for saveUserGroups()
68 * @param User $u
69 * @param array $removegroup
70 * @param array $addgroup
71 * @param string $reason
72 */
73
74 function doSaveUserGroups($u, $removegroup, $addgroup, $reason)
75 {
76 $oldGroups = $u->getGroups();
77 $newGroups = $oldGroups;
78 // remove then add groups
79 if(isset($removegroup)) {
80 $newGroups = array_diff($newGroups, $removegroup);
81 foreach( $removegroup as $group ) {
82 if ( $this->canRemove( $group ) ) {
83 $u->removeGroup( $group );
84 }
85 }
86 }
87 if(isset($addgroup)) {
88 $newGroups = array_merge($newGroups, $addgroup);
89 foreach( $addgroup as $group ) {
90 if ( $this->canAdd( $group ) ) {
91 $u->addGroup( $group );
92 }
93 }
94 }
95 $newGroups = array_unique( $newGroups );
96
97 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
98 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
99
100 wfRunHooks( 'UserRights', array( &$u, $addgroup, $removegroup ) );
101 $log = new LogPage( 'rights' );
102 $log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), $reason, array( $this->makeGroupNameList( $oldGroups ),
103 $this->makeGroupNameList( $newGroups ) ) );
104 }
105
106 /**
107 * Save user groups changes in the database.
108 * Data comes from the editUserGroupsForm() form function
109 *
110 * @param string $username Username to apply changes to.
111 * @param array $removegroup id of groups to be removed.
112 * @param array $addgroup id of groups to be added.
113 * @param string $reason Reason for group change
114 *
115 */
116 function saveUserGroups( $username, $removegroup, $addgroup, $reason = '' ) {
117 global $wgOut;
118 $u = User::newFromName($username);
119 if(is_null($u)) {
120 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
121 }
122 if($u->getID() == 0) {
123 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
124 }
125
126 $this->doSaveUserGroups($u, $removegroup, $addgroup, $reason);
127 }
128
129 function makeGroupNameList( $ids ) {
130 return implode( ', ', $ids );
131 }
132
133 /**
134 * Output a form to allow searching for a user
135 */
136 function switchForm() {
137 global $wgOut, $wgRequest;
138 $username = $wgRequest->getText( 'user-editname' );
139 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->action, 'name' => 'uluser' ) );
140 $form .= '<fieldset><legend>' . wfMsgHtml( 'userrights-lookup-user' ) . '</legend>';
141 $form .= '<p>' . Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user-editname', 'username', 30, $username ) . '</p>';
142 $form .= '<p>' . Xml::submitButton( wfMsg( 'editusergroup' ), array( 'name' => 'ssearchuser' ) ) . '</p>';
143 $form .= '</fieldset>';
144 $form .= '</form>';
145 $wgOut->addHTML( $form );
146 }
147
148 /**
149 * Edit user groups membership
150 * @param string $username Name of the user.
151 */
152 function editUserGroupsForm($username) {
153 global $wgOut;
154
155 $user = User::newFromName($username);
156 if( is_null( $user ) ) {
157 $wgOut->addWikiText( wfMsg( 'nouserspecified' ) );
158 return;
159 } elseif( $user->getID() == 0 ) {
160 $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
161 return;
162 }
163
164 $this->showEditUserGroupsForm( $username, $user->getGroups() );
165 $this->showLogFragment( $user, $wgOut );
166 }
167
168 /**
169 * Go through used and available groups and return the ones that this
170 * form will be able to manipulate based on the current user's system
171 * permissions.
172 *
173 * @param $groups Array: list of groups the given user is in
174 * @return Array: Tuple of addable, then removable groups
175 */
176 protected function splitGroups( $groups ) {
177 list($addable, $removable) = array_values( $this->changeableGroups() );
178 $removable = array_intersect($removable, $groups ); // Can't remove groups the user doesn't have
179 $addable = array_diff( $addable, $groups ); // Can't add groups the user does have
180
181 return array( $addable, $removable );
182 }
183
184 /**
185 * Show the form to edit group memberships.
186 *
187 * @todo make all CSS-y and semantic
188 * @param $username String: Name of user you're editing
189 * @param $groups Array: Array of groups the user is in
190 */
191 protected function showEditUserGroupsForm( $username, $groups ) {
192 global $wgOut, $wgUser;
193
194 list( $addable, $removable ) = $this->splitGroups( $groups );
195
196 $wgOut->addHTML(
197 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->action, 'name' => 'editGroup' ) ) .
198 Xml::hidden( 'user-editname', $username ) .
199 Xml::hidden( 'wpEditToken', $wgUser->editToken( $username ) ) .
200 Xml::openElement( 'fieldset' ) .
201 Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
202 $wgOut->parse( wfMsg( 'editinguser', $username ) ) .
203 $this->explainRights() .
204 "<table border='0'>
205 <tr>
206 <td></td>
207 <td>
208 <table width='400'>
209 <tr>
210 <td width='50%'>" . $this->removeSelect( $removable ) . "</td>
211 <td width='50%'>" . $this->addSelect( $addable ) . "</td>
212 </tr>
213 </table>
214 </tr>
215 <tr>
216 <td colspan='2'>" .
217 $wgOut->parse( wfMsg('userrights-groupshelp') ) .
218 "</td>
219 </tr>
220 <tr>
221 <td>" .
222 Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
223 "</td>
224 <td>" .
225 Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
226 "</td>
227 </tr>
228 <tr>
229 <td></td>
230 <td>" .
231 Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
232 "</td>
233 </tr>
234 </table>\n" .
235 Xml::closeElement( 'fieldset' ) .
236 Xml::closeElement( 'form' ) . "\n"
237 );
238 }
239
240 /**
241 * Prepare a list of groups the user is able to add and remove
242 *
243 * @return string
244 */
245 private function explainRights() {
246 global $wgUser, $wgLang;
247
248 $out = array();
249 list( $add, $remove ) = array_values( $this->changeableGroups() );
250
251 if( count( $add ) > 0 )
252 $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', $wgLang->listToText( $add ) );
253 if( count( $remove ) > 0 )
254 $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', $wgLang->listToText( $remove ) );
255
256 return count( $out ) > 0
257 ? implode( ' ', $out )
258 : wfMsgExt( 'userrights-available-none', 'parseinline' );
259 }
260
261 /**
262 * Adds the <select> thingie where you can select what groups to remove
263 *
264 * @param array $groups The groups that can be removed
265 * @return string XHTML <select> element
266 */
267 private function removeSelect( $groups ) {
268 return $this->doSelect( $groups, 'member' );
269 }
270
271 /**
272 * Adds the <select> thingie where you can select what groups to add
273 *
274 * @param array $groups The groups that can be added
275 * @return string XHTML <select> element
276 */
277 private function addSelect( $groups ) {
278 return $this->doSelect( $groups, 'available' );
279 }
280
281 /**
282 * Adds the <select> thingie where you can select what groups to add/remove
283 *
284 * @param array $groups The groups that can be added/removed
285 * @param string $name 'member' or 'available'
286 * @return string XHTML <select> element
287 */
288 private function doSelect( $groups, $name ) {
289 $ret = wfMsgHtml( "{$this->mName}-groups$name" ) .
290 Xml::openElement( 'select', array(
291 'name' => "{$name}[]",
292 'multiple' => 'multiple',
293 'size' => '6',
294 'style' => 'width: 100%;'
295 )
296 );
297 foreach ($groups as $group) {
298 $ret .= Xml::element( 'option', array( 'value' => $group ), User::getGroupName( $group ) );
299 }
300 $ret .= Xml::closeElement( 'select' );
301 return $ret;
302 }
303
304 /**
305 * @param string $group The name of the group to check
306 * @return bool Can we remove the group?
307 */
308 private function canRemove( $group ) {
309 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
310 // PHP.
311 $groups = $this->changeableGroups();
312 return in_array( $group, $groups['remove'] );
313 }
314
315 /**
316 * @param string $group The name of the group to check
317 * @return bool Can we add the group?
318 */
319 private function canAdd( $group ) {
320 $groups = $this->changeableGroups();
321 return in_array( $group, $groups['add'] );
322 }
323
324 /**
325 * Returns an array of the groups that the user can add/remove.
326 *
327 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
328 */
329 function changeableGroups() {
330 global $wgUser;
331
332 $groups = array( 'add' => array(), 'remove' => array() );
333 $addergroups = $wgUser->getEffectiveGroups();
334
335 foreach ($addergroups as $addergroup) {
336 $groups = array_merge_recursive(
337 $groups, $this->changeableByGroup($addergroup)
338 );
339 $groups['add'] = array_unique( $groups['add'] );
340 $groups['remove'] = array_unique( $groups['remove'] );
341 }
342 return $groups;
343 }
344
345 /**
346 * Returns an array of the groups that a particular group can add/remove.
347 *
348 * @param String $group The group to check for whether it can add/remove
349 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
350 */
351 private function changeableByGroup( $group ) {
352 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups;
353
354 if( empty($wgGroupPermissions[$group]['userrights']) ) {
355 // This group doesn't give the right to modify anything
356 return array( 'add' => array(), 'remove' => array() );
357 }
358 if( empty($wgAddGroups[$group]) and empty($wgRemoveGroups[$group]) ) {
359 // This group gives the right to modify everything (reverse-
360 // compatibility with old "userrights lets you change
361 // everything")
362 return array(
363 'add' => User::getAllGroups(),
364 'remove' => User::getAllGroups()
365 );
366 }
367
368 // Okay, it's not so simple, we have to go through the arrays
369 $groups = array( 'add' => array(), 'remove' => array() );
370 if( empty($wgAddGroups[$group]) ) {
371 // Don't add anything to $groups
372 } elseif( $wgAddGroups[$group] === true ) {
373 // You get everything
374 $groups['add'] = User::getAllGroups();
375 } elseif( is_array($wgAddGroups[$group]) ) {
376 $groups['add'] = $wgAddGroups[$group];
377 }
378
379 // Same thing for remove
380 if( empty($wgRemoveGroups[$group]) ) {
381 } elseif($wgRemoveGroups[$group] === true ) {
382 $groups['remove'] = User::getAllGroups();
383 } elseif( is_array($wgRemoveGroups[$group]) ) {
384 $groups['remove'] = $wgRemoveGroups[$group];
385 }
386 return $groups;
387 }
388
389 /**
390 * Show a rights log fragment for the specified user
391 *
392 * @param User $user User to show log for
393 * @param OutputPage $output OutputPage to use
394 */
395 protected function showLogFragment( $user, $output ) {
396 $viewer = new LogViewer(
397 new LogReader(
398 new FauxRequest(
399 array(
400 'type' => 'rights',
401 'page' => $user->getUserPage()->getPrefixedText(),
402 )
403 )
404 )
405 );
406 $output->addHtml( "<h2>" . htmlspecialchars( LogPage::logName( 'rights' ) ) . "</h2>\n" );
407 $viewer->showList( $output );
408 }
409
410 }