(bug 12698) Create PAGESIZE parser function, to return the size of a page. Quite...
[mediawiki.git] / includes / SpecialUserrights.php
blob4061a89654a2c32d1da32a201cb194ac74381452
1 <?php
3 /**
4 * Special page to allow managing user group membership
6 * @addtogroup SpecialPage
7 */
9 /**
10 * A class to manage user levels rights.
11 * @addtogroup 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 = User::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 $log = new LogPage( 'rights' );
187 $log->addEntry( 'rights',
188 $user->getUserPage(),
189 $wgRequest->getText( 'user-reason' ),
190 array(
191 $this->makeGroupNameList( $oldGroups ),
192 $this->makeGroupNameList( $newGroups )
199 * Edit user groups membership
200 * @param string $username Name of the user.
202 function editUserGroupsForm( $username ) {
203 global $wgOut;
205 $user = $this->fetchUser( $username );
206 if( !$user ) {
207 return;
210 $groups = $user->getGroups();
212 $this->showEditUserGroupsForm( $user, $groups );
214 // This isn't really ideal logging behavior, but let's not hide the
215 // interwiki logs if we're using them as is.
216 $this->showLogFragment( $user, $wgOut );
220 * Normalize the input username, which may be local or remote, and
221 * return a user (or proxy) object for manipulating it.
223 * Side effects: error output for invalid access
224 * @return mixed User, UserRightsProxy, or null
226 function fetchUser( $username ) {
227 global $wgOut, $wgUser;
229 $parts = explode( '@', $username );
230 if( count( $parts ) < 2 ) {
231 $name = trim( $username );
232 $database = '';
233 } else {
234 list( $name, $database ) = array_map( 'trim', $parts );
236 if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
237 $wgOut->addWikiMsg( 'userrights-no-interwiki' );
238 return null;
240 if( !UserRightsProxy::validDatabase( $database ) ) {
241 $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
242 return null;
246 if( $name == '' ) {
247 $wgOut->addWikiMsg( 'nouserspecified' );
248 return false;
251 if( $name{0} == '#' ) {
252 // Numeric ID can be specified...
253 // We'll do a lookup for the name internally.
254 $id = intval( substr( $name, 1 ) );
256 if( $database == '' ) {
257 $name = User::whoIs( $id );
258 } else {
259 $name = UserRightsProxy::whoIs( $database, $id );
262 if( !$name ) {
263 $wgOut->addWikiMsg( 'noname' );
264 return null;
268 if( $database == '' ) {
269 $user = User::newFromName( $name );
270 } else {
271 $user = UserRightsProxy::newFromName( $database, $name );
274 if( !$user || $user->isAnon() ) {
275 $wgOut->addWikiMsg( 'nosuchusershort', $username );
276 return null;
279 return $user;
282 function makeGroupNameList( $ids ) {
283 return implode( ', ', $ids );
287 * Output a form to allow searching for a user
289 function switchForm() {
290 global $wgOut, $wgScript;
291 $wgOut->addHTML(
292 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
293 Xml::hidden( 'title', 'Special:Userrights' ) .
294 Xml::openElement( 'fieldset' ) .
295 Xml::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
296 Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget ) . ' ' .
297 Xml::submitButton( wfMsg( 'editusergroup' ) ) .
298 Xml::closeElement( 'fieldset' ) .
299 Xml::closeElement( 'form' ) . "\n"
304 * Go through used and available groups and return the ones that this
305 * form will be able to manipulate based on the current user's system
306 * permissions.
308 * @param $groups Array: list of groups the given user is in
309 * @return Array: Tuple of addable, then removable groups
311 protected function splitGroups( $groups ) {
312 global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
313 list($addable, $removable) = array_values( $this->changeableGroups() );
315 $removable = array_intersect(
316 array_merge($this->isself ? $wgGroupsRemoveFromSelf : array(), $removable),
317 $groups ); // Can't remove groups the user doesn't have
318 $addable = array_diff(
319 array_merge($this->isself ? $wgGroupsAddToSelf : array(), $addable),
320 $groups ); // Can't add groups the user does have
322 return array( $addable, $removable );
326 * Show the form to edit group memberships.
328 * @param $user User or UserRightsProxy you're editing
329 * @param $groups Array: Array of groups the user is in
331 protected function showEditUserGroupsForm( $user, $groups ) {
332 global $wgOut, $wgUser;
334 list( $addable, $removable ) = $this->splitGroups( $groups );
336 $list = array();
337 foreach( $user->getGroups() as $group )
338 $list[] = self::buildGroupLink( $group );
340 $grouplist = '';
341 if( count( $list ) > 0 ) {
342 $grouplist = Xml::tags( 'p', null, wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) );
344 $wgOut->addHTML(
345 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
346 Xml::hidden( 'user', $this->mTarget ) .
347 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget ) ) .
348 Xml::openElement( 'fieldset' ) .
349 Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
350 wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
351 $grouplist .
352 Xml::tags( 'p', null, $this->groupCheckboxes( $groups ) ) .
353 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
354 "<tr>
355 <td colspan='2'>" .
356 $wgOut->parse( wfMsg( 'userrights-groups-help' ) ) .
357 "</td>
358 </tr>
359 <tr>
360 <td class='mw-label'>" .
361 Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
362 "</td>
363 <td class='mw-input'>" .
364 Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
365 "</td>
366 </tr>
367 <tr>
368 <td></td>
369 <td class='mw-submit'>" .
370 Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
371 "</td>
372 </tr>" .
373 Xml::closeElement( 'table' ) . "\n" .
374 Xml::closeElement( 'fieldset' ) .
375 Xml::closeElement( 'form' ) . "\n"
380 * Format a link to a group description page
382 * @param string $group
383 * @return string
385 private static function buildGroupLink( $group ) {
386 static $cache = array();
387 if( !isset( $cache[$group] ) )
388 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupName( $group ) );
389 return $cache[$group];
393 * Adds the <select> thingie where you can select what groups to add/remove
395 * @param array $groups The groups that can be added/removed
396 * @param string $name 'removable' or 'available'
397 * @return string XHTML <select> element
399 private function groupCheckboxes( $usergroups ) {
400 $allgroups = User::getAllGroups();
401 $ret = '';
403 $column = 1;
404 $settable_col = '';
405 $unsettable_col = '';
407 foreach ($allgroups as $group) {
408 $set = in_array( $group, $usergroups );
409 # Should the checkbox be disabled?
410 $disabled = !(
411 ( $set && $this->canRemove( $group ) ) ||
412 ( !$set && $this->canAdd( $group ) ) );
413 # Do we need to point out that this action is irreversible?
414 $irreversible = !$disabled && (
415 ($set && !$this->canAdd( $group )) ||
416 (!$set && !$this->canRemove( $group ) ) );
418 $attr = $disabled ? array( 'disabled' => 'disabled' ) : array();
419 $text = $irreversible
420 ? wfMsgHtml( 'userrights-irreversible-marker', User::getGroupMember( $group ) )
421 : User::getGroupMember( $group );
422 $checkbox = Xml::checkLabel( $text, "wpGroup-$group",
423 "wpGroup-$group", $set, $attr );
424 $checkbox = $disabled ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkbox ) : $checkbox;
426 if ($disabled) {
427 $unsettable_col .= "$checkbox<br/>\n";
428 } else {
429 $settable_col .= "$checkbox<br/>\n";
433 if ($column) {
434 $ret .= Xml::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
435 "<tr>
437 if( $settable_col !== '' ) {
438 $ret .= xml::element( 'th', null, wfMsg( 'userrights-changeable-col' ) );
440 if( $unsettable_col !== '' ) {
441 $ret .= xml::element( 'th', null, wfMsg( 'userrights-unchangeable-col' ) );
443 $ret.= "</tr>
444 <tr>
446 if( $settable_col !== '' ) {
447 $ret .=
448 " <td style='vertical-align:top;'>
449 $settable_col
450 </td>
453 if( $unsettable_col !== '' ) {
454 $ret .=
455 " <td style='vertical-align:top;'>
456 $unsettable_col
457 </td>
458 </tr>";
460 $ret .= Xml::closeElement( 'table' );
463 return $ret;
467 * @param string $group The name of the group to check
468 * @return bool Can we remove the group?
470 private function canRemove( $group ) {
471 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
472 // PHP.
473 $groups = $this->changeableGroups();
474 return in_array( $group, $groups['remove'] ) || ($this->isself && in_array( $group, $groups['remove-self'] ));
478 * @param string $group The name of the group to check
479 * @return bool Can we add the group?
481 private function canAdd( $group ) {
482 $groups = $this->changeableGroups();
483 return in_array( $group, $groups['add'] ) || ($this->isself && in_array( $group, $groups['add-self'] ));
487 * Returns an array of the groups that the user can add/remove.
489 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
491 function changeableGroups() {
492 global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
494 if( $wgUser->isAllowed( 'userrights' ) ) {
495 // This group gives the right to modify everything (reverse-
496 // compatibility with old "userrights lets you change
497 // everything")
498 // Using array_merge to make the groups reindexed
499 $all = array_merge( User::getAllGroups() );
500 return array(
501 'add' => $all,
502 'remove' => $all,
503 'add-self' => array(),
504 'remove-self' => array()
508 // Okay, it's not so simple, we will have to go through the arrays
509 $groups = array(
510 'add' => array(),
511 'remove' => array(),
512 'add-self' => $wgGroupsAddToSelf,
513 'remove-self' => $wgGroupsRemoveFromSelf);
514 $addergroups = $wgUser->getEffectiveGroups();
516 foreach ($addergroups as $addergroup) {
517 $groups = array_merge_recursive(
518 $groups, $this->changeableByGroup($addergroup)
520 $groups['add'] = array_unique( $groups['add'] );
521 $groups['remove'] = array_unique( $groups['remove'] );
523 return $groups;
527 * Returns an array of the groups that a particular group can add/remove.
529 * @param String $group The group to check for whether it can add/remove
530 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
532 private function changeableByGroup( $group ) {
533 global $wgAddGroups, $wgRemoveGroups;
535 $groups = array( 'add' => array(), 'remove' => array() );
536 if( empty($wgAddGroups[$group]) ) {
537 // Don't add anything to $groups
538 } elseif( $wgAddGroups[$group] === true ) {
539 // You get everything
540 $groups['add'] = User::getAllGroups();
541 } elseif( is_array($wgAddGroups[$group]) ) {
542 $groups['add'] = $wgAddGroups[$group];
545 // Same thing for remove
546 if( empty($wgRemoveGroups[$group]) ) {
547 } elseif($wgRemoveGroups[$group] === true ) {
548 $groups['remove'] = User::getAllGroups();
549 } elseif( is_array($wgRemoveGroups[$group]) ) {
550 $groups['remove'] = $wgRemoveGroups[$group];
552 return $groups;
556 * Show a rights log fragment for the specified user
558 * @param User $user User to show log for
559 * @param OutputPage $output OutputPage to use
561 protected function showLogFragment( $user, $output ) {
562 $output->addHtml( Xml::element( 'h2', null, LogPage::logName( 'rights' ) . "\n" ) );
563 LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage()->getPrefixedText() );