Spacing cleanup
[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 Use checkboxes or something, this list thing is incomprehensible to
8 * normal human beings.
9 */
10
11 /**
12 * A class to manage user levels rights.
13 * @addtogroup SpecialPage
14 */
15 class UserrightsPage extends SpecialPage {
16 # The target of the local right-adjuster's interest. Can be gotten from
17 # either a GET parameter or a subpage-style parameter, so have a member
18 # variable for it.
19 protected $mTarget;
20 protected $isself = false;
21
22 public function __construct() {
23 parent::__construct( 'Userrights' );
24 }
25
26 public function isRestricted() {
27 return true;
28 }
29
30 public function userCanExecute( $user ) {
31 $available = $this->changeableGroups();
32 return !empty( $available['add'] )
33 or !empty( $available['remove'] )
34 or ($this->isself and
35 (!empty( $available['add-self'] )
36 or !empty( $available['remove-self'] )));
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 * @param mixed $par String if any subpage provided, else null
44 */
45 function execute( $par ) {
46 // If the visitor doesn't have permissions to assign or remove
47 // any groups, it's a bit silly to give them the user search prompt.
48 global $wgUser, $wgRequest;
49
50 if( $par ) {
51 $this->mTarget = $par;
52 } else {
53 $this->mTarget = $wgRequest->getVal( 'user' );
54 }
55
56 if (!$this->mTarget) {
57 /*
58 * If the user specified no target, and they can only
59 * edit their own groups, automatically set them as the
60 * target.
61 */
62 $available = $this->changeableGroups();
63 if (empty($available['add']) && empty($available['remove']))
64 $this->mTarget = $wgUser->getName();
65 }
66
67 if ($this->mTarget == $wgUser->getName())
68 $this->isself = true;
69
70 if( !$this->userCanExecute( $wgUser ) ) {
71 // fixme... there may be intermediate groups we can mention.
72 global $wgOut;
73 $wgOut->showPermissionsErrorPage( array(
74 $wgUser->isAnon()
75 ? 'userrights-nologin'
76 : 'userrights-notallowed' ) );
77 return;
78 }
79
80 if ( wfReadOnly() ) {
81 global $wgOut;
82 $wgOut->readOnlyPage();
83 return;
84 }
85
86 $this->outputHeader();
87
88 $this->setHeaders();
89
90 // show the general form
91 $this->switchForm();
92
93 if( $wgRequest->wasPosted() ) {
94 // save settings
95 if( $wgRequest->getCheck( 'saveusergroups' ) ) {
96 $reason = $wgRequest->getVal( 'user-reason' );
97 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $this->mTarget ) ) {
98 $this->saveUserGroups(
99 $this->mTarget,
100 $wgRequest->getArray( 'removable' ),
101 $wgRequest->getArray( 'available' ),
102 $reason
103 );
104 }
105 }
106 }
107
108 // show some more forms
109 if( $this->mTarget ) {
110 $this->editUserGroupsForm( $this->mTarget );
111 }
112 }
113
114 /**
115 * Save user groups changes in the database.
116 * Data comes from the editUserGroupsForm() form function
117 *
118 * @param string $username Username to apply changes to.
119 * @param array $removegroup id of groups to be removed.
120 * @param array $addgroup id of groups to be added.
121 * @param string $reason Reason for group change
122 * @return null
123 */
124 function saveUserGroups( $username, $removegroup, $addgroup, $reason = '') {
125 global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
126
127 $user = $this->fetchUser( $username );
128 if( !$user ) {
129 return;
130 }
131
132 // Validate input set...
133 $changeable = $this->changeableGroups();
134 if ($wgUser->getId() != 0 && $wgUser->getId() == $user->getId()) {
135 $addable = array_merge($changeable['add'], $wgGroupsAddToSelf);
136 $removable = array_merge($changeable['remove'], $wgGroupsRemoveFromSelf);
137 } else {
138 $addable = $changeable['add'];
139 $removable = $changeable['remove'];
140 }
141
142 $removegroup = array_unique(
143 array_intersect( (array)$removegroup, $removable ) );
144 $addgroup = array_unique(
145 array_intersect( (array)$addgroup, $addable ) );
146
147 $oldGroups = $user->getGroups();
148 $newGroups = $oldGroups;
149 // remove then add groups
150 if( $removegroup ) {
151 $newGroups = array_diff($newGroups, $removegroup);
152 foreach( $removegroup as $group ) {
153 $user->removeGroup( $group );
154 }
155 }
156 if( $addgroup ) {
157 $newGroups = array_merge($newGroups, $addgroup);
158 foreach( $addgroup as $group ) {
159 $user->addGroup( $group );
160 }
161 }
162 $newGroups = array_unique( $newGroups );
163
164 // Ensure that caches are cleared
165 $user->invalidateCache();
166
167 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
168 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
169 if( $user instanceof User ) {
170 // hmmm
171 wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
172 }
173
174 if( $newGroups != $oldGroups ) {
175 $log = new LogPage( 'rights' );
176
177 global $wgRequest;
178 $log->addEntry( 'rights',
179 $user->getUserPage(),
180 $wgRequest->getText( 'user-reason' ),
181 array(
182 $this->makeGroupNameList( $oldGroups ),
183 $this->makeGroupNameList( $newGroups )
184 )
185 );
186 }
187 }
188
189 /**
190 * Edit user groups membership
191 * @param string $username Name of the user.
192 */
193 function editUserGroupsForm( $username ) {
194 global $wgOut;
195
196 $user = $this->fetchUser( $username );
197 if( !$user ) {
198 return;
199 }
200
201 $groups = $user->getGroups();
202
203 $this->showEditUserGroupsForm( $user, $groups );
204
205 // This isn't really ideal logging behavior, but let's not hide the
206 // interwiki logs if we're using them as is.
207 $this->showLogFragment( $user, $wgOut );
208 }
209
210 /**
211 * Normalize the input username, which may be local or remote, and
212 * return a user (or proxy) object for manipulating it.
213 *
214 * Side effects: error output for invalid access
215 * @return mixed User, UserRightsProxy, or null
216 */
217 function fetchUser( $username ) {
218 global $wgOut, $wgUser;
219
220 $parts = explode( '@', $username );
221 if( count( $parts ) < 2 ) {
222 $name = trim( $username );
223 $database = '';
224 } else {
225 list( $name, $database ) = array_map( 'trim', $parts );
226
227 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
228 $wgOut->addWikiMsg( 'userrights-no-interwiki' );
229 return null;
230 }
231 if( !UserRightsProxy::validDatabase( $database ) ) {
232 $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
233 return null;
234 }
235 }
236
237 if( $name == '' ) {
238 $wgOut->addWikiMsg( 'nouserspecified' );
239 return false;
240 }
241
242 if( $name{0} == '#' ) {
243 // Numeric ID can be specified...
244 // We'll do a lookup for the name internally.
245 $id = intval( substr( $name, 1 ) );
246
247 if( $database == '' ) {
248 $name = User::whoIs( $id );
249 } else {
250 $name = UserRightsProxy::whoIs( $database, $id );
251 }
252
253 if( !$name ) {
254 $wgOut->addWikiMsg( 'noname' );
255 return null;
256 }
257 }
258
259 if( $database == '' ) {
260 $user = User::newFromName( $name );
261 } else {
262 $user = UserRightsProxy::newFromName( $database, $name );
263 }
264
265 if( !$user || $user->isAnon() ) {
266 $wgOut->addWikiMsg( 'nosuchusershort', $username );
267 return null;
268 }
269
270 return $user;
271 }
272
273 function makeGroupNameList( $ids ) {
274 return implode( ', ', $ids );
275 }
276
277 /**
278 * Output a form to allow searching for a user
279 */
280 function switchForm() {
281 global $wgOut, $wgScript;
282 $wgOut->addHTML(
283 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
284 Xml::hidden( 'title', 'Special:Userrights' ) .
285 Xml::openElement( 'fieldset' ) .
286 Xml::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
287 Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget ) . ' ' .
288 Xml::submitButton( wfMsg( 'editusergroup' ) ) .
289 Xml::closeElement( 'fieldset' ) .
290 Xml::closeElement( 'form' ) . "\n"
291 );
292 }
293
294 /**
295 * Go through used and available groups and return the ones that this
296 * form will be able to manipulate based on the current user's system
297 * permissions.
298 *
299 * @param $groups Array: list of groups the given user is in
300 * @return Array: Tuple of addable, then removable groups
301 */
302 protected function splitGroups( $groups ) {
303 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
304 list($addable, $removable) = array_values( $this->changeableGroups() );
305
306 $removable = array_intersect(
307 array_merge($this->isself ? $wgGroupsRemoveFromSelf : array(), $removable),
308 $groups ); // Can't remove groups the user doesn't have
309 $addable = array_diff(
310 array_merge($this->isself ? $wgGroupsAddToSelf : array(), $addable),
311 $groups ); // Can't add groups the user does have
312
313 return array( $addable, $removable );
314 }
315
316 /**
317 * Show the form to edit group memberships.
318 *
319 * @param $user User or UserRightsProxy you're editing
320 * @param $groups Array: Array of groups the user is in
321 */
322 protected function showEditUserGroupsForm( $user, $groups ) {
323 global $wgOut, $wgUser;
324
325 list( $addable, $removable ) = $this->splitGroups( $groups );
326
327 $list = array();
328 foreach( $user->getGroups() as $group )
329 $list[] = self::buildGroupLink( $group );
330
331 $grouplist = '';
332 if( count( $list ) > 0 ) {
333 $grouplist = '<p>' . wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) . '</p>';
334 }
335 $wgOut->addHTML(
336 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
337 Xml::hidden( 'user', $this->mTarget ) .
338 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget ) ) .
339 Xml::openElement( 'fieldset' ) .
340 Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
341 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
342 $grouplist .
343 $this->explainRights() .
344 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
345 "<tr>
346 <td></td>
347 <td>" .
348 Xml::openElement( 'table', array( 'style' => 'width:400px;', 'id' => 'mw-userrights-table-inner' ) ) .
349 "<tr>
350 <td style='width:50%;'>" .
351 $this->removeSelect( $removable ) .
352 "</td>
353 <td style='width:50%;'>" .
354 $this->addSelect( $addable ) .
355 "</td>
356 </tr>" .
357 Xml::closeElement( 'table' ) .
358 "</tr>
359 <tr>
360 <td colspan='2'>" .
361 $wgOut->parse( wfMsg( 'userrights-groupshelp' ) ) .
362 "</td>
363 </tr>
364 <tr>
365 <td>" .
366 Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
367 "</td>
368 <td>" .
369 Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
370 "</td>
371 </tr>
372 <tr>
373 <td></td>
374 <td>" .
375 Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
376 "</td>
377 </tr>" .
378 Xml::closeElement( 'table' ) . "\n" .
379 Xml::closeElement( 'fieldset' ) .
380 Xml::closeElement( 'form' ) . "\n"
381 );
382 }
383
384 /**
385 * Format a link to a group description page
386 *
387 * @param string $group
388 * @return string
389 */
390 private static function buildGroupLink( $group ) {
391 static $cache = array();
392 if( !isset( $cache[$group] ) )
393 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
394 return $cache[$group];
395 }
396
397 /**
398 * Prepare a list of groups the user is able to add and remove
399 *
400 * @return string
401 */
402 private function explainRights() {
403 global $wgUser, $wgLang;
404
405 $out = array();
406 list( $add, $remove, $addself, $rmself ) = array_values( $this->changeableGroups() );
407
408 if( count( $add ) > 0 )
409 $out[] = wfMsgExt( 'userrights-available-add', 'parseinline',
410 $wgLang->listToText( $add ), count( $add ) );
411 if( count( $remove ) > 0 )
412 $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline',
413 $wgLang->listToText( $remove ), count( $add ) );
414 if( count( $addself ) > 0 )
415 $out[] = wfMsgExt( 'userrights-available-add-self', 'parseinline',
416 $wgLang->listToText( $addself ), count( $addself ) );
417 if( count( $rmself ) > 0 )
418 $out[] = wfMsgExt( 'userrights-available-remove-self', 'parseinline',
419 $wgLang->listToText( $rmself ), count( $rmself ) );
420
421 return count( $out ) > 0
422 ? implode( '<br />', $out )
423 : wfMsgExt( 'userrights-available-none', 'parseinline' );
424 }
425
426 /**
427 * Adds the <select> thingie where you can select what groups to remove
428 *
429 * @param array $groups The groups that can be removed
430 * @return string XHTML <select> element
431 */
432 private function removeSelect( $groups ) {
433 return $this->doSelect( $groups, 'removable' );
434 }
435
436 /**
437 * Adds the <select> thingie where you can select what groups to add
438 *
439 * @param array $groups The groups that can be added
440 * @return string XHTML <select> element
441 */
442 private function addSelect( $groups ) {
443 return $this->doSelect( $groups, 'available' );
444 }
445
446 /**
447 * Adds the <select> thingie where you can select what groups to add/remove
448 *
449 * @param array $groups The groups that can be added/removed
450 * @param string $name 'removable' or 'available'
451 * @return string XHTML <select> element
452 */
453 private function doSelect( $groups, $name ) {
454 $ret = wfMsgHtml( "{$this->mName}-groups$name" ) .
455 Xml::openElement( 'select', array(
456 'name' => "{$name}[]",
457 'multiple' => 'multiple',
458 'size' => '6',
459 'style' => 'width: 100%;'
460 )
461 );
462 foreach ($groups as $group) {
463 $ret .= Xml::element( 'option', array( 'value' => $group ), User::getGroupName( $group ) );
464 }
465 $ret .= Xml::closeElement( 'select' );
466 return $ret;
467 }
468
469 /**
470 * @param string $group The name of the group to check
471 * @return bool Can we remove the group?
472 */
473 private function canRemove( $group ) {
474 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
475 // PHP.
476 $groups = $this->changeableGroups();
477 return in_array( $group, $groups['remove'] );
478 }
479
480 /**
481 * @param string $group The name of the group to check
482 * @return bool Can we add the group?
483 */
484 private function canAdd( $group ) {
485 $groups = $this->changeableGroups();
486 return in_array( $group, $groups['add'] );
487 }
488
489 /**
490 * Returns an array of the groups that the user can add/remove.
491 *
492 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
493 */
494 function changeableGroups() {
495 global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
496
497 if( $wgUser->isAllowed( 'userrights' ) ) {
498 // This group gives the right to modify everything (reverse-
499 // compatibility with old "userrights lets you change
500 // everything")
501 // Using array_merge to make the groups reindexed
502 $all = array_merge( User::getAllGroups() );
503 return array(
504 'add' => $all,
505 'remove' => $all,
506 'add-self' => array(),
507 'remove-self' => array()
508 );
509 }
510
511 // Okay, it's not so simple, we will have to go through the arrays
512 $groups = array(
513 'add' => array(),
514 'remove' => array(),
515 'add-self' => $wgGroupsAddToSelf,
516 'remove-self' => $wgGroupsRemoveFromSelf);
517 $addergroups = $wgUser->getEffectiveGroups();
518
519 foreach ($addergroups as $addergroup) {
520 $groups = array_merge_recursive(
521 $groups, $this->changeableByGroup($addergroup)
522 );
523 $groups['add'] = array_unique( $groups['add'] );
524 $groups['remove'] = array_unique( $groups['remove'] );
525 }
526 return $groups;
527 }
528
529 /**
530 * Returns an array of the groups that a particular group can add/remove.
531 *
532 * @param String $group The group to check for whether it can add/remove
533 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
534 */
535 private function changeableByGroup( $group ) {
536 global $wgAddGroups, $wgRemoveGroups;
537
538 $groups = array( 'add' => array(), 'remove' => array() );
539 if( empty($wgAddGroups[$group]) ) {
540 // Don't add anything to $groups
541 } elseif( $wgAddGroups[$group] === true ) {
542 // You get everything
543 $groups['add'] = User::getAllGroups();
544 } elseif( is_array($wgAddGroups[$group]) ) {
545 $groups['add'] = $wgAddGroups[$group];
546 }
547
548 // Same thing for remove
549 if( empty($wgRemoveGroups[$group]) ) {
550 } elseif($wgRemoveGroups[$group] === true ) {
551 $groups['remove'] = User::getAllGroups();
552 } elseif( is_array($wgRemoveGroups[$group]) ) {
553 $groups['remove'] = $wgRemoveGroups[$group];
554 }
555 return $groups;
556 }
557
558 /**
559 * Show a rights log fragment for the specified user
560 *
561 * @param User $user User to show log for
562 * @param OutputPage $output OutputPage to use
563 */
564 protected function showLogFragment( $user, $output ) {
565 $viewer = new LogViewer(
566 new LogReader(
567 new FauxRequest(
568 array(
569 'type' => 'rights',
570 'page' => $user->getUserPage()->getPrefixedText(),
571 )
572 )
573 )
574 );
575 $output->addHtml( Xml::element( 'h2', null, LogPage::logName( 'rights' ) . "\n" ) );
576 $viewer->showList( $output );
577 }
578 }