Add a 'ChangeUserGroups' hook in Special:Userrights
[lhc/web/wiklou.git] / includes / specials / SpecialUserrights.php
1 <?php
2 /**
3 * Implements Special:Userrights
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special page to allow managing user group membership
26 *
27 * @ingroup SpecialPage
28 */
29 class UserrightsPage extends SpecialPage {
30 /**
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
33 * variable for it.
34 * @var null|string $mTarget
35 */
36 protected $mTarget;
37 /*
38 * @var null|User $mFetchedUser The user object of the target username or null.
39 */
40 protected $mFetchedUser = null;
41 protected $isself = false;
42
43 public function __construct() {
44 parent::__construct( 'Userrights' );
45 }
46
47 public function doesWrites() {
48 return true;
49 }
50
51 /**
52 * @param User $user
53 * @param bool $checkIfSelf
54 * @return bool
55 */
56 public function userCanChangeRights( $user, $checkIfSelf = true ) {
57 $available = $this->changeableGroups();
58 if ( $user->getId() == 0 ) {
59 return false;
60 }
61
62 return !empty( $available['add'] )
63 || !empty( $available['remove'] )
64 || ( ( $this->isself || !$checkIfSelf ) &&
65 ( !empty( $available['add-self'] )
66 || !empty( $available['remove-self'] ) ) );
67 }
68
69 /**
70 * Manage forms to be shown according to posted data.
71 * Depending on the submit button used, call a form or a save function.
72 *
73 * @param string|null $par String if any subpage provided, else null
74 * @throws UserBlockedError|PermissionsError
75 */
76 public function execute( $par ) {
77 $user = $this->getUser();
78 $request = $this->getRequest();
79 $session = $request->getSession();
80 $out = $this->getOutput();
81
82 if ( $par !== null ) {
83 $this->mTarget = $par;
84 } else {
85 $this->mTarget = $request->getVal( 'user' );
86 }
87
88 if ( is_string( $this->mTarget ) ) {
89 $this->mTarget = trim( $this->mTarget );
90 }
91
92 if ( $this->mTarget !== null && User::getCanonicalName( $this->mTarget ) === $user->getName() ) {
93 $this->isself = true;
94 }
95
96 $fetchedStatus = $this->fetchUser( $this->mTarget, true );
97 if ( $fetchedStatus->isOK() ) {
98 $this->mFetchedUser = $fetchedStatus->value;
99 if ( $this->mFetchedUser instanceof User ) {
100 // Set the 'relevant user' in the skin, so it displays links like Contributions,
101 // User logs, UserRights, etc.
102 $this->getSkin()->setRelevantUser( $this->mFetchedUser );
103 }
104 }
105
106 // show a successbox, if the user rights was saved successfully
107 if (
108 $session->get( 'specialUserrightsSaveSuccess' ) &&
109 $this->mFetchedUser !== null
110 ) {
111 // Remove session data for the success message
112 $session->remove( 'specialUserrightsSaveSuccess' );
113
114 $out->addModules( [ 'mediawiki.special.userrights' ] );
115 $out->addModuleStyles( 'mediawiki.notification.convertmessagebox.styles' );
116 $out->addHTML(
117 Html::rawElement(
118 'div',
119 [
120 'class' => 'mw-notify-success successbox',
121 'id' => 'mw-preferences-success',
122 'data-mw-autohide' => 'false',
123 ],
124 Html::element(
125 'p',
126 [],
127 $this->msg( 'savedrights', $this->mFetchedUser->getName() )->text()
128 )
129 )
130 );
131 }
132
133 $this->setHeaders();
134 $this->outputHeader();
135
136 $out->addModuleStyles( 'mediawiki.special' );
137 $this->addHelpLink( 'Help:Assigning permissions' );
138
139 $this->switchForm();
140
141 if (
142 $request->wasPosted() &&
143 $request->getCheck( 'saveusergroups' ) &&
144 $this->mTarget !== null &&
145 $user->matchEditToken( $request->getVal( 'wpEditToken' ), $this->mTarget )
146 ) {
147 /*
148 * If the user is blocked and they only have "partial" access
149 * (e.g. they don't have the userrights permission), then don't
150 * allow them to change any user rights.
151 */
152 if ( $user->isBlocked() && !$user->isAllowed( 'userrights' ) ) {
153 throw new UserBlockedError( $user->getBlock() );
154 }
155
156 $this->checkReadOnly();
157
158 // save settings
159 if ( !$fetchedStatus->isOK() ) {
160 $this->getOutput()->addWikiText( $fetchedStatus->getWikiText() );
161
162 return;
163 }
164
165 $targetUser = $this->mFetchedUser;
166 if ( $targetUser instanceof User ) { // UserRightsProxy doesn't have this method (bug 61252)
167 $targetUser->clearInstanceCache(); // bug 38989
168 }
169
170 if ( $request->getVal( 'conflictcheck-originalgroups' )
171 !== implode( ',', $targetUser->getGroups() )
172 ) {
173 $out->addWikiMsg( 'userrights-conflict' );
174 } else {
175 $this->saveUserGroups(
176 $this->mTarget,
177 $request->getVal( 'user-reason' ),
178 $targetUser
179 );
180
181 // Set session data for the success message
182 $session->set( 'specialUserrightsSaveSuccess', 1 );
183
184 $out->redirect( $this->getSuccessURL() );
185
186 return;
187 }
188 }
189
190 // show some more forms
191 if ( $this->mTarget !== null ) {
192 $this->editUserGroupsForm( $this->mTarget );
193 }
194 }
195
196 function getSuccessURL() {
197 return $this->getPageTitle( $this->mTarget )->getFullURL();
198 }
199
200 /**
201 * Save user groups changes in the database.
202 * Data comes from the editUserGroupsForm() form function
203 *
204 * @param string $username Username to apply changes to.
205 * @param string $reason Reason for group change
206 * @param User|UserRightsProxy $user Target user object.
207 * @return null
208 */
209 function saveUserGroups( $username, $reason, $user ) {
210 $allgroups = $this->getAllGroups();
211 $addgroup = [];
212 $removegroup = [];
213
214 // This could possibly create a highly unlikely race condition if permissions are changed between
215 // when the form is loaded and when the form is saved. Ignoring it for the moment.
216 foreach ( $allgroups as $group ) {
217 // We'll tell it to remove all unchecked groups, and add all checked groups.
218 // Later on, this gets filtered for what can actually be removed
219 if ( $this->getRequest()->getCheck( "wpGroup-$group" ) ) {
220 $addgroup[] = $group;
221 } else {
222 $removegroup[] = $group;
223 }
224 }
225
226 $this->doSaveUserGroups( $user, $addgroup, $removegroup, $reason );
227 }
228
229 /**
230 * Save user groups changes in the database.
231 *
232 * @param User|UserRightsProxy $user
233 * @param array $add Array of groups to add
234 * @param array $remove Array of groups to remove
235 * @param string $reason Reason for group change
236 * @return array Tuple of added, then removed groups
237 */
238 function doSaveUserGroups( $user, $add, $remove, $reason = '' ) {
239 // Validate input set...
240 $isself = $user->getName() == $this->getUser()->getName();
241 $groups = $user->getGroups();
242 $changeable = $this->changeableGroups();
243 $addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : [] );
244 $removable = array_merge( $changeable['remove'], $isself ? $changeable['remove-self'] : [] );
245
246 $remove = array_unique(
247 array_intersect( (array)$remove, $removable, $groups ) );
248 $add = array_unique( array_diff(
249 array_intersect( (array)$add, $addable ),
250 $groups )
251 );
252
253 Hooks::run( 'ChangeUserGroups', [ $this->getUser(), $user, &$add, &$remove ] );
254
255 $oldGroups = $user->getGroups();
256 $newGroups = $oldGroups;
257
258 // Remove then add groups
259 if ( $remove ) {
260 foreach ( $remove as $index => $group ) {
261 if ( !$user->removeGroup( $group ) ) {
262 unset( $remove[$index] );
263 }
264 }
265 $newGroups = array_diff( $newGroups, $remove );
266 }
267 if ( $add ) {
268 foreach ( $add as $index => $group ) {
269 if ( !$user->addGroup( $group ) ) {
270 unset( $add[$index] );
271 }
272 }
273 $newGroups = array_merge( $newGroups, $add );
274 }
275 $newGroups = array_unique( $newGroups );
276
277 // Ensure that caches are cleared
278 $user->invalidateCache();
279
280 // update groups in external authentication database
281 Hooks::run( 'UserGroupsChanged', [ $user, $add, $remove, $this->getUser(), $reason ] );
282 MediaWiki\Auth\AuthManager::callLegacyAuthPlugin(
283 'updateExternalDBGroups', [ $user, $add, $remove ]
284 );
285
286 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) . "\n" );
287 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) . "\n" );
288 // Deprecated in favor of UserGroupsChanged hook
289 Hooks::run( 'UserRights', [ &$user, $add, $remove ], '1.26' );
290
291 if ( $newGroups != $oldGroups ) {
292 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason );
293 }
294
295 return [ $add, $remove ];
296 }
297
298 /**
299 * Add a rights log entry for an action.
300 * @param User $user
301 * @param array $oldGroups
302 * @param array $newGroups
303 * @param array $reason
304 */
305 function addLogEntry( $user, $oldGroups, $newGroups, $reason ) {
306 $logEntry = new ManualLogEntry( 'rights', 'rights' );
307 $logEntry->setPerformer( $this->getUser() );
308 $logEntry->setTarget( $user->getUserPage() );
309 $logEntry->setComment( $reason );
310 $logEntry->setParameters( [
311 '4::oldgroups' => $oldGroups,
312 '5::newgroups' => $newGroups,
313 ] );
314 $logid = $logEntry->insert();
315 $logEntry->publish( $logid );
316 }
317
318 /**
319 * Edit user groups membership
320 * @param string $username Name of the user.
321 */
322 function editUserGroupsForm( $username ) {
323 $status = $this->fetchUser( $username, true );
324 if ( !$status->isOK() ) {
325 $this->getOutput()->addWikiText( $status->getWikiText() );
326
327 return;
328 } else {
329 $user = $status->value;
330 }
331
332 $groups = $user->getGroups();
333
334 $this->showEditUserGroupsForm( $user, $groups );
335
336 // This isn't really ideal logging behavior, but let's not hide the
337 // interwiki logs if we're using them as is.
338 $this->showLogFragment( $user, $this->getOutput() );
339 }
340
341 /**
342 * Normalize the input username, which may be local or remote, and
343 * return a user (or proxy) object for manipulating it.
344 *
345 * Side effects: error output for invalid access
346 * @param string $username
347 * @param bool $writing
348 * @return Status
349 */
350 public function fetchUser( $username, $writing = true ) {
351 $parts = explode( $this->getConfig()->get( 'UserrightsInterwikiDelimiter' ), $username );
352 if ( count( $parts ) < 2 ) {
353 $name = trim( $username );
354 $database = '';
355 } else {
356 list( $name, $database ) = array_map( 'trim', $parts );
357
358 if ( $database == wfWikiID() ) {
359 $database = '';
360 } else {
361 if ( $writing && !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
362 return Status::newFatal( 'userrights-no-interwiki' );
363 }
364 if ( !UserRightsProxy::validDatabase( $database ) ) {
365 return Status::newFatal( 'userrights-nodatabase', $database );
366 }
367 }
368 }
369
370 if ( $name === '' ) {
371 return Status::newFatal( 'nouserspecified' );
372 }
373
374 if ( $name[0] == '#' ) {
375 // Numeric ID can be specified...
376 // We'll do a lookup for the name internally.
377 $id = intval( substr( $name, 1 ) );
378
379 if ( $database == '' ) {
380 $name = User::whoIs( $id );
381 } else {
382 $name = UserRightsProxy::whoIs( $database, $id );
383 }
384
385 if ( !$name ) {
386 return Status::newFatal( 'noname' );
387 }
388 } else {
389 $name = User::getCanonicalName( $name );
390 if ( $name === false ) {
391 // invalid name
392 return Status::newFatal( 'nosuchusershort', $username );
393 }
394 }
395
396 if ( $database == '' ) {
397 $user = User::newFromName( $name );
398 } else {
399 $user = UserRightsProxy::newFromName( $database, $name );
400 }
401
402 if ( !$user || $user->isAnon() ) {
403 return Status::newFatal( 'nosuchusershort', $username );
404 }
405
406 return Status::newGood( $user );
407 }
408
409 /**
410 * @since 1.15
411 *
412 * @param array $ids
413 *
414 * @return string
415 */
416 public function makeGroupNameList( $ids ) {
417 if ( empty( $ids ) ) {
418 return $this->msg( 'rightsnone' )->inContentLanguage()->text();
419 } else {
420 return implode( ', ', $ids );
421 }
422 }
423
424 /**
425 * Output a form to allow searching for a user
426 */
427 function switchForm() {
428 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
429
430 $this->getOutput()->addHTML(
431 Html::openElement(
432 'form',
433 [
434 'method' => 'get',
435 'action' => wfScript(),
436 'name' => 'uluser',
437 'id' => 'mw-userrights-form1'
438 ]
439 ) .
440 Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
441 Xml::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
442 Xml::inputLabel(
443 $this->msg( 'userrights-user-editname' )->text(),
444 'user',
445 'username',
446 30,
447 str_replace( '_', ' ', $this->mTarget ),
448 [
449 'class' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
450 ] + (
451 // Set autofocus on blank input and error input
452 $this->mFetchedUser === null ? [ 'autofocus' => '' ] : []
453 )
454 ) . ' ' .
455 Xml::submitButton(
456 $this->msg( 'editusergroup' )->text()
457 ) .
458 Html::closeElement( 'fieldset' ) .
459 Html::closeElement( 'form' ) . "\n"
460 );
461 }
462
463 /**
464 * Go through used and available groups and return the ones that this
465 * form will be able to manipulate based on the current user's system
466 * permissions.
467 *
468 * @param array $groups List of groups the given user is in
469 * @return array Tuple of addable, then removable groups
470 */
471 protected function splitGroups( $groups ) {
472 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
473
474 $removable = array_intersect(
475 array_merge( $this->isself ? $removeself : [], $removable ),
476 $groups
477 ); // Can't remove groups the user doesn't have
478 $addable = array_diff(
479 array_merge( $this->isself ? $addself : [], $addable ),
480 $groups
481 ); // Can't add groups the user does have
482
483 return [ $addable, $removable ];
484 }
485
486 /**
487 * Show the form to edit group memberships.
488 *
489 * @param User|UserRightsProxy $user User or UserRightsProxy you're editing
490 * @param array $groups Array of groups the user is in
491 */
492 protected function showEditUserGroupsForm( $user, $groups ) {
493 $list = [];
494 $membersList = [];
495 foreach ( $groups as $group ) {
496 $list[] = self::buildGroupLink( $group );
497 $membersList[] = self::buildGroupMemberLink( $group );
498 }
499
500 $autoList = [];
501 $autoMembersList = [];
502 if ( $user instanceof User ) {
503 foreach ( Autopromote::getAutopromoteGroups( $user ) as $group ) {
504 $autoList[] = self::buildGroupLink( $group );
505 $autoMembersList[] = self::buildGroupMemberLink( $group );
506 }
507 }
508
509 $language = $this->getLanguage();
510 $displayedList = $this->msg( 'userrights-groupsmember-type' )
511 ->rawParams(
512 $language->listToText( $list ),
513 $language->listToText( $membersList )
514 )->escaped();
515 $displayedAutolist = $this->msg( 'userrights-groupsmember-type' )
516 ->rawParams(
517 $language->listToText( $autoList ),
518 $language->listToText( $autoMembersList )
519 )->escaped();
520
521 $grouplist = '';
522 $count = count( $list );
523 if ( $count > 0 ) {
524 $grouplist = $this->msg( 'userrights-groupsmember' )
525 ->numParams( $count )
526 ->params( $user->getName() )
527 ->parse();
528 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
529 }
530
531 $count = count( $autoList );
532 if ( $count > 0 ) {
533 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto' )
534 ->numParams( $count )
535 ->params( $user->getName() )
536 ->parse();
537 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
538 }
539
540 $userToolLinks = Linker::userToolLinks(
541 $user->getId(),
542 $user->getName(),
543 false, /* default for redContribsWhenNoEdits */
544 Linker::TOOL_LINKS_EMAIL /* Add "send e-mail" link */
545 );
546
547 list( $groupCheckboxes, $canChangeAny ) = $this->groupCheckboxes( $groups, $user );
548 $this->getOutput()->addHTML(
549 Xml::openElement(
550 'form',
551 [
552 'method' => 'post',
553 'action' => $this->getPageTitle()->getLocalURL(),
554 'name' => 'editGroup',
555 'id' => 'mw-userrights-form2'
556 ]
557 ) .
558 Html::hidden( 'user', $this->mTarget ) .
559 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget ) ) .
560 Html::hidden(
561 'conflictcheck-originalgroups',
562 implode( ',', $user->getGroups() )
563 ) . // Conflict detection
564 Xml::openElement( 'fieldset' ) .
565 Xml::element(
566 'legend',
567 [],
568 $this->msg(
569 $canChangeAny ? 'userrights-editusergroup' : 'userrights-viewusergroup',
570 $user->getName()
571 )->text()
572 ) .
573 $this->msg(
574 $canChangeAny ? 'editinguser' : 'viewinguserrights'
575 )->params( wfEscapeWikiText( $user->getName() ) )
576 ->rawParams( $userToolLinks )->parse()
577 );
578 if ( $canChangeAny ) {
579 $this->getOutput()->addHTML(
580 $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
581 $grouplist .
582 $groupCheckboxes .
583 Xml::openElement( 'table', [ 'id' => 'mw-userrights-table-outer' ] ) .
584 "<tr>
585 <td class='mw-label'>" .
586 Xml::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
587 "</td>
588 <td class='mw-input'>" .
589 Xml::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ),
590 [ 'id' => 'wpReason', 'maxlength' => 255 ] ) .
591 "</td>
592 </tr>
593 <tr>
594 <td></td>
595 <td class='mw-submit'>" .
596 Xml::submitButton( $this->msg( 'saveusergroups', $user->getName() )->text(),
597 [ 'name' => 'saveusergroups' ] +
598 Linker::tooltipAndAccesskeyAttribs( 'userrights-set' )
599 ) .
600 "</td>
601 </tr>" .
602 Xml::closeElement( 'table' ) . "\n"
603 );
604 } else {
605 $this->getOutput()->addHTML( $grouplist );
606 }
607 $this->getOutput()->addHTML(
608 Xml::closeElement( 'fieldset' ) .
609 Xml::closeElement( 'form' ) . "\n"
610 );
611 }
612
613 /**
614 * Format a link to a group description page
615 *
616 * @param string $group
617 * @return string
618 */
619 private static function buildGroupLink( $group ) {
620 return User::makeGroupLinkHTML( $group, User::getGroupName( $group ) );
621 }
622
623 /**
624 * Format a link to a group member description page
625 *
626 * @param string $group
627 * @return string
628 */
629 private static function buildGroupMemberLink( $group ) {
630 return User::makeGroupLinkHTML( $group, User::getGroupMember( $group ) );
631 }
632
633 /**
634 * Returns an array of all groups that may be edited
635 * @return array Array of groups that may be edited.
636 */
637 protected static function getAllGroups() {
638 return User::getAllGroups();
639 }
640
641 /**
642 * Adds a table with checkboxes where you can select what groups to add/remove
643 *
644 * @todo Just pass the username string?
645 * @param array $usergroups Groups the user belongs to
646 * @param User $user
647 * @return Array with 2 elements: the XHTML table element with checkxboes, and
648 * whether any groups are changeable
649 */
650 private function groupCheckboxes( $usergroups, $user ) {
651 $allgroups = $this->getAllGroups();
652 $ret = '';
653
654 // Put all column info into an associative array so that extensions can
655 // more easily manage it.
656 $columns = [ 'unchangeable' => [], 'changeable' => [] ];
657
658 foreach ( $allgroups as $group ) {
659 $set = in_array( $group, $usergroups );
660 // Should the checkbox be disabled?
661 $disabled = !(
662 ( $set && $this->canRemove( $group ) ) ||
663 ( !$set && $this->canAdd( $group ) ) );
664 // Do we need to point out that this action is irreversible?
665 $irreversible = !$disabled && (
666 ( $set && !$this->canAdd( $group ) ) ||
667 ( !$set && !$this->canRemove( $group ) ) );
668
669 $checkbox = [
670 'set' => $set,
671 'disabled' => $disabled,
672 'irreversible' => $irreversible
673 ];
674
675 if ( $disabled ) {
676 $columns['unchangeable'][$group] = $checkbox;
677 } else {
678 $columns['changeable'][$group] = $checkbox;
679 }
680 }
681
682 // Build the HTML table
683 $ret .= Xml::openElement( 'table', [ 'class' => 'mw-userrights-groups' ] ) .
684 "<tr>\n";
685 foreach ( $columns as $name => $column ) {
686 if ( $column === [] ) {
687 continue;
688 }
689 // Messages: userrights-changeable-col, userrights-unchangeable-col
690 $ret .= Xml::element(
691 'th',
692 null,
693 $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text()
694 );
695 }
696
697 $ret .= "</tr>\n<tr>\n";
698 foreach ( $columns as $column ) {
699 if ( $column === [] ) {
700 continue;
701 }
702 $ret .= "\t<td style='vertical-align:top;'>\n";
703 foreach ( $column as $group => $checkbox ) {
704 $attr = $checkbox['disabled'] ? [ 'disabled' => 'disabled' ] : [];
705
706 $member = User::getGroupMember( $group, $user->getName() );
707 if ( $checkbox['irreversible'] ) {
708 $text = $this->msg( 'userrights-irreversible-marker', $member )->text();
709 } else {
710 $text = $member;
711 }
712 $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group,
713 "wpGroup-" . $group, $checkbox['set'], $attr );
714 $ret .= "\t\t" . ( $checkbox['disabled']
715 ? Xml::tags( 'span', [ 'class' => 'mw-userrights-disabled' ], $checkboxHtml )
716 : $checkboxHtml
717 ) . "<br />\n";
718 }
719 $ret .= "\t</td>\n";
720 }
721 $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
722
723 return [ $ret, (bool)$columns['changeable'] ];
724 }
725
726 /**
727 * @param string $group The name of the group to check
728 * @return bool Can we remove the group?
729 */
730 private function canRemove( $group ) {
731 $groups = $this->changeableGroups();
732
733 return in_array(
734 $group,
735 $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] )
736 );
737 }
738
739 /**
740 * @param string $group The name of the group to check
741 * @return bool Can we add the group?
742 */
743 private function canAdd( $group ) {
744 $groups = $this->changeableGroups();
745
746 return in_array(
747 $group,
748 $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] )
749 );
750 }
751
752 /**
753 * Returns $this->getUser()->changeableGroups()
754 *
755 * @return array Array(
756 * 'add' => array( addablegroups ),
757 * 'remove' => array( removablegroups ),
758 * 'add-self' => array( addablegroups to self ),
759 * 'remove-self' => array( removable groups from self )
760 * )
761 */
762 function changeableGroups() {
763 return $this->getUser()->changeableGroups();
764 }
765
766 /**
767 * Show a rights log fragment for the specified user
768 *
769 * @param User $user User to show log for
770 * @param OutputPage $output OutputPage to use
771 */
772 protected function showLogFragment( $user, $output ) {
773 $rightsLogPage = new LogPage( 'rights' );
774 $output->addHTML( Xml::element( 'h2', null, $rightsLogPage->getName()->text() ) );
775 LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage() );
776 }
777
778 /**
779 * Return an array of subpages beginning with $search that this special page will accept.
780 *
781 * @param string $search Prefix to search for
782 * @param int $limit Maximum number of results to return (usually 10)
783 * @param int $offset Number of results to skip (usually 0)
784 * @return string[] Matching subpages
785 */
786 public function prefixSearchSubpages( $search, $limit, $offset ) {
787 $user = User::newFromName( $search );
788 if ( !$user ) {
789 // No prefix suggestion for invalid user
790 return [];
791 }
792 // Autocomplete subpage as user list - public to allow caching
793 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
794 }
795
796 protected function getGroupName() {
797 return 'users';
798 }
799 }
800