3 * Implements Special:Userrights
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Special page to allow managing user group membership
27 * @ingroup SpecialPage
29 class UserrightsPage
extends SpecialPage
{
30 # The target of the local right-adjuster's interest. Can be gotten from
31 # either a GET parameter or a subpage-style parameter, so have a member
34 protected $isself = false;
36 public function __construct() {
37 parent
::__construct( 'Userrights' );
40 public function isRestricted() {
44 public function userCanExecute( User
$user ) {
45 return $this->userCanChangeRights( $user, false );
50 * @param bool $checkIfSelf
53 public function userCanChangeRights( $user, $checkIfSelf = true ) {
54 $available = $this->changeableGroups();
55 if ( $user->getId() == 0 ) {
59 return !empty( $available['add'] )
60 ||
!empty( $available['remove'] )
61 ||
( ( $this->isself ||
!$checkIfSelf ) &&
62 ( !empty( $available['add-self'] )
63 ||
!empty( $available['remove-self'] ) ) );
67 * Manage forms to be shown according to posted data.
68 * Depending on the submit button used, call a form or a save function.
70 * @param string|null $par String if any subpage provided, else null
71 * @throws UserBlockedError|PermissionsError
73 public function execute( $par ) {
74 // If the visitor doesn't have permissions to assign or remove
75 // any groups, it's a bit silly to give them the user search prompt.
77 $user = $this->getUser();
80 * If the user is blocked and they only have "partial" access
81 * (e.g. they don't have the userrights permission), then don't
82 * allow them to use Special:UserRights.
84 if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
85 throw new UserBlockedError( $user->getBlock() );
88 $request = $this->getRequest();
90 if ( $par !== null ) {
91 $this->mTarget
= $par;
93 $this->mTarget
= $request->getVal( 'user' );
96 $available = $this->changeableGroups();
98 if ( $this->mTarget
=== null ) {
100 * If the user specified no target, and they can only
101 * edit their own groups, automatically set them as the
104 if ( !count( $available['add'] ) && !count( $available['remove'] ) ) {
105 $this->mTarget
= $user->getName();
109 if ( User
::getCanonicalName( $this->mTarget
) == $user->getName() ) {
110 $this->isself
= true;
113 if ( !$this->userCanChangeRights( $user, true ) ) {
114 if ( $this->isself
&& $request->getCheck( 'success' ) ) {
115 // bug 48609: if the user just removed its own rights, this would
116 // leads it in a "permissions error" page. In that case, show a
117 // message that it can't anymore use this page instead of an error
119 $out = $this->getOutput();
120 $out->wrapWikiMsg( "<div class=\"successbox\">\n$1\n</div>", 'userrights-removed-self' );
121 $out->returnToMain();
126 // @todo FIXME: There may be intermediate groups we can mention.
127 $msg = $user->isAnon() ?
'userrights-nologin' : 'userrights-notallowed';
128 throw new PermissionsError( null, array( array( $msg ) ) );
131 $this->checkReadOnly();
134 $this->outputHeader();
136 $out = $this->getOutput();
137 $out->addModuleStyles( 'mediawiki.special' );
139 // show the general form
140 if ( count( $available['add'] ) ||
count( $available['remove'] ) ) {
145 $request->wasPosted() &&
146 $request->getCheck( 'saveusergroups' ) &&
147 $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget
)
150 $status = $this->fetchUser( $this->mTarget
);
151 if ( !$status->isOK() ) {
152 $this->getOutput()->addWikiText( $status->getWikiText() );
157 $targetUser = $status->value
;
158 if ( $targetUser instanceof User
) { // UserRightsProxy doesn't have this method (bug 61252)
159 $targetUser->clearInstanceCache(); // bug 38989
162 if ( $request->getVal( 'conflictcheck-originalgroups' )
163 !== implode( ',', $targetUser->getGroups() )
165 $out->addWikiMsg( 'userrights-conflict' );
167 $this->saveUserGroups(
169 $request->getVal( 'user-reason' ),
173 $out->redirect( $this->getSuccessURL() );
179 // show some more forms
180 if ( $this->mTarget
!== null ) {
181 $this->editUserGroupsForm( $this->mTarget
);
185 function getSuccessURL() {
186 return $this->getPageTitle( $this->mTarget
)->getFullURL( array( 'success' => 1 ) );
190 * Save user groups changes in the database.
191 * Data comes from the editUserGroupsForm() form function
193 * @param string $username Username to apply changes to.
194 * @param string $reason Reason for group change
195 * @param User|UserRightsProxy $user Target user object.
198 function saveUserGroups( $username, $reason, $user ) {
199 $allgroups = $this->getAllGroups();
201 $removegroup = array();
203 // This could possibly create a highly unlikely race condition if permissions are changed between
204 // when the form is loaded and when the form is saved. Ignoring it for the moment.
205 foreach ( $allgroups as $group ) {
206 // We'll tell it to remove all unchecked groups, and add all checked groups.
207 // Later on, this gets filtered for what can actually be removed
208 if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
209 $addgroup[] = $group;
211 $removegroup[] = $group;
215 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
219 * Save user groups changes in the database.
222 * @param array $add Array of groups to add
223 * @param array $remove Array of groups to remove
224 * @param string $reason Reason for group change
225 * @return array Tuple of added, then removed groups
227 function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
230 // Validate input set...
231 $isself = ( $user->getName() == $this->getUser()->getName() );
232 $groups = $user->getGroups();
233 $changeable = $this->changeableGroups();
234 $addable = array_merge( $changeable['add'], $isself ?
$changeable['add-self'] : array() );
235 $removable = array_merge( $changeable['remove'], $isself ?
$changeable['remove-self'] : array() );
237 $remove = array_unique(
238 array_intersect( (array)$remove, $removable, $groups ) );
239 $add = array_unique( array_diff(
240 array_intersect( (array)$add, $addable ),
244 $oldGroups = $user->getGroups();
245 $newGroups = $oldGroups;
247 // remove then add groups
249 $newGroups = array_diff( $newGroups, $remove );
250 foreach ( $remove as $group ) {
251 $user->removeGroup( $group );
255 $newGroups = array_merge( $newGroups, $add );
256 foreach ( $add as $group ) {
257 $user->addGroup( $group );
260 $newGroups = array_unique( $newGroups );
262 // Ensure that caches are cleared
263 $user->invalidateCache();
265 // update groups in external authentication database
266 $wgAuth->updateExternalDBGroups( $user, $add, $remove );
268 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) . "\n" );
269 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) . "\n" );
270 wfRunHooks( 'UserRights', array( &$user, $add, $remove ) );
272 if ( $newGroups != $oldGroups ) {
273 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
276 return array( $add, $remove );
280 * Add a rights log entry for an action.
282 * @param array $oldGroups
283 * @param array $newGroups
284 * @param array $reason
286 function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
287 $logEntry = new ManualLogEntry( 'rights', 'rights' );
288 $logEntry->setPerformer( $this->getUser() );
289 $logEntry->setTarget( $user->getUserPage() );
290 $logEntry->setComment( $reason );
291 $logEntry->setParameters( array(
292 '4::oldgroups' => $oldGroups,
293 '5::newgroups' => $newGroups,
295 $logid = $logEntry->insert();
296 $logEntry->publish( $logid );
300 * Edit user groups membership
301 * @param string $username Name of the user.
303 function editUserGroupsForm( $username ) {
304 $status = $this->fetchUser( $username );
305 if ( !$status->isOK() ) {
306 $this->getOutput()->addWikiText( $status->getWikiText() );
310 $user = $status->value
;
313 $groups = $user->getGroups();
315 $this->showEditUserGroupsForm( $user, $groups );
317 // This isn't really ideal logging behavior, but let's not hide the
318 // interwiki logs if we're using them as is.
319 $this->showLogFragment( $user, $this->getOutput() );
323 * Normalize the input username, which may be local or remote, and
324 * return a user (or proxy) object for manipulating it.
326 * Side effects: error output for invalid access
327 * @param string $username
330 public function fetchUser( $username ) {
331 $parts = explode( $this->getConfig()->get( 'UserrightsInterwikiDelimiter' ), $username );
332 if ( count( $parts ) < 2 ) {
333 $name = trim( $username );
336 list( $name, $database ) = array_map( 'trim', $parts );
338 if ( $database == wfWikiID() ) {
341 if ( !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
342 return Status
::newFatal( 'userrights-no-interwiki' );
344 if ( !UserRightsProxy
::validDatabase( $database ) ) {
345 return Status
::newFatal( 'userrights-nodatabase', $database );
350 if ( $name === '' ) {
351 return Status
::newFatal( 'nouserspecified' );
354 if ( $name[0] == '#' ) {
355 // Numeric ID can be specified...
356 // We'll do a lookup for the name internally.
357 $id = intval( substr( $name, 1 ) );
359 if ( $database == '' ) {
360 $name = User
::whoIs( $id );
362 $name = UserRightsProxy
::whoIs( $database, $id );
366 return Status
::newFatal( 'noname' );
369 $name = User
::getCanonicalName( $name );
370 if ( $name === false ) {
372 return Status
::newFatal( 'nosuchusershort', $username );
376 if ( $database == '' ) {
377 $user = User
::newFromName( $name );
379 $user = UserRightsProxy
::newFromName( $database, $name );
382 if ( !$user ||
$user->isAnon() ) {
383 return Status
::newFatal( 'nosuchusershort', $username );
386 return Status
::newGood( $user );
389 function makeGroupNameList( $ids ) {
390 if ( empty( $ids ) ) {
391 return $this->msg( 'rightsnone' )->inContentLanguage()->text();
393 return implode( ', ', $ids );
398 * Make a list of group names to be stored as parameter for log entries
400 * @deprecated since 1.21; use LogFormatter instead.
404 function makeGroupNameListForLog( $ids ) {
405 wfDeprecated( __METHOD__
, '1.21' );
407 if ( empty( $ids ) ) {
410 return $this->makeGroupNameList( $ids );
415 * Output a form to allow searching for a user
417 function switchForm() {
418 $this->getOutput()->addHTML(
423 'action' => wfScript(),
425 'id' => 'mw-userrights-form1'
428 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
429 Xml
::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
431 $this->msg( 'userrights-user-editname' )->text(),
435 str_replace( '_', ' ', $this->mTarget
),
436 array( 'autofocus' => true )
438 Xml
::submitButton( $this->msg( 'editusergroup' )->text() ) .
439 Html
::closeElement( 'fieldset' ) .
440 Html
::closeElement( 'form' ) . "\n"
445 * Go through used and available groups and return the ones that this
446 * form will be able to manipulate based on the current user's system
449 * @param array $groups List of groups the given user is in
450 * @return array Tuple of addable, then removable groups
452 protected function splitGroups( $groups ) {
453 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
455 $removable = array_intersect(
456 array_merge( $this->isself ?
$removeself : array(), $removable ),
458 ); // Can't remove groups the user doesn't have
459 $addable = array_diff(
460 array_merge( $this->isself ?
$addself : array(), $addable ),
462 ); // Can't add groups the user does have
464 return array( $addable, $removable );
468 * Show the form to edit group memberships.
470 * @param User|UserRightsProxy $user User or UserRightsProxy you're editing
471 * @param array $groups Array of groups the user is in
473 protected function showEditUserGroupsForm( $user, $groups ) {
475 $membersList = array();
476 foreach ( $groups as $group ) {
477 $list[] = self
::buildGroupLink( $group );
478 $membersList[] = self
::buildGroupMemberLink( $group );
482 $autoMembersList = array();
483 if ( $user instanceof User
) {
484 foreach ( Autopromote
::getAutopromoteGroups( $user ) as $group ) {
485 $autoList[] = self
::buildGroupLink( $group );
486 $autoMembersList[] = self
::buildGroupMemberLink( $group );
490 $language = $this->getLanguage();
491 $displayedList = $this->msg( 'userrights-groupsmember-type',
492 $language->listToText( $list ),
493 $language->listToText( $membersList )
495 $displayedAutolist = $this->msg( 'userrights-groupsmember-type',
496 $language->listToText( $autoList ),
497 $language->listToText( $autoMembersList )
501 $count = count( $list );
503 $grouplist = $this->msg( 'userrights-groupsmember', $count, $user->getName() )->parse();
504 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
507 $count = count( $autoList );
509 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto', $count, $user->getName() )
511 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
514 $userToolLinks = Linker
::userToolLinks(
517 false, /* default for redContribsWhenNoEdits */
518 Linker
::TOOL_LINKS_EMAIL
/* Add "send e-mail" link */
521 $this->getOutput()->addHTML(
526 'action' => $this->getPageTitle()->getLocalURL(),
527 'name' => 'editGroup',
528 'id' => 'mw-userrights-form2'
531 Html
::hidden( 'user', $this->mTarget
) .
532 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget
) ) .
534 'conflictcheck-originalgroups',
535 implode( ',', $user->getGroups() )
536 ) . // Conflict detection
537 Xml
::openElement( 'fieldset' ) .
541 $this->msg( 'userrights-editusergroup', $user->getName() )->text()
543 $this->msg( 'editinguser' )->params( wfEscapeWikiText( $user->getName() ) )
544 ->rawParams( $userToolLinks )->parse() .
545 $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
547 $this->groupCheckboxes( $groups, $user ) .
548 Xml
::openElement( 'table', array( 'id' => 'mw-userrights-table-outer' ) ) .
550 <td class='mw-label'>" .
551 Xml
::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
553 <td class='mw-input'>" .
554 Xml
::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ),
555 array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
560 <td class='mw-submit'>" .
561 Xml
::submitButton( $this->msg( 'saveusergroups' )->text(),
562 array( 'name' => 'saveusergroups' ) +
563 Linker
::tooltipAndAccesskeyAttribs( 'userrights-set' )
567 Xml
::closeElement( 'table' ) . "\n" .
568 Xml
::closeElement( 'fieldset' ) .
569 Xml
::closeElement( 'form' ) . "\n"
574 * Format a link to a group description page
576 * @param string $group
579 private static function buildGroupLink( $group ) {
580 return User
::makeGroupLinkHtml( $group, User
::getGroupName( $group ) );
584 * Format a link to a group member description page
586 * @param string $group
589 private static function buildGroupMemberLink( $group ) {
590 return User
::makeGroupLinkHtml( $group, User
::getGroupMember( $group ) );
594 * Returns an array of all groups that may be edited
595 * @return array Array of groups that may be edited.
597 protected static function getAllGroups() {
598 return User
::getAllGroups();
602 * Adds a table with checkboxes where you can select what groups to add/remove
604 * @todo Just pass the username string?
605 * @param array $usergroups Groups the user belongs to
607 * @return string XHTML table element with checkboxes
609 private function groupCheckboxes( $usergroups, $user ) {
610 $allgroups = $this->getAllGroups();
613 // Put all column info into an associative array so that extensions can
614 // more easily manage it.
615 $columns = array( 'unchangeable' => array(), 'changeable' => array() );
617 foreach ( $allgroups as $group ) {
618 $set = in_array( $group, $usergroups );
619 // Should the checkbox be disabled?
621 ( $set && $this->canRemove( $group ) ) ||
622 ( !$set && $this->canAdd( $group ) ) );
623 // Do we need to point out that this action is irreversible?
624 $irreversible = !$disabled && (
625 ( $set && !$this->canAdd( $group ) ) ||
626 ( !$set && !$this->canRemove( $group ) ) );
630 'disabled' => $disabled,
631 'irreversible' => $irreversible
635 $columns['unchangeable'][$group] = $checkbox;
637 $columns['changeable'][$group] = $checkbox;
641 // Build the HTML table
642 $ret .= Xml
::openElement( 'table', array( 'class' => 'mw-userrights-groups' ) ) .
644 foreach ( $columns as $name => $column ) {
645 if ( $column === array() ) {
648 // Messages: userrights-changeable-col, userrights-unchangeable-col
649 $ret .= Xml
::element(
652 $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text()
656 $ret .= "</tr>\n<tr>\n";
657 foreach ( $columns as $column ) {
658 if ( $column === array() ) {
661 $ret .= "\t<td style='vertical-align:top;'>\n";
662 foreach ( $column as $group => $checkbox ) {
663 $attr = $checkbox['disabled'] ?
array( 'disabled' => 'disabled' ) : array();
665 $member = User
::getGroupMember( $group, $user->getName() );
666 if ( $checkbox['irreversible'] ) {
667 $text = $this->msg( 'userrights-irreversible-marker', $member )->escaped();
669 $text = htmlspecialchars( $member );
671 $checkboxHtml = Xml
::checkLabel( $text, "wpGroup-" . $group,
672 "wpGroup-" . $group, $checkbox['set'], $attr );
673 $ret .= "\t\t" . ( $checkbox['disabled']
674 ? Xml
::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkboxHtml )
680 $ret .= Xml
::closeElement( 'tr' ) . Xml
::closeElement( 'table' );
686 * @param string $group The name of the group to check
687 * @return bool Can we remove the group?
689 private function canRemove( $group ) {
690 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks, PHP.
691 $groups = $this->changeableGroups();
695 $groups['remove'] ) ||
( $this->isself
&& in_array( $group, $groups['remove-self'] )
700 * @param string $group The name of the group to check
701 * @return bool Can we add the group?
703 private function canAdd( $group ) {
704 $groups = $this->changeableGroups();
708 $groups['add'] ) ||
( $this->isself
&& in_array( $group, $groups['add-self'] )
713 * Returns $this->getUser()->changeableGroups()
715 * @return array Array(
716 * 'add' => array( addablegroups ),
717 * 'remove' => array( removablegroups ),
718 * 'add-self' => array( addablegroups to self ),
719 * 'remove-self' => array( removable groups from self )
722 function changeableGroups() {
723 return $this->getUser()->changeableGroups();
727 * Show a rights log fragment for the specified user
729 * @param User $user User to show log for
730 * @param OutputPage $output OutputPage to use
732 protected function showLogFragment( $user, $output ) {
733 $rightsLogPage = new LogPage( 'rights' );
734 $output->addHTML( Xml
::element( 'h2', null, $rightsLogPage->getName()->text() ) );
735 LogEventsList
::showLogExtract( $output, 'rights', $user->getUserPage() );
738 protected function getGroupName() {