Update messages.inc and rebuild MessagesEn.php
[mediawiki.git] / includes / SpecialUserrights.php
blob1873b48726e4a99714eeb2ac11db9440891748b9
1 <?php
2 /**
3 * Special page to allow managing user group membership
5 * @file
6 * @ingroup SpecialPage
7 */
9 /**
10 * A class to manage user levels rights.
11 * @ingroup SpecialPage
13 class UserrightsPage extends SpecialPage {
14 # The target of the local right-adjuster's interest. Can be gotten from
15 # either a GET parameter or a subpage-style parameter, so have a member
16 # variable for it.
17 protected $mTarget;
18 protected $isself = false;
20 public function __construct() {
21 parent::__construct( 'Userrights' );
24 public function isRestricted() {
25 return true;
28 public function userCanExecute( $user ) {
29 $available = $this->changeableGroups();
30 return !empty( $available['add'] )
31 or !empty( $available['remove'] )
32 or ($this->isself and
33 (!empty( $available['add-self'] )
34 or !empty( $available['remove-self'] )));
37 /**
38 * Manage forms to be shown according to posted data.
39 * Depending on the submit button used, call a form or a save function.
41 * @param mixed $par String if any subpage provided, else null
43 function execute( $par ) {
44 // If the visitor doesn't have permissions to assign or remove
45 // any groups, it's a bit silly to give them the user search prompt.
46 global $wgUser, $wgRequest;
48 if( $par ) {
49 $this->mTarget = $par;
50 } else {
51 $this->mTarget = $wgRequest->getVal( 'user' );
54 if (!$this->mTarget) {
56 * If the user specified no target, and they can only
57 * edit their own groups, automatically set them as the
58 * target.
60 $available = $this->changeableGroups();
61 if (empty($available['add']) && empty($available['remove']))
62 $this->mTarget = $wgUser->getName();
65 if ($this->mTarget == $wgUser->getName())
66 $this->isself = true;
68 if( !$this->userCanExecute( $wgUser ) ) {
69 // fixme... there may be intermediate groups we can mention.
70 global $wgOut;
71 $wgOut->showPermissionsErrorPage( array(
72 $wgUser->isAnon()
73 ? 'userrights-nologin'
74 : 'userrights-notallowed' ) );
75 return;
78 if ( wfReadOnly() ) {
79 global $wgOut;
80 $wgOut->readOnlyPage();
81 return;
84 $this->outputHeader();
86 $this->setHeaders();
88 // show the general form
89 $this->switchForm();
91 if( $wgRequest->wasPosted() ) {
92 // save settings
93 if( $wgRequest->getCheck( 'saveusergroups' ) ) {
94 $reason = $wgRequest->getVal( 'user-reason' );
95 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $this->mTarget ) ) {
96 $this->saveUserGroups(
97 $this->mTarget,
98 $reason
104 // show some more forms
105 if( $this->mTarget ) {
106 $this->editUserGroupsForm( $this->mTarget );
111 * Save user groups changes in the database.
112 * Data comes from the editUserGroupsForm() form function
114 * @param string $username Username to apply changes to.
115 * @param string $reason Reason for group change
116 * @return null
118 function saveUserGroups( $username, $reason = '') {
119 global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
121 $user = $this->fetchUser( $username );
122 if( !$user ) {
123 return;
126 $allgroups = $this->getAllGroups();
127 $addgroup = array();
128 $removegroup = array();
130 // This could possibly create a highly unlikely race condition if permissions are changed between
131 // when the form is loaded and when the form is saved. Ignoring it for the moment.
132 foreach ($allgroups as $group) {
133 // We'll tell it to remove all unchecked groups, and add all checked groups.
134 // Later on, this gets filtered for what can actually be removed
135 if ($wgRequest->getCheck( "wpGroup-$group" )) {
136 $addgroup[] = $group;
137 } else {
138 $removegroup[] = $group;
142 // Validate input set...
143 $changeable = $this->changeableGroups();
144 if ($wgUser->getId() != 0 && $wgUser->getId() == $user->getId()) {
145 $addable = array_merge($changeable['add'], $wgGroupsAddToSelf);
146 $removable = array_merge($changeable['remove'], $wgGroupsRemoveFromSelf);
147 } else {
148 $addable = $changeable['add'];
149 $removable = $changeable['remove'];
152 $removegroup = array_unique(
153 array_intersect( (array)$removegroup, $removable ) );
154 $addgroup = array_unique(
155 array_intersect( (array)$addgroup, $addable ) );
157 $oldGroups = $user->getGroups();
158 $newGroups = $oldGroups;
159 // remove then add groups
160 if( $removegroup ) {
161 $newGroups = array_diff($newGroups, $removegroup);
162 foreach( $removegroup as $group ) {
163 $user->removeGroup( $group );
166 if( $addgroup ) {
167 $newGroups = array_merge($newGroups, $addgroup);
168 foreach( $addgroup as $group ) {
169 $user->addGroup( $group );
172 $newGroups = array_unique( $newGroups );
174 // Ensure that caches are cleared
175 $user->invalidateCache();
177 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
178 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
179 if( $user instanceof User ) {
180 // hmmm
181 wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
184 if( $newGroups != $oldGroups ) {
185 $this->addLogEntry( $user, $oldGroups, $newGroups );
190 * Add a rights log entry for an action.
192 function addLogEntry( $user, $oldGroups, $newGroups ) {
193 global $wgRequest;
194 $log = new LogPage( 'rights' );
196 $log->addEntry( 'rights',
197 $user->getUserPage(),
198 $wgRequest->getText( 'user-reason' ),
199 array(
200 $this->makeGroupNameList( $oldGroups ),
201 $this->makeGroupNameList( $newGroups )
207 * Edit user groups membership
208 * @param string $username Name of the user.
210 function editUserGroupsForm( $username ) {
211 global $wgOut;
213 $user = $this->fetchUser( $username );
214 if( !$user ) {
215 return;
218 $groups = $user->getGroups();
220 $this->showEditUserGroupsForm( $user, $groups );
222 // This isn't really ideal logging behavior, but let's not hide the
223 // interwiki logs if we're using them as is.
224 $this->showLogFragment( $user, $wgOut );
228 * Normalize the input username, which may be local or remote, and
229 * return a user (or proxy) object for manipulating it.
231 * Side effects: error output for invalid access
232 * @return mixed User, UserRightsProxy, or null
234 function fetchUser( $username ) {
235 global $wgOut, $wgUser;
237 $parts = explode( '@', $username );
238 if( count( $parts ) < 2 ) {
239 $name = trim( $username );
240 $database = '';
241 } else {
242 list( $name, $database ) = array_map( 'trim', $parts );
244 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
245 $wgOut->addWikiMsg( 'userrights-no-interwiki' );
246 return null;
248 if( !UserRightsProxy::validDatabase( $database ) ) {
249 $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
250 return null;
254 if( $name == '' ) {
255 $wgOut->addWikiMsg( 'nouserspecified' );
256 return false;
259 if( $name{0} == '#' ) {
260 // Numeric ID can be specified...
261 // We'll do a lookup for the name internally.
262 $id = intval( substr( $name, 1 ) );
264 if( $database == '' ) {
265 $name = User::whoIs( $id );
266 } else {
267 $name = UserRightsProxy::whoIs( $database, $id );
270 if( !$name ) {
271 $wgOut->addWikiMsg( 'noname' );
272 return null;
276 if( $database == '' ) {
277 $user = User::newFromName( $name );
278 } else {
279 $user = UserRightsProxy::newFromName( $database, $name );
282 if( !$user || $user->isAnon() ) {
283 $wgOut->addWikiMsg( 'nosuchusershort', $username );
284 return null;
287 return $user;
290 function makeGroupNameList( $ids ) {
291 return implode( ', ', $ids );
295 * Output a form to allow searching for a user
297 function switchForm() {
298 global $wgOut, $wgScript;
299 $wgOut->addHTML(
300 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
301 Xml::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
302 Xml::openElement( 'fieldset' ) .
303 Xml::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
304 Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget ) . ' ' .
305 Xml::submitButton( wfMsg( 'editusergroup' ) ) .
306 Xml::closeElement( 'fieldset' ) .
307 Xml::closeElement( 'form' ) . "\n"
312 * Go through used and available groups and return the ones that this
313 * form will be able to manipulate based on the current user's system
314 * permissions.
316 * @param $groups Array: list of groups the given user is in
317 * @return Array: Tuple of addable, then removable groups
319 protected function splitGroups( $groups ) {
320 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
321 list($addable, $removable) = array_values( $this->changeableGroups() );
323 $removable = array_intersect(
324 array_merge($this->isself ? $wgGroupsRemoveFromSelf : array(), $removable),
325 $groups ); // Can't remove groups the user doesn't have
326 $addable = array_diff(
327 array_merge($this->isself ? $wgGroupsAddToSelf : array(), $addable),
328 $groups ); // Can't add groups the user does have
330 return array( $addable, $removable );
334 * Show the form to edit group memberships.
336 * @param $user User or UserRightsProxy you're editing
337 * @param $groups Array: Array of groups the user is in
339 protected function showEditUserGroupsForm( $user, $groups ) {
340 global $wgOut, $wgUser;
342 list( $addable, $removable ) = $this->splitGroups( $groups );
344 $list = array();
345 foreach( $user->getGroups() as $group )
346 $list[] = self::buildGroupLink( $group );
348 $grouplist = '';
349 if( count( $list ) > 0 ) {
350 $grouplist = Xml::tags( 'p', null, wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) );
352 $wgOut->addHTML(
353 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
354 Xml::hidden( 'user', $this->mTarget ) .
355 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget ) ) .
356 Xml::openElement( 'fieldset' ) .
357 Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
358 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
359 wfMsgExt( 'userrights-groups-help', array( 'parse' ) ) .
360 $grouplist .
361 Xml::tags( 'p', null, $this->groupCheckboxes( $groups ) ) .
362 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
363 "<tr>
364 <td class='mw-label'>" .
365 Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
366 "</td>
367 <td class='mw-input'>" .
368 Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
369 "</td>
370 </tr>
371 <tr>
372 <td></td>
373 <td class='mw-submit'>" .
374 Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
375 "</td>
376 </tr>" .
377 Xml::closeElement( 'table' ) . "\n" .
378 Xml::closeElement( 'fieldset' ) .
379 Xml::closeElement( 'form' ) . "\n"
384 * Format a link to a group description page
386 * @param string $group
387 * @return string
389 private static function buildGroupLink( $group ) {
390 static $cache = array();
391 if( !isset( $cache[$group] ) )
392 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupName( $group ) );
393 return $cache[$group];
397 * Returns an array of all groups that may be edited
398 * @return array Array of groups that may be edited.
400 protected static function getAllGroups() {
401 return User::getAllGroups();
405 * Adds the <select> thingie where you can select what groups to add/remove
407 * @param array $groups The groups that can be added/removed
408 * @param string $name 'removable' or 'available'
409 * @return string XHTML <select> element
411 private function groupCheckboxes( $usergroups ) {
412 $allgroups = $this->getAllGroups();
413 $ret = '';
415 $column = 1;
416 $settable_col = '';
417 $unsettable_col = '';
419 foreach ($allgroups as $group) {
420 $set = in_array( $group, $usergroups );
421 # Should the checkbox be disabled?
422 $disabled = !(
423 ( $set && $this->canRemove( $group ) ) ||
424 ( !$set && $this->canAdd( $group ) ) );
425 # Do we need to point out that this action is irreversible?
426 $irreversible = !$disabled && (
427 ($set && !$this->canAdd( $group )) ||
428 (!$set && !$this->canRemove( $group ) ) );
430 $attr = $disabled ? array( 'disabled' => 'disabled' ) : array();
431 $text = $irreversible
432 ? wfMsgHtml( 'userrights-irreversible-marker', User::getGroupMember( $group ) )
433 : User::getGroupMember( $group );
434 $checkbox = Xml::checkLabel( $text, "wpGroup-$group",
435 "wpGroup-$group", $set, $attr );
436 $checkbox = $disabled ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkbox ) : $checkbox;
438 if ($disabled) {
439 $unsettable_col .= "$checkbox<br />\n";
440 } else {
441 $settable_col .= "$checkbox<br />\n";
445 if ($column) {
446 $ret .= Xml::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
447 "<tr>
449 if( $settable_col !== '' ) {
450 $ret .= xml::element( 'th', null, wfMsg( 'userrights-changeable-col' ) );
452 if( $unsettable_col !== '' ) {
453 $ret .= xml::element( 'th', null, wfMsg( 'userrights-unchangeable-col' ) );
455 $ret.= "</tr>
456 <tr>
458 if( $settable_col !== '' ) {
459 $ret .=
460 " <td style='vertical-align:top;'>
461 $settable_col
462 </td>
465 if( $unsettable_col !== '' ) {
466 $ret .=
467 " <td style='vertical-align:top;'>
468 $unsettable_col
469 </td>
472 $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
475 return $ret;
479 * @param string $group The name of the group to check
480 * @return bool Can we remove the group?
482 private function canRemove( $group ) {
483 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
484 // PHP.
485 $groups = $this->changeableGroups();
486 return in_array( $group, $groups['remove'] ) || ($this->isself && in_array( $group, $groups['remove-self'] ));
490 * @param string $group The name of the group to check
491 * @return bool Can we add the group?
493 private function canAdd( $group ) {
494 $groups = $this->changeableGroups();
495 return in_array( $group, $groups['add'] ) || ($this->isself && in_array( $group, $groups['add-self'] ));
499 * Returns an array of the groups that the user can add/remove.
501 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
503 function changeableGroups() {
504 global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
506 if( $wgUser->isAllowed( 'userrights' ) ) {
507 // This group gives the right to modify everything (reverse-
508 // compatibility with old "userrights lets you change
509 // everything")
510 // Using array_merge to make the groups reindexed
511 $all = array_merge( User::getAllGroups() );
512 return array(
513 'add' => $all,
514 'remove' => $all,
515 'add-self' => array(),
516 'remove-self' => array()
520 // Okay, it's not so simple, we will have to go through the arrays
521 $groups = array(
522 'add' => array(),
523 'remove' => array(),
524 'add-self' => $wgGroupsAddToSelf,
525 'remove-self' => $wgGroupsRemoveFromSelf);
526 $addergroups = $wgUser->getEffectiveGroups();
528 foreach ($addergroups as $addergroup) {
529 $groups = array_merge_recursive(
530 $groups, $this->changeableByGroup($addergroup)
532 $groups['add'] = array_unique( $groups['add'] );
533 $groups['remove'] = array_unique( $groups['remove'] );
535 return $groups;
539 * Returns an array of the groups that a particular group can add/remove.
541 * @param String $group The group to check for whether it can add/remove
542 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
544 private function changeableByGroup( $group ) {
545 global $wgAddGroups, $wgRemoveGroups;
547 $groups = array( 'add' => array(), 'remove' => array() );
548 if( empty($wgAddGroups[$group]) ) {
549 // Don't add anything to $groups
550 } elseif( $wgAddGroups[$group] === true ) {
551 // You get everything
552 $groups['add'] = User::getAllGroups();
553 } elseif( is_array($wgAddGroups[$group]) ) {
554 $groups['add'] = $wgAddGroups[$group];
557 // Same thing for remove
558 if( empty($wgRemoveGroups[$group]) ) {
559 } elseif($wgRemoveGroups[$group] === true ) {
560 $groups['remove'] = User::getAllGroups();
561 } elseif( is_array($wgRemoveGroups[$group]) ) {
562 $groups['remove'] = $wgRemoveGroups[$group];
564 return $groups;
568 * Show a rights log fragment for the specified user
570 * @param User $user User to show log for
571 * @param OutputPage $output OutputPage to use
573 protected function showLogFragment( $user, $output ) {
574 $output->addHtml( Xml::element( 'h2', null, LogPage::logName( 'rights' ) . "\n" ) );
575 LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage()->getPrefixedText() );