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 return !empty( $available['add'] )
69 ||
!empty( $available['remove'] )
70 ||
( ( $isself ||
!$checkIfSelf ) &&
71 ( !empty( $available['add-self'] )
72 ||
!empty( $available['remove-self'] ) ) );
76 * Manage forms to be shown according to posted data.
77 * Depending on the submit button used, call a form or a save function.
79 * @param string|null $par String if any subpage provided, else null
80 * @throws UserBlockedError|PermissionsError
82 public function execute( $par ) {
83 $user = $this->getUser();
84 $request = $this->getRequest();
85 $session = $request->getSession();
86 $out = $this->getOutput();
88 $out->addModules( [ 'mediawiki.special.userrights' ] );
90 $this->mTarget
= $par ??
$request->getVal( 'user' );
92 if ( is_string( $this->mTarget
) ) {
93 $this->mTarget
= trim( $this->mTarget
);
96 if ( $this->mTarget
!== null && User
::getCanonicalName( $this->mTarget
) === $user->getName() ) {
100 $fetchedStatus = $this->fetchUser( $this->mTarget
, true );
101 if ( $fetchedStatus->isOK() ) {
102 $this->mFetchedUser
= $fetchedStatus->value
;
103 if ( $this->mFetchedUser
instanceof User
) {
104 // Set the 'relevant user' in the skin, so it displays links like Contributions,
105 // User logs, UserRights, etc.
106 $this->getSkin()->setRelevantUser( $this->mFetchedUser
);
110 // show a successbox, if the user rights was saved successfully
112 $session->get( 'specialUserrightsSaveSuccess' ) &&
113 $this->mFetchedUser
!== null
115 // Remove session data for the success message
116 $session->remove( 'specialUserrightsSaveSuccess' );
118 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
123 'class' => 'mw-notify-success successbox',
124 'id' => 'mw-preferences-success',
125 'data-mw-autohide' => 'false',
130 $this->msg( 'savedrights', $this->mFetchedUser
->getName() )->text()
137 $this->outputHeader();
139 $out->addModuleStyles( 'mediawiki.special' );
140 $this->addHelpLink( 'Help:Assigning permissions' );
145 $request->wasPosted() &&
146 $request->getCheck( 'saveusergroups' ) &&
147 $this->mTarget
!== null &&
148 $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget
)
151 * If the user is blocked and they only have "partial" access
152 * (e.g. they don't have the userrights permission), then don't
153 * allow them to change any user rights.
155 if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
156 throw new UserBlockedError( $user->getBlock() );
159 $this->checkReadOnly();
162 if ( !$fetchedStatus->isOK() ) {
163 $this->getOutput()->addWikiTextAsInterface( $fetchedStatus->getWikiText() );
168 $targetUser = $this->mFetchedUser
;
169 if ( $targetUser instanceof User
) { // UserRightsProxy doesn't have this method (T63252)
170 $targetUser->clearInstanceCache(); // T40989
173 $conflictCheck = $request->getVal( 'conflictcheck-originalgroups' );
174 $conflictCheck = ( $conflictCheck === '' ) ?
[] : explode( ',', $conflictCheck );
175 $userGroups = $targetUser->getGroups();
177 if ( $userGroups !== $conflictCheck ) {
178 $out->addWikiMsg( 'userrights-conflict' );
180 $status = $this->saveUserGroups(
182 $request->getVal( 'user-reason' ),
186 if ( $status->isOK() ) {
187 // Set session data for the success message
188 $session->set( 'specialUserrightsSaveSuccess', 1 );
190 $out->redirect( $this->getSuccessURL() );
193 // Print an error message and redisplay the form
194 $out->wrapWikiTextAsInterface( 'error', $status->getWikiText() );
199 // show some more forms
200 if ( $this->mTarget
!== null ) {
201 $this->editUserGroupsForm( $this->mTarget
);
205 function getSuccessURL() {
206 return $this->getPageTitle( $this->mTarget
)->getFullURL();
210 * Returns true if this user rights form can set and change user group expiries.
211 * Subclasses may wish to override this to return false.
215 public function canProcessExpiries() {
220 * Converts a user group membership expiry string into a timestamp. Words like
221 * 'existing' or 'other' should have been filtered out before calling this
224 * @param string $expiry
225 * @return string|null|false A string containing a valid timestamp, or null
226 * if the expiry is infinite, or false if the timestamp is not valid
228 public static function expiryToTimestamp( $expiry ) {
229 if ( wfIsInfinity( $expiry ) ) {
233 $unix = strtotime( $expiry );
235 if ( !$unix ||
$unix === -1 ) {
239 // @todo FIXME: Non-qualified absolute times are not in users specified timezone
240 // and there isn't notice about it in the ui (see ProtectionForm::getExpiry)
241 return wfTimestamp( TS_MW
, $unix );
245 * Save user groups changes in the database.
246 * Data comes from the editUserGroupsForm() form function
248 * @param string $username Username to apply changes to.
249 * @param string $reason Reason for group change
250 * @param User|UserRightsProxy $user Target user object.
253 protected function saveUserGroups( $username, $reason, $user ) {
254 $allgroups = $this->getAllGroups();
256 $groupExpiries = []; // associative array of (group name => expiry)
258 $existingUGMs = $user->getGroupMemberships();
260 // This could possibly create a highly unlikely race condition if permissions are changed between
261 // when the form is loaded and when the form is saved. Ignoring it for the moment.
262 foreach ( $allgroups as $group ) {
263 // We'll tell it to remove all unchecked groups, and add all checked groups.
264 // Later on, this gets filtered for what can actually be removed
265 if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
266 $addgroup[] = $group;
268 if ( $this->canProcessExpiries() ) {
269 // read the expiry information from the request
270 $expiryDropdown = $this->getRequest()->getVal( "wpExpiry-$group" );
271 if ( $expiryDropdown === 'existing' ) {
275 if ( $expiryDropdown === 'other' ) {
276 $expiryValue = $this->getRequest()->getVal( "wpExpiry-$group-other" );
278 $expiryValue = $expiryDropdown;
281 // validate the expiry
282 $groupExpiries[$group] = self
::expiryToTimestamp( $expiryValue );
284 if ( $groupExpiries[$group] === false ) {
285 return Status
::newFatal( 'userrights-invalid-expiry', $group );
288 // not allowed to have things expiring in the past
289 if ( $groupExpiries[$group] && $groupExpiries[$group] < wfTimestampNow() ) {
290 return Status
::newFatal( 'userrights-expiry-in-past', $group );
293 // if the user can only add this group (not remove it), the expiry time
294 // cannot be brought forward (T156784)
295 if ( !$this->canRemove( $group ) &&
296 isset( $existingUGMs[$group] ) &&
297 ( $existingUGMs[$group]->getExpiry() ?
: 'infinity' ) >
298 ( $groupExpiries[$group] ?
: 'infinity' )
300 return Status
::newFatal( 'userrights-cannot-shorten-expiry', $group );
304 $removegroup[] = $group;
308 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason, [], $groupExpiries );
310 return Status
::newGood();
314 * Save user groups changes in the database. This function does not throw errors;
315 * instead, it ignores groups that the performer does not have permission to set.
317 * @param User|UserRightsProxy $user
318 * @param array $add Array of groups to add
319 * @param array $remove Array of groups to remove
320 * @param string $reason Reason for group change
321 * @param array $tags Array of change tags to add to the log entry
322 * @param array $groupExpiries Associative array of (group name => expiry),
323 * containing only those groups that are to have new expiry values set
324 * @return array Tuple of added, then removed groups
326 function doSaveUserGroups( $user, array $add, array $remove, $reason = '',
327 array $tags = [], array $groupExpiries = []
329 // Validate input set...
330 $isself = $user->getName() == $this->getUser()->getName();
331 $groups = $user->getGroups();
332 $ugms = $user->getGroupMemberships();
333 $changeable = $this->changeableGroups();
334 $addable = array_merge( $changeable['add'], $isself ?
$changeable['add-self'] : [] );
335 $removable = array_merge( $changeable['remove'], $isself ?
$changeable['remove-self'] : [] );
337 $remove = array_unique(
338 array_intersect( (array)$remove, $removable, $groups ) );
339 $add = array_intersect( (array)$add, $addable );
341 // add only groups that are not already present or that need their expiry updated,
342 // UNLESS the user can only add this group (not remove it) and the expiry time
343 // is being brought forward (T156784)
344 $add = array_filter( $add,
345 function ( $group ) use ( $groups, $groupExpiries, $removable, $ugms ) {
346 if ( isset( $groupExpiries[$group] ) &&
347 !in_array( $group, $removable ) &&
348 isset( $ugms[$group] ) &&
349 ( $ugms[$group]->getExpiry() ?
: 'infinity' ) >
350 ( $groupExpiries[$group] ?
: 'infinity' )
354 return !in_array( $group, $groups ) ||
array_key_exists( $group, $groupExpiries );
357 Hooks
::run( 'ChangeUserGroups', [ $this->getUser(), $user, &$add, &$remove ] );
359 $oldGroups = $groups;
360 $oldUGMs = $user->getGroupMemberships();
361 $newGroups = $oldGroups;
363 // Remove groups, then add new ones/update expiries of existing ones
365 foreach ( $remove as $index => $group ) {
366 if ( !$user->removeGroup( $group ) ) {
367 unset( $remove[$index] );
370 $newGroups = array_diff( $newGroups, $remove );
373 foreach ( $add as $index => $group ) {
374 $expiry = $groupExpiries[$group] ??
null;
375 if ( !$user->addGroup( $group, $expiry ) ) {
376 unset( $add[$index] );
379 $newGroups = array_merge( $newGroups, $add );
381 $newGroups = array_unique( $newGroups );
382 $newUGMs = $user->getGroupMemberships();
384 // Ensure that caches are cleared
385 $user->invalidateCache();
387 // update groups in external authentication database
388 Hooks
::run( 'UserGroupsChanged', [ $user, $add, $remove, $this->getUser(),
389 $reason, $oldUGMs, $newUGMs ] );
390 MediaWiki\Auth\AuthManager
::callLegacyAuthPlugin(
391 'updateExternalDBGroups', [ $user, $add, $remove ]
394 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) . "\n" );
395 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) . "\n" );
396 wfDebug( 'oldUGMs: ' . print_r( $oldUGMs, true ) . "\n" );
397 wfDebug( 'newUGMs: ' . print_r( $newUGMs, true ) . "\n" );
398 // Deprecated in favor of UserGroupsChanged hook
399 Hooks
::run( 'UserRights', [ &$user, $add, $remove ], '1.26' );
401 // Only add a log entry if something actually changed
402 if ( $newGroups != $oldGroups ||
$newUGMs != $oldUGMs ) {
403 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason, $tags, $oldUGMs, $newUGMs );
406 return [ $add, $remove ];
410 * Serialise a UserGroupMembership object for storage in the log_params section
411 * of the logging table. Only keeps essential data, removing redundant fields.
413 * @param UserGroupMembership|null $ugm May be null if things get borked
416 protected static function serialiseUgmForLog( $ugm ) {
417 if ( !$ugm instanceof UserGroupMembership
) {
420 return [ 'expiry' => $ugm->getExpiry() ];
424 * Add a rights log entry for an action.
425 * @param User|UserRightsProxy $user
426 * @param array $oldGroups
427 * @param array $newGroups
428 * @param string $reason
429 * @param array $tags Change tags for the log entry
430 * @param array $oldUGMs Associative array of (group name => UserGroupMembership)
431 * @param array $newUGMs Associative array of (group name => UserGroupMembership)
433 protected function addLogEntry( $user, array $oldGroups, array $newGroups, $reason,
434 array $tags, array $oldUGMs, array $newUGMs
436 // make sure $oldUGMs and $newUGMs are in the same order, and serialise
437 // each UGM object to a simplified array
438 $oldUGMs = array_map( function ( $group ) use ( $oldUGMs ) {
439 return isset( $oldUGMs[$group] ) ?
440 self
::serialiseUgmForLog( $oldUGMs[$group] ) :
443 $newUGMs = array_map( function ( $group ) use ( $newUGMs ) {
444 return isset( $newUGMs[$group] ) ?
445 self
::serialiseUgmForLog( $newUGMs[$group] ) :
449 $logEntry = new ManualLogEntry( 'rights', 'rights' );
450 $logEntry->setPerformer( $this->getUser() );
451 $logEntry->setTarget( $user->getUserPage() );
452 $logEntry->setComment( $reason );
453 $logEntry->setParameters( [
454 '4::oldgroups' => $oldGroups,
455 '5::newgroups' => $newGroups,
456 'oldmetadata' => $oldUGMs,
457 'newmetadata' => $newUGMs,
459 $logid = $logEntry->insert();
460 if ( count( $tags ) ) {
461 $logEntry->setTags( $tags );
463 $logEntry->publish( $logid );
467 * Edit user groups membership
468 * @param string $username Name of the user.
470 function editUserGroupsForm( $username ) {
471 $status = $this->fetchUser( $username, true );
472 if ( !$status->isOK() ) {
473 $this->getOutput()->addWikiTextAsInterface( $status->getWikiText() );
477 $user = $status->value
;
480 $groups = $user->getGroups();
481 $groupMemberships = $user->getGroupMemberships();
482 $this->showEditUserGroupsForm( $user, $groups, $groupMemberships );
484 // This isn't really ideal logging behavior, but let's not hide the
485 // interwiki logs if we're using them as is.
486 $this->showLogFragment( $user, $this->getOutput() );
490 * Normalize the input username, which may be local or remote, and
491 * return a user (or proxy) object for manipulating it.
493 * Side effects: error output for invalid access
494 * @param string $username
495 * @param bool $writing
498 public function fetchUser( $username, $writing = true ) {
499 $parts = explode( $this->getConfig()->get( 'UserrightsInterwikiDelimiter' ), $username );
500 if ( count( $parts ) < 2 ) {
501 $name = trim( $username );
504 list( $name, $wikiId ) = array_map( 'trim', $parts );
506 if ( WikiMap
::isCurrentWikiId( $wikiId ) ) {
509 if ( $writing && !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
510 return Status
::newFatal( 'userrights-no-interwiki' );
512 if ( !UserRightsProxy
::validDatabase( $wikiId ) ) {
513 return Status
::newFatal( 'userrights-nodatabase', $wikiId );
518 if ( $name === '' ) {
519 return Status
::newFatal( 'nouserspecified' );
522 if ( $name[0] == '#' ) {
523 // Numeric ID can be specified...
524 // We'll do a lookup for the name internally.
525 $id = intval( substr( $name, 1 ) );
527 if ( $wikiId == '' ) {
528 $name = User
::whoIs( $id );
530 $name = UserRightsProxy
::whoIs( $wikiId, $id );
534 return Status
::newFatal( 'noname' );
537 $name = User
::getCanonicalName( $name );
538 if ( $name === false ) {
540 return Status
::newFatal( 'nosuchusershort', $username );
544 if ( $wikiId == '' ) {
545 $user = User
::newFromName( $name );
547 $user = UserRightsProxy
::newFromName( $wikiId, $name );
550 if ( !$user ||
$user->isAnon() ) {
551 return Status
::newFatal( 'nosuchusershort', $username );
554 return Status
::newGood( $user );
564 public function makeGroupNameList( $ids ) {
565 if ( empty( $ids ) ) {
566 return $this->msg( 'rightsnone' )->inContentLanguage()->text();
568 return implode( ', ', $ids );
573 * Output a form to allow searching for a user
575 function switchForm() {
576 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
578 $this->getOutput()->addHTML(
583 'action' => wfScript(),
585 'id' => 'mw-userrights-form1'
588 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
589 Xml
::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
591 $this->msg( 'userrights-user-editname' )->text(),
595 str_replace( '_', ' ', $this->mTarget
),
597 'class' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
599 // Set autofocus on blank input and error input
600 $this->mFetchedUser
=== null ?
[ 'autofocus' => '' ] : []
604 $this->msg( 'editusergroup' )->text()
606 Html
::closeElement( 'fieldset' ) .
607 Html
::closeElement( 'form' ) . "\n"
612 * Show the form to edit group memberships.
614 * @param User|UserRightsProxy $user User or UserRightsProxy you're editing
615 * @param array $groups Array of groups the user is in. Not used by this implementation
616 * anymore, but kept for backward compatibility with subclasses
617 * @param array $groupMemberships Associative array of (group name => UserGroupMembership
618 * object) containing the groups the user is in
620 protected function showEditUserGroupsForm( $user, $groups, $groupMemberships ) {
621 $list = $membersList = $tempList = $tempMembersList = [];
622 foreach ( $groupMemberships as $ugm ) {
623 $linkG = UserGroupMembership
::getLink( $ugm, $this->getContext(), 'html' );
624 $linkM = UserGroupMembership
::getLink( $ugm, $this->getContext(), 'html',
626 if ( $ugm->getExpiry() ) {
627 $tempList[] = $linkG;
628 $tempMembersList[] = $linkM;
631 $membersList[] = $linkM;
637 $autoMembersList = [];
638 if ( $user instanceof User
) {
639 foreach ( Autopromote
::getAutopromoteGroups( $user ) as $group ) {
640 $autoList[] = UserGroupMembership
::getLink( $group, $this->getContext(), 'html' );
641 $autoMembersList[] = UserGroupMembership
::getLink( $group, $this->getContext(),
642 'html', $user->getName() );
646 $language = $this->getLanguage();
647 $displayedList = $this->msg( 'userrights-groupsmember-type' )
649 $language->commaList( array_merge( $tempList, $list ) ),
650 $language->commaList( array_merge( $tempMembersList, $membersList ) )
652 $displayedAutolist = $this->msg( 'userrights-groupsmember-type' )
654 $language->commaList( $autoList ),
655 $language->commaList( $autoMembersList )
659 $count = count( $list ) +
count( $tempList );
661 $grouplist = $this->msg( 'userrights-groupsmember' )
662 ->numParams( $count )
663 ->params( $user->getName() )
665 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
668 $count = count( $autoList );
670 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto' )
671 ->numParams( $count )
672 ->params( $user->getName() )
674 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
677 $userToolLinks = Linker
::userToolLinks(
680 false, /* default for redContribsWhenNoEdits */
681 Linker
::TOOL_LINKS_EMAIL
/* Add "send e-mail" link */
684 list( $groupCheckboxes, $canChangeAny ) =
685 $this->groupCheckboxes( $groupMemberships, $user );
686 $this->getOutput()->addHTML(
691 'action' => $this->getPageTitle()->getLocalURL(),
692 'name' => 'editGroup',
693 'id' => 'mw-userrights-form2'
696 Html
::hidden( 'user', $this->mTarget
) .
697 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget
) ) .
699 'conflictcheck-originalgroups',
700 implode( ',', $user->getGroups() )
701 ) . // Conflict detection
702 Xml
::openElement( 'fieldset' ) .
707 $canChangeAny ?
'userrights-editusergroup' : 'userrights-viewusergroup',
712 $canChangeAny ?
'editinguser' : 'viewinguserrights'
713 )->params( wfEscapeWikiText( $user->getName() ) )
714 ->rawParams( $userToolLinks )->parse()
716 if ( $canChangeAny ) {
717 $conf = $this->getConfig();
718 $oldCommentSchema = $conf->get( 'CommentTableSchemaMigrationStage' ) === MIGRATION_OLD
;
719 $this->getOutput()->addHTML(
720 $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
723 Xml
::openElement( 'table', [ 'id' => 'mw-userrights-table-outer' ] ) .
725 <td class='mw-label'>" .
726 Xml
::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
728 <td class='mw-input'>" .
729 Xml
::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ), [
731 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
732 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
733 // Unicode codepoints (or 255 UTF-8 bytes for old schema).
734 'maxlength' => $oldCommentSchema ?
255 : CommentStore
::COMMENT_CHARACTER_LIMIT
,
740 <td class='mw-submit'>" .
741 Xml
::submitButton( $this->msg( 'saveusergroups', $user->getName() )->text(),
742 [ 'name' => 'saveusergroups' ] +
743 Linker
::tooltipAndAccesskeyAttribs( 'userrights-set' )
747 Xml
::closeElement( 'table' ) . "\n"
750 $this->getOutput()->addHTML( $grouplist );
752 $this->getOutput()->addHTML(
753 Xml
::closeElement( 'fieldset' ) .
754 Xml
::closeElement( 'form' ) . "\n"
759 * Returns an array of all groups that may be edited
760 * @return array Array of groups that may be edited.
762 protected static function getAllGroups() {
763 return User
::getAllGroups();
767 * Adds a table with checkboxes where you can select what groups to add/remove
769 * @param UserGroupMembership[] $usergroups Associative array of (group name as string =>
770 * UserGroupMembership object) for groups the user belongs to
772 * @return array Array with 2 elements: the XHTML table element with checkxboes, and
773 * whether any groups are changeable
775 private function groupCheckboxes( $usergroups, $user ) {
776 $allgroups = $this->getAllGroups();
779 // Get the list of preset expiry times from the system message
780 $expiryOptionsMsg = $this->msg( 'userrights-expiry-options' )->inContentLanguage();
781 $expiryOptions = $expiryOptionsMsg->isDisabled() ?
783 explode( ',', $expiryOptionsMsg->text() );
785 // Put all column info into an associative array so that extensions can
786 // more easily manage it.
787 $columns = [ 'unchangeable' => [], 'changeable' => [] ];
789 foreach ( $allgroups as $group ) {
790 $set = isset( $usergroups[$group] );
791 // Users who can add the group, but not remove it, can only lengthen
792 // expiries, not shorten them. So they should only see the expiry
793 // dropdown if the group currently has a finite expiry
794 $canOnlyLengthenExpiry = ( $set && $this->canAdd( $group ) &&
795 !$this->canRemove( $group ) && $usergroups[$group]->getExpiry() );
796 // Should the checkbox be disabled?
797 $disabledCheckbox = !(
798 ( $set && $this->canRemove( $group ) ) ||
799 ( !$set && $this->canAdd( $group ) ) );
800 // Should the expiry elements be disabled?
801 $disabledExpiry = $disabledCheckbox && !$canOnlyLengthenExpiry;
802 // Do we need to point out that this action is irreversible?
803 $irreversible = !$disabledCheckbox && (
804 ( $set && !$this->canAdd( $group ) ) ||
805 ( !$set && !$this->canRemove( $group ) ) );
809 'disabled' => $disabledCheckbox,
810 'disabled-expiry' => $disabledExpiry,
811 'irreversible' => $irreversible
814 if ( $disabledCheckbox && $disabledExpiry ) {
815 $columns['unchangeable'][$group] = $checkbox;
817 $columns['changeable'][$group] = $checkbox;
821 // Build the HTML table
822 $ret .= Xml
::openElement( 'table', [ 'class' => 'mw-userrights-groups' ] ) .
824 foreach ( $columns as $name => $column ) {
825 if ( $column === [] ) {
828 // Messages: userrights-changeable-col, userrights-unchangeable-col
829 $ret .= Xml
::element(
832 $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text()
836 $ret .= "</tr>\n<tr>\n";
837 foreach ( $columns as $column ) {
838 if ( $column === [] ) {
841 $ret .= "\t<td style='vertical-align:top;'>\n";
842 foreach ( $column as $group => $checkbox ) {
843 $attr = [ 'class' => 'mw-userrights-groupcheckbox' ];
844 if ( $checkbox['disabled'] ) {
845 $attr['disabled'] = 'disabled';
848 $member = UserGroupMembership
::getGroupMemberName( $group, $user->getName() );
849 if ( $checkbox['irreversible'] ) {
850 $text = $this->msg( 'userrights-irreversible-marker', $member )->text();
851 } elseif ( $checkbox['disabled'] && !$checkbox['disabled-expiry'] ) {
852 $text = $this->msg( 'userrights-no-shorten-expiry-marker', $member )->text();
856 $checkboxHtml = Xml
::checkLabel( $text, "wpGroup-" . $group,
857 "wpGroup-" . $group, $checkbox['set'], $attr );
859 if ( $this->canProcessExpiries() ) {
860 $uiUser = $this->getUser();
861 $uiLanguage = $this->getLanguage();
863 $currentExpiry = isset( $usergroups[$group] ) ?
864 $usergroups[$group]->getExpiry() :
867 // If the user can't modify the expiry, print the current expiry below
868 // it in plain text. Otherwise provide UI to set/change the expiry
869 if ( $checkbox['set'] &&
870 ( $checkbox['irreversible'] ||
$checkbox['disabled-expiry'] )
872 if ( $currentExpiry ) {
873 $expiryFormatted = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
874 $expiryFormattedD = $uiLanguage->userDate( $currentExpiry, $uiUser );
875 $expiryFormattedT = $uiLanguage->userTime( $currentExpiry, $uiUser );
876 $expiryHtml = $this->msg( 'userrights-expiry-current' )->params(
877 $expiryFormatted, $expiryFormattedD, $expiryFormattedT )->text();
879 $expiryHtml = $this->msg( 'userrights-expiry-none' )->text();
881 // T171345: Add a hidden form element so that other groups can still be manipulated,
882 // otherwise saving errors out with an invalid expiry time for this group.
883 $expiryHtml .= Html
::hidden( "wpExpiry-$group",
884 $currentExpiry ?
'existing' : 'infinite' );
885 $expiryHtml .= "<br />\n";
887 $expiryHtml = Xml
::element( 'span', null,
888 $this->msg( 'userrights-expiry' )->text() );
889 $expiryHtml .= Xml
::openElement( 'span' );
891 // add a form element to set the expiry date
892 $expiryFormOptions = new XmlSelect(
894 "mw-input-wpExpiry-$group", // forward compatibility with HTMLForm
895 $currentExpiry ?
'existing' : 'infinite'
897 if ( $checkbox['disabled-expiry'] ) {
898 $expiryFormOptions->setAttribute( 'disabled', 'disabled' );
901 if ( $currentExpiry ) {
902 $timestamp = $uiLanguage->userTimeAndDate( $currentExpiry, $uiUser );
903 $d = $uiLanguage->userDate( $currentExpiry, $uiUser );
904 $t = $uiLanguage->userTime( $currentExpiry, $uiUser );
905 $existingExpiryMessage = $this->msg( 'userrights-expiry-existing',
906 $timestamp, $d, $t );
907 $expiryFormOptions->addOption( $existingExpiryMessage->text(), 'existing' );
910 $expiryFormOptions->addOption(
911 $this->msg( 'userrights-expiry-none' )->text(),
914 $expiryFormOptions->addOption(
915 $this->msg( 'userrights-expiry-othertime' )->text(),
918 foreach ( $expiryOptions as $option ) {
919 if ( strpos( $option, ":" ) === false ) {
920 $displayText = $value = $option;
922 list( $displayText, $value ) = explode( ":", $option );
924 $expiryFormOptions->addOption( $displayText, htmlspecialchars( $value ) );
927 // Add expiry dropdown
928 $expiryHtml .= $expiryFormOptions->getHTML() . '<br />';
930 // Add custom expiry field
932 'id' => "mw-input-wpExpiry-$group-other",
933 'class' => 'mw-userrights-expiryfield',
935 if ( $checkbox['disabled-expiry'] ) {
936 $attribs['disabled'] = 'disabled';
938 $expiryHtml .= Xml
::input( "wpExpiry-$group-other", 30, '', $attribs );
940 // If the user group is set but the checkbox is disabled, mimic a
941 // checked checkbox in the form submission
942 if ( $checkbox['set'] && $checkbox['disabled'] ) {
943 $expiryHtml .= Html
::hidden( "wpGroup-$group", 1 );
946 $expiryHtml .= Xml
::closeElement( 'span' );
950 'id' => "mw-userrights-nested-wpGroup-$group",
951 'class' => 'mw-userrights-nested',
953 $checkboxHtml .= "\t\t\t" . Xml
::tags( 'div', $divAttribs, $expiryHtml ) . "\n";
955 $ret .= "\t\t" . ( ( $checkbox['disabled'] && $checkbox['disabled-expiry'] )
956 ? Xml
::tags( 'div', [ 'class' => 'mw-userrights-disabled' ], $checkboxHtml )
957 : Xml
::tags( 'div', [], $checkboxHtml )
962 $ret .= Xml
::closeElement( 'tr' ) . Xml
::closeElement( 'table' );
964 return [ $ret, (bool)$columns['changeable'] ];
968 * @param string $group The name of the group to check
969 * @return bool Can we remove the group?
971 private function canRemove( $group ) {
972 $groups = $this->changeableGroups();
976 $groups['remove'] ) ||
( $this->isself
&& in_array( $group, $groups['remove-self'] )
981 * @param string $group The name of the group to check
982 * @return bool Can we add the group?
984 private function canAdd( $group ) {
985 $groups = $this->changeableGroups();
989 $groups['add'] ) ||
( $this->isself
&& in_array( $group, $groups['add-self'] )
994 * Returns $this->getUser()->changeableGroups()
996 * @return array Array(
997 * 'add' => array( addablegroups ),
998 * 'remove' => array( removablegroups ),
999 * 'add-self' => array( addablegroups to self ),
1000 * 'remove-self' => array( removable groups from self )
1003 function changeableGroups() {
1004 return $this->getUser()->changeableGroups();
1008 * Show a rights log fragment for the specified user
1010 * @param User $user User to show log for
1011 * @param OutputPage $output OutputPage to use
1013 protected function showLogFragment( $user, $output ) {
1014 $rightsLogPage = new LogPage( 'rights' );
1015 $output->addHTML( Xml
::element( 'h2', null, $rightsLogPage->getName()->text() ) );
1016 LogEventsList
::showLogExtract( $output, 'rights', $user->getUserPage() );
1020 * Return an array of subpages beginning with $search that this special page will accept.
1022 * @param string $search Prefix to search for
1023 * @param int $limit Maximum number of results to return (usually 10)
1024 * @param int $offset Number of results to skip (usually 0)
1025 * @return string[] Matching subpages
1027 public function prefixSearchSubpages( $search, $limit, $offset ) {
1028 $user = User
::newFromName( $search );
1030 // No prefix suggestion for invalid user
1033 // Autocomplete subpage as user list - public to allow caching
1034 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
1037 protected function getGroupName() {