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
{
31 * The target of the local right-adjuster's interest. Can be gotten from
32 * either a GET parameter or a subpage-style parameter, so have a member
34 * @var null|string $mTarget
38 * @var null|User $mFetchedUser The user object of the target username or null.
40 protected $mFetchedUser = null;
41 protected $isself = false;
43 public function __construct() {
44 parent
::__construct( 'Userrights' );
47 public function doesWrites() {
52 * Check whether the current user (from context) can change the target user's rights.
54 * @param User $targetUser User whose rights are being changed
55 * @param bool $checkIfSelf If false, assume that the current user can add/remove groups defined
56 * in $wgGroupsAddToSelf / $wgGroupsRemoveFromSelf, without checking if it's the same as target
60 public function userCanChangeRights( $targetUser, $checkIfSelf = true ) {
61 $isself = $this->getUser()->equals( $targetUser );
63 $available = $this->changeableGroups();
64 if ( $targetUser->getId() === 0 ) {
68 if ( $available['add'] ||
$available['remove'] ) {
69 // can change some rights for any user
73 if ( ( $available['add-self'] ||
$available['remove-self'] )
74 && ( $isself ||
!$checkIfSelf )
76 // can change some rights for self
84 * Manage forms to be shown according to posted data.
85 * Depending on the submit button used, call a form or a save function.
87 * @param string|null $par String if any subpage provided, else null
88 * @throws UserBlockedError|PermissionsError
90 public function execute( $par ) {
91 $user = $this->getUser();
92 $request = $this->getRequest();
93 $session = $request->getSession();
94 $out = $this->getOutput();
96 $out->addModules( [ 'mediawiki.special.userrights' ] );
98 $this->mTarget
= $par ??
$request->getVal( 'user' );
100 if ( is_string( $this->mTarget
) ) {
101 $this->mTarget
= trim( $this->mTarget
);
104 if ( $this->mTarget
!== null && User
::getCanonicalName( $this->mTarget
) === $user->getName() ) {
105 $this->isself
= true;
108 $fetchedStatus = $this->fetchUser( $this->mTarget
, true );
109 if ( $fetchedStatus->isOK() ) {
110 $this->mFetchedUser
= $fetchedStatus->value
;
111 if ( $this->mFetchedUser
instanceof User
) {
112 // Set the 'relevant user' in the skin, so it displays links like Contributions,
113 // User logs, UserRights, etc.
114 $this->getSkin()->setRelevantUser( $this->mFetchedUser
);
118 // show a successbox, if the user rights was saved successfully
120 $session->get( 'specialUserrightsSaveSuccess' ) &&
121 $this->mFetchedUser
!== null
123 // Remove session data for the success message
124 $session->remove( 'specialUserrightsSaveSuccess' );
126 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
131 'class' => 'mw-notify-success successbox',
132 'id' => 'mw-preferences-success',
133 'data-mw-autohide' => 'false',
138 $this->msg( 'savedrights', $this->mFetchedUser
->getName() )->text()
145 $this->outputHeader();
147 $out->addModuleStyles( 'mediawiki.special' );
148 $this->addHelpLink( 'Help:Assigning permissions' );
153 $request->wasPosted() &&
154 $request->getCheck( 'saveusergroups' ) &&
155 $this->mTarget
!== null &&
156 $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget
)
159 * If the user is blocked and they only have "partial" access
160 * (e.g. they don't have the userrights permission), then don't
161 * allow them to change any user rights.
163 if ( !$user->isAllowed( 'userrights' ) ) {
164 // @TODO Should the user be blocked from changing user rights if they
165 // are partially blocked?
166 $block = $user->getBlock();
168 throw new UserBlockedError( $user->getBlock() );
172 $this->checkReadOnly();
175 if ( !$fetchedStatus->isOK() ) {
176 $this->getOutput()->addWikiTextAsInterface( $fetchedStatus->getWikiText() );
181 $targetUser = $this->mFetchedUser
;
182 if ( $targetUser instanceof User
) { // UserRightsProxy doesn't have this method (T63252)
183 $targetUser->clearInstanceCache(); // T40989
186 $conflictCheck = $request->getVal( 'conflictcheck-originalgroups' );
187 $conflictCheck = ( $conflictCheck === '' ) ?
[] : explode( ',', $conflictCheck );
188 $userGroups = $targetUser->getGroups();
190 if ( $userGroups !== $conflictCheck ) {
191 $out->wrapWikiMsg( '<span class="error">$1</span>', 'userrights-conflict' );
193 $status = $this->saveUserGroups(
195 $request->getVal( 'user-reason' ),
199 if ( $status->isOK() ) {
200 // Set session data for the success message
201 $session->set( 'specialUserrightsSaveSuccess', 1 );
203 $out->redirect( $this->getSuccessURL() );
206 // Print an error message and redisplay the form
207 $out->wrapWikiTextAsInterface( 'error', $status->getWikiText() );
212 // show some more forms
213 if ( $this->mTarget
!== null ) {
214 $this->editUserGroupsForm( $this->mTarget
);
218 function getSuccessURL() {
219 return $this->getPageTitle( $this->mTarget
)->getFullURL();
223 * Returns true if this user rights form can set and change user group expiries.
224 * Subclasses may wish to override this to return false.
228 public function canProcessExpiries() {
233 * Converts a user group membership expiry string into a timestamp. Words like
234 * 'existing' or 'other' should have been filtered out before calling this
237 * @param string $expiry
238 * @return string|null|false A string containing a valid timestamp, or null
239 * if the expiry is infinite, or false if the timestamp is not valid
241 public static function expiryToTimestamp( $expiry ) {
242 if ( wfIsInfinity( $expiry ) ) {
246 $unix = strtotime( $expiry );
248 if ( !$unix ||
$unix === -1 ) {
252 // @todo FIXME: Non-qualified absolute times are not in users specified timezone
253 // and there isn't notice about it in the ui (see ProtectionForm::getExpiry)
254 return wfTimestamp( TS_MW
, $unix );
258 * Save user groups changes in the database.
259 * Data comes from the editUserGroupsForm() form function
261 * @param string $username Username to apply changes to.
262 * @param string $reason Reason for group change
263 * @param User|UserRightsProxy $user Target user object.
266 protected function saveUserGroups( $username, $reason, $user ) {
267 $allgroups = $this->getAllGroups();
269 $groupExpiries = []; // associative array of (group name => expiry)
271 $existingUGMs = $user->getGroupMemberships();
273 // This could possibly create a highly unlikely race condition if permissions are changed between
274 // when the form is loaded and when the form is saved. Ignoring it for the moment.
275 foreach ( $allgroups as $group ) {
276 // We'll tell it to remove all unchecked groups, and add all checked groups.
277 // Later on, this gets filtered for what can actually be removed
278 if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
279 $addgroup[] = $group;
281 if ( $this->canProcessExpiries() ) {
282 // read the expiry information from the request
283 $expiryDropdown = $this->getRequest()->getVal( "wpExpiry-$group" );
284 if ( $expiryDropdown === 'existing' ) {
288 if ( $expiryDropdown === 'other' ) {
289 $expiryValue = $this->getRequest()->getVal( "wpExpiry-$group-other" );
291 $expiryValue = $expiryDropdown;
294 // validate the expiry
295 $groupExpiries[$group] = self
::expiryToTimestamp( $expiryValue );
297 if ( $groupExpiries[$group] === false ) {
298 return Status
::newFatal( 'userrights-invalid-expiry', $group );
301 // not allowed to have things expiring in the past
302 if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
303 return Status
::newFatal( 'userrights-expiry-in-past', $group );
306 // if the user can only add this group (not remove it), the expiry time
307 // cannot be brought forward (T156784)
308 if ( !$this->canRemove( $group ) &&
309 isset( $existingUGMs[$group] ) &&
310 ( $existingUGMs[$group]->getExpiry() ?
: 'infinity' ) >
311 ( $groupExpiries[$group] ?
: 'infinity' )
313 return Status
::newFatal( 'userrights-cannot-shorten-expiry', $group );
317 $removegroup[] = $group;
321 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason, [], $groupExpiries );
323 return Status
::newGood();
327 * Save user groups changes in the database. This function does not throw errors;
328 * instead, it ignores groups that the performer does not have permission to set.
330 * @param User|UserRightsProxy $user
331 * @param array $add Array of groups to add
332 * @param array $remove Array of groups to remove
333 * @param string $reason Reason for group change
334 * @param array $tags Array of change tags to add to the log entry
335 * @param array $groupExpiries Associative array of (group name => expiry),
336 * containing only those groups that are to have new expiry values set
337 * @return array Tuple of added, then removed groups
339 function doSaveUserGroups( $user, array $add, array $remove, $reason = '',
340 array $tags = [], array $groupExpiries = []
342 // Validate input set...
343 $isself = $user->getName() == $this->getUser()->getName();
344 $groups = $user->getGroups();
345 $ugms = $user->getGroupMemberships();
346 $changeable = $this->changeableGroups();
347 $addable = array_merge( $changeable['add'], $isself ?
$changeable['add-self'] : [] );
348 $removable = array_merge( $changeable['remove'], $isself ?
$changeable['remove-self'] : [] );
350 $remove = array_unique(
351 array_intersect( (array)$remove, $removable, $groups ) );
352 $add = array_intersect( (array)$add, $addable );
354 // add only groups that are not already present or that need their expiry updated,
355 // UNLESS the user can only add this group (not remove it) and the expiry time
356 // is being brought forward (T156784)
357 $add = array_filter( $add,
358 function ( $group ) use ( $groups, $groupExpiries, $removable, $ugms ) {
359 if ( isset( $groupExpiries[$group] ) &&
360 !in_array( $group, $removable ) &&
361 isset( $ugms[$group] ) &&
362 ( $ugms[$group]->getExpiry() ?
: 'infinity' ) >
363 ( $groupExpiries[$group] ?
: 'infinity' )
367 return !in_array( $group, $groups ) ||
array_key_exists( $group, $groupExpiries );
370 Hooks
::run( 'ChangeUserGroups', [ $this->getUser(), $user, &$add, &$remove ] );
372 $oldGroups = $groups;
373 $oldUGMs = $user->getGroupMemberships();
374 $newGroups = $oldGroups;
376 // Remove groups, then add new ones/update expiries of existing ones
378 foreach ( $remove as $index => $group ) {
379 if ( !$user->removeGroup( $group ) ) {
380 unset( $remove[$index] );
383 $newGroups = array_diff( $newGroups, $remove );
386 foreach ( $add as $index => $group ) {
387 $expiry = $groupExpiries[$group] ??
null;
388 if ( !$user->addGroup( $group, $expiry ) ) {
389 unset( $add[$index] );
392 $newGroups = array_merge( $newGroups, $add );
394 $newGroups = array_unique( $newGroups );
395 $newUGMs = $user->getGroupMemberships();
397 // Ensure that caches are cleared
398 $user->invalidateCache();
400 // update groups in external authentication database
401 Hooks
::run( 'UserGroupsChanged', [ $user, $add, $remove, $this->getUser(),
402 $reason, $oldUGMs, $newUGMs ] );
404 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) . "\n" );
405 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) . "\n" );
406 wfDebug( 'oldUGMs: ' . print_r( $oldUGMs, true ) . "\n" );
407 wfDebug( 'newUGMs: ' . print_r( $newUGMs, true ) . "\n" );
408 // Deprecated in favor of UserGroupsChanged hook
409 Hooks
::run( 'UserRights', [ &$user, $add, $remove ], '1.26' );
411 // Only add a log entry if something actually changed
412 if ( $newGroups != $oldGroups ||
$newUGMs != $oldUGMs ) {
413 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason, $tags, $oldUGMs, $newUGMs );
416 return [ $add, $remove ];
420 * Serialise a UserGroupMembership object for storage in the log_params section
421 * of the logging table. Only keeps essential data, removing redundant fields.
423 * @param UserGroupMembership|null $ugm May be null if things get borked
426 protected static function serialiseUgmForLog( $ugm ) {
427 if ( !$ugm instanceof UserGroupMembership
) {
430 return [ 'expiry' => $ugm->getExpiry() ];
434 * Add a rights log entry for an action.
435 * @param User|UserRightsProxy $user
436 * @param array $oldGroups
437 * @param array $newGroups
438 * @param string $reason
439 * @param array $tags Change tags for the log entry
440 * @param array $oldUGMs Associative array of (group name => UserGroupMembership)
441 * @param array $newUGMs Associative array of (group name => UserGroupMembership)
443 protected function addLogEntry( $user, array $oldGroups, array $newGroups, $reason,
444 array $tags, array $oldUGMs, array $newUGMs
446 // make sure $oldUGMs and $newUGMs are in the same order, and serialise
447 // each UGM object to a simplified array
448 $oldUGMs = array_map( function ( $group ) use ( $oldUGMs ) {
449 return isset( $oldUGMs[$group] ) ?
450 self
::serialiseUgmForLog( $oldUGMs[$group] ) :
453 $newUGMs = array_map( function ( $group ) use ( $newUGMs ) {
454 return isset( $newUGMs[$group] ) ?
455 self
::serialiseUgmForLog( $newUGMs[$group] ) :
459 $logEntry = new ManualLogEntry( 'rights', 'rights' );
460 $logEntry->setPerformer( $this->getUser() );
461 $logEntry->setTarget( $user->getUserPage() );
462 $logEntry->setComment( $reason );
463 $logEntry->setParameters( [
464 '4::oldgroups' => $oldGroups,
465 '5::newgroups' => $newGroups,
466 'oldmetadata' => $oldUGMs,
467 'newmetadata' => $newUGMs,
469 $logid = $logEntry->insert();
470 if ( count( $tags ) ) {
471 $logEntry->setTags( $tags );
473 $logEntry->publish( $logid );
477 * Edit user groups membership
478 * @param string $username Name of the user.
480 function editUserGroupsForm( $username ) {
481 $status = $this->fetchUser( $username, true );
482 if ( !$status->isOK() ) {
483 $this->getOutput()->addWikiTextAsInterface( $status->getWikiText() );
487 $user = $status->value
;
490 $groups = $user->getGroups();
491 $groupMemberships = $user->getGroupMemberships();
492 $this->showEditUserGroupsForm( $user, $groups, $groupMemberships );
494 // This isn't really ideal logging behavior, but let's not hide the
495 // interwiki logs if we're using them as is.
496 $this->showLogFragment( $user, $this->getOutput() );
500 * Normalize the input username, which may be local or remote, and
501 * return a user (or proxy) object for manipulating it.
503 * Side effects: error output for invalid access
504 * @param string $username
505 * @param bool $writing
508 public function fetchUser( $username, $writing = true ) {
509 $parts = explode( $this->getConfig()->get( 'UserrightsInterwikiDelimiter' ), $username );
510 if ( count( $parts ) < 2 ) {
511 $name = trim( $username );
514 list( $name, $wikiId ) = array_map( 'trim', $parts );
516 if ( WikiMap
::isCurrentWikiId( $wikiId ) ) {
519 if ( $writing && !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
520 return Status
::newFatal( 'userrights-no-interwiki' );
522 if ( !UserRightsProxy
::validDatabase( $wikiId ) ) {
523 return Status
::newFatal( 'userrights-nodatabase', $wikiId );
528 if ( $name === '' ) {
529 return Status
::newFatal( 'nouserspecified' );
532 if ( $name[0] == '#' ) {
533 // Numeric ID can be specified...
534 // We'll do a lookup for the name internally.
535 $id = intval( substr( $name, 1 ) );
537 if ( $wikiId == '' ) {
538 $name = User
::whoIs( $id );
540 $name = UserRightsProxy
::whoIs( $wikiId, $id );
544 return Status
::newFatal( 'noname' );
547 $name = User
::getCanonicalName( $name );
548 if ( $name === false ) {
550 return Status
::newFatal( 'nosuchusershort', $username );
554 if ( $wikiId == '' ) {
555 $user = User
::newFromName( $name );
557 $user = UserRightsProxy
::newFromName( $wikiId, $name );
560 if ( !$user ||
$user->isAnon() ) {
561 return Status
::newFatal( 'nosuchusershort', $username );
564 return Status
::newGood( $user );
574 public function makeGroupNameList( $ids ) {
575 if ( empty( $ids ) ) {
576 return $this->msg( 'rightsnone' )->inContentLanguage()->text();
578 return implode( ', ', $ids );
583 * Output a form to allow searching for a user
585 function switchForm() {
586 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
588 $this->getOutput()->addHTML(
593 'action' => wfScript(),
595 'id' => 'mw-userrights-form1'
598 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
599 Xml
::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
601 $this->msg( 'userrights-user-editname' )->text(),
605 str_replace( '_', ' ', $this->mTarget
),
607 'class' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
609 // Set autofocus on blank input and error input
610 $this->mFetchedUser
=== null ?
[ 'autofocus' => '' ] : []
614 $this->msg( 'editusergroup' )->text()
616 Html
::closeElement( 'fieldset' ) .
617 Html
::closeElement( 'form' ) . "\n"
622 * Show the form to edit group memberships.
624 * @param User|UserRightsProxy $user User or UserRightsProxy you're editing
625 * @param array $groups Array of groups the user is in. Not used by this implementation
626 * anymore, but kept for backward compatibility with subclasses
627 * @param array $groupMemberships Associative array of (group name => UserGroupMembership
628 * object) containing the groups the user is in
630 protected function showEditUserGroupsForm( $user, $groups, $groupMemberships ) {
631 $list = $membersList = $tempList = $tempMembersList = [];
632 foreach ( $groupMemberships as $ugm ) {
633 $linkG = UserGroupMembership
::getLink( $ugm, $this->getContext(), 'html' );
634 $linkM = UserGroupMembership
::getLink( $ugm, $this->getContext(), 'html',
636 if ( $ugm->getExpiry() ) {
637 $tempList[] = $linkG;
638 $tempMembersList[] = $linkM;
641 $membersList[] = $linkM;
647 $autoMembersList = [];
648 if ( $user instanceof User
) {
649 foreach ( Autopromote
::getAutopromoteGroups( $user ) as $group ) {
650 $autoList[] = UserGroupMembership
::getLink( $group, $this->getContext(), 'html' );
651 $autoMembersList[] = UserGroupMembership
::getLink( $group, $this->getContext(),
652 'html', $user->getName() );
656 $language = $this->getLanguage();
657 $displayedList = $this->msg( 'userrights-groupsmember-type' )
659 $language->commaList( array_merge( $tempList, $list ) ),
660 $language->commaList( array_merge( $tempMembersList, $membersList ) )
662 $displayedAutolist = $this->msg( 'userrights-groupsmember-type' )
664 $language->commaList( $autoList ),
665 $language->commaList( $autoMembersList )
669 $count = count( $list ) +
count( $tempList );
671 $grouplist = $this->msg( 'userrights-groupsmember' )
672 ->numParams( $count )
673 ->params( $user->getName() )
675 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
678 $count = count( $autoList );
680 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto' )
681 ->numParams( $count )
682 ->params( $user->getName() )
684 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
687 $userToolLinks = Linker
::userToolLinks(
690 false, /* default for redContribsWhenNoEdits */
691 Linker
::TOOL_LINKS_EMAIL
/* Add "send e-mail" link */
694 list( $groupCheckboxes, $canChangeAny ) =
695 $this->groupCheckboxes( $groupMemberships, $user );
696 $this->getOutput()->addHTML(
701 'action' => $this->getPageTitle()->getLocalURL(),
702 'name' => 'editGroup',
703 'id' => 'mw-userrights-form2'
706 Html
::hidden( 'user', $this->mTarget
) .
707 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget
) ) .
709 'conflictcheck-originalgroups',
710 implode( ',', $user->getGroups() )
711 ) . // Conflict detection
712 Xml
::openElement( 'fieldset' ) .
717 $canChangeAny ?
'userrights-editusergroup' : 'userrights-viewusergroup',
722 $canChangeAny ?
'editinguser' : 'viewinguserrights'
723 )->params( wfEscapeWikiText( $user->getName() ) )
724 ->rawParams( $userToolLinks )->parse()
726 if ( $canChangeAny ) {
727 $this->getOutput()->addHTML(
728 $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
731 Xml
::openElement( 'table', [ 'id' => 'mw-userrights-table-outer' ] ) .
733 <td class='mw-label'>" .
734 Xml
::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
736 <td class='mw-input'>" .
737 Xml
::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ), [
739 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
740 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
741 // Unicode codepoints.
742 'maxlength' => CommentStore
::COMMENT_CHARACTER_LIMIT
,
748 <td class='mw-submit'>" .
749 Xml
::submitButton( $this->msg( 'saveusergroups', $user->getName() )->text(),
750 [ 'name' => 'saveusergroups' ] +
751 Linker
::tooltipAndAccesskeyAttribs( 'userrights-set' )
755 Xml
::closeElement( 'table' ) . "\n"
758 $this->getOutput()->addHTML( $grouplist );
760 $this->getOutput()->addHTML(
761 Xml
::closeElement( 'fieldset' ) .
762 Xml
::closeElement( 'form' ) . "\n"
767 * Returns an array of all groups that may be edited
768 * @return array Array of groups that may be edited.
770 protected static function getAllGroups() {
771 return User
::getAllGroups();
775 * Adds a table with checkboxes where you can select what groups to add/remove
777 * @param UserGroupMembership[] $usergroups Associative array of (group name as string =>
778 * UserGroupMembership object) for groups the user belongs to
780 * @return array Array with 2 elements: the XHTML table element with checkxboes, and
781 * whether any groups are changeable
783 private function groupCheckboxes( $usergroups, $user ) {
784 $allgroups = $this->getAllGroups();
787 // Get the list of preset expiry times from the system message
788 $expiryOptionsMsg = $this->msg( 'userrights-expiry-options' )->inContentLanguage();
789 $expiryOptions = $expiryOptionsMsg->isDisabled() ?
791 explode( ',', $expiryOptionsMsg->text() );
793 // Put all column info into an associative array so that extensions can
794 // more easily manage it.
795 $columns = [ 'unchangeable' => [], 'changeable' => [] ];
797 foreach ( $allgroups as $group ) {
798 $set = isset( $usergroups[$group] );
799 // Users who can add the group, but not remove it, can only lengthen
800 // expiries, not shorten them. So they should only see the expiry
801 // dropdown if the group currently has a finite expiry
802 $canOnlyLengthenExpiry = ( $set && $this->canAdd( $group ) &&
803 !$this->canRemove( $group ) && $usergroups[$group]->getExpiry() );
804 // Should the checkbox be disabled?
805 $disabledCheckbox = !(
806 ( $set && $this->canRemove( $group ) ) ||
807 ( !$set && $this->canAdd( $group ) ) );
808 // Should the expiry elements be disabled?
809 $disabledExpiry = $disabledCheckbox && !$canOnlyLengthenExpiry;
810 // Do we need to point out that this action is irreversible?
811 $irreversible = !$disabledCheckbox && (
812 ( $set && !$this->canAdd( $group ) ) ||
813 ( !$set && !$this->canRemove( $group ) ) );
817 'disabled' => $disabledCheckbox,
818 'disabled-expiry' => $disabledExpiry,
819 'irreversible' => $irreversible
822 if ( $disabledCheckbox && $disabledExpiry ) {
823 $columns['unchangeable'][$group] = $checkbox;
825 $columns['changeable'][$group] = $checkbox;
829 // Build the HTML table
830 $ret .= Xml
::openElement( 'table', [ 'class' => 'mw-userrights-groups' ] ) .
832 foreach ( $columns as $name => $column ) {
833 if ( $column === [] ) {
836 // Messages: userrights-changeable-col, userrights-unchangeable-col
837 $ret .= Xml
::element(
840 $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text()
844 $ret .= "</tr>\n<tr>\n";
845 foreach ( $columns as $column ) {
846 if ( $column === [] ) {
849 $ret .= "\t<td style='vertical-align:top;'>\n";
850 foreach ( $column as $group => $checkbox ) {
851 $attr = [ 'class' => 'mw-userrights-groupcheckbox' ];
852 if ( $checkbox['disabled'] ) {
853 $attr['disabled'] = 'disabled';
856 $member = UserGroupMembership
::getGroupMemberName( $group, $user->getName() );
857 if ( $checkbox['irreversible'] ) {
858 $text = $this->msg( 'userrights-irreversible-marker', $member )->text();
859 } elseif ( $checkbox['disabled'] && !$checkbox['disabled-expiry'] ) {
860 $text = $this->msg( 'userrights-no-shorten-expiry-marker', $member )->text();
864 $checkboxHtml = Xml
::checkLabel( $text, "wpGroup-" . $group,
865 "wpGroup-" . $group, $checkbox['set'], $attr );
867 if ( $this->canProcessExpiries() ) {
868 $uiUser = $this->getUser();
869 $uiLanguage = $this->getLanguage();
871 $currentExpiry = isset( $usergroups[$group] ) ?
872 $usergroups[$group]->getExpiry() :
875 // If the user can't modify the expiry, print the current expiry below
876 // it in plain text. Otherwise provide UI to set/change the expiry
877 if ( $checkbox['set'] &&
878 ( $checkbox['irreversible'] ||
$checkbox['disabled-expiry'] )
880 if ( $currentExpiry ) {
881 $expiryFormatted = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
882 $expiryFormattedD = $uiLanguage->userDate( $currentExpiry, $uiUser );
883 $expiryFormattedT = $uiLanguage->userTime( $currentExpiry, $uiUser );
884 $expiryHtml = $this->msg( 'userrights-expiry-current' )->params(
885 $expiryFormatted, $expiryFormattedD, $expiryFormattedT )->text();
887 $expiryHtml = $this->msg( 'userrights-expiry-none' )->text();
889 // T171345: Add a hidden form element so that other groups can still be manipulated,
890 // otherwise saving errors out with an invalid expiry time for this group.
891 $expiryHtml .= Html
::hidden( "wpExpiry-$group",
892 $currentExpiry ?
'existing' : 'infinite' );
893 $expiryHtml .= "<br />\n";
895 $expiryHtml = Xml
::element( 'span', null,
896 $this->msg( 'userrights-expiry' )->text() );
897 $expiryHtml .= Xml
::openElement( 'span' );
899 // add a form element to set the expiry date
900 $expiryFormOptions = new XmlSelect(
902 "mw-input-wpExpiry-$group", // forward compatibility with HTMLForm
903 $currentExpiry ?
'existing' : 'infinite'
905 if ( $checkbox['disabled-expiry'] ) {
906 $expiryFormOptions->setAttribute( 'disabled', 'disabled' );
909 if ( $currentExpiry ) {
910 $timestamp = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
911 $d = $uiLanguage->userDate( $currentExpiry, $uiUser );
912 $t = $uiLanguage->userTime( $currentExpiry, $uiUser );
913 $existingExpiryMessage = $this->msg( 'userrights-expiry-existing',
914 $timestamp, $d, $t );
915 $expiryFormOptions->addOption( $existingExpiryMessage->text(), 'existing' );
918 $expiryFormOptions->addOption(
919 $this->msg( 'userrights-expiry-none' )->text(),
922 $expiryFormOptions->addOption(
923 $this->msg( 'userrights-expiry-othertime' )->text(),
926 foreach ( $expiryOptions as $option ) {
927 if ( strpos( $option, ":" ) === false ) {
928 $displayText = $value = $option;
930 list( $displayText, $value ) = explode( ":", $option );
932 $expiryFormOptions->addOption( $displayText, htmlspecialchars( $value ) );
935 // Add expiry dropdown
936 $expiryHtml .= $expiryFormOptions->getHTML() . '<br />';
938 // Add custom expiry field
940 'id' => "mw-input-wpExpiry-$group-other",
941 'class' => 'mw-userrights-expiryfield',
943 if ( $checkbox['disabled-expiry'] ) {
944 $attribs['disabled'] = 'disabled';
946 $expiryHtml .= Xml
::input( "wpExpiry-$group-other", 30, '', $attribs );
948 // If the user group is set but the checkbox is disabled, mimic a
949 // checked checkbox in the form submission
950 if ( $checkbox['set'] && $checkbox['disabled'] ) {
951 $expiryHtml .= Html
::hidden( "wpGroup-$group", 1 );
954 $expiryHtml .= Xml
::closeElement( 'span' );
958 'id' => "mw-userrights-nested-wpGroup-$group",
959 'class' => 'mw-userrights-nested',
961 $checkboxHtml .= "\t\t\t" . Xml
::tags( 'div', $divAttribs, $expiryHtml ) . "\n";
963 $ret .= "\t\t" . ( ( $checkbox['disabled'] && $checkbox['disabled-expiry'] )
964 ? Xml
::tags( 'div', [ 'class' => 'mw-userrights-disabled' ], $checkboxHtml )
965 : Xml
::tags( 'div', [], $checkboxHtml )
970 $ret .= Xml
::closeElement( 'tr' ) . Xml
::closeElement( 'table' );
972 return [ $ret, (bool)$columns['changeable'] ];
976 * @param string $group The name of the group to check
977 * @return bool Can we remove the group?
979 private function canRemove( $group ) {
980 $groups = $this->changeableGroups();
984 $groups['remove'] ) ||
( $this->isself
&& in_array( $group, $groups['remove-self'] )
989 * @param string $group The name of the group to check
990 * @return bool Can we add the group?
992 private function canAdd( $group ) {
993 $groups = $this->changeableGroups();
997 $groups['add'] ) ||
( $this->isself
&& in_array( $group, $groups['add-self'] )
1002 * Returns $this->getUser()->changeableGroups()
1005 * 'add' => [ addablegroups ],
1006 * 'remove' => [ removablegroups ],
1007 * 'add-self' => [ addablegroups to self ],
1008 * 'remove-self' => [ removable groups from self ]
1011 function changeableGroups() {
1012 return $this->getUser()->changeableGroups();
1016 * Show a rights log fragment for the specified user
1018 * @param User $user User to show log for
1019 * @param OutputPage $output OutputPage to use
1021 protected function showLogFragment( $user, $output ) {
1022 $rightsLogPage = new LogPage( 'rights' );
1023 $output->addHTML( Xml
::element( 'h2', null, $rightsLogPage->getName()->text() ) );
1024 LogEventsList
::showLogExtract( $output, 'rights', $user->getUserPage() );
1028 * Return an array of subpages beginning with $search that this special page will accept.
1030 * @param string $search Prefix to search for
1031 * @param int $limit Maximum number of results to return (usually 10)
1032 * @param int $offset Number of results to skip (usually 0)
1033 * @return string[] Matching subpages
1035 public function prefixSearchSubpages( $search, $limit, $offset ) {
1036 $user = User
::newFromName( $search );
1038 // No prefix suggestion for invalid user
1041 // Autocomplete subpage as user list - public to allow caching
1042 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
1045 protected function getGroupName() {