Merge "Remove trailing empty lines in PHP"
[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 * @param array $tags Array of change tags to add to the log entry
237 * @return array Tuple of added, then removed groups
238 */
239 function doSaveUserGroups( $user, $add, $remove, $reason = '', $tags = [] ) {
240 // Validate input set...
241 $isself = $user->getName() == $this->getUser()->getName();
242 $groups = $user->getGroups();
243 $changeable = $this->changeableGroups();
244 $addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : [] );
245 $removable = array_merge( $changeable['remove'], $isself ? $changeable['remove-self'] : [] );
246
247 $remove = array_unique(
248 array_intersect( (array)$remove, $removable, $groups ) );
249 $add = array_unique( array_diff(
250 array_intersect( (array)$add, $addable ),
251 $groups )
252 );
253
254 Hooks::run( 'ChangeUserGroups', [ $this->getUser(), $user, &$add, &$remove ] );
255
256 $oldGroups = $user->getGroups();
257 $newGroups = $oldGroups;
258
259 // Remove then add groups
260 if ( $remove ) {
261 foreach ( $remove as $index => $group ) {
262 if ( !$user->removeGroup( $group ) ) {
263 unset( $remove[$index] );
264 }
265 }
266 $newGroups = array_diff( $newGroups, $remove );
267 }
268 if ( $add ) {
269 foreach ( $add as $index => $group ) {
270 if ( !$user->addGroup( $group ) ) {
271 unset( $add[$index] );
272 }
273 }
274 $newGroups = array_merge( $newGroups, $add );
275 }
276 $newGroups = array_unique( $newGroups );
277
278 // Ensure that caches are cleared
279 $user->invalidateCache();
280
281 // update groups in external authentication database
282 Hooks::run( 'UserGroupsChanged', [ $user, $add, $remove, $this->getUser(), $reason ] );
283 MediaWiki\Auth\AuthManager::callLegacyAuthPlugin(
284 'updateExternalDBGroups', [ $user, $add, $remove ]
285 );
286
287 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) . "\n" );
288 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) . "\n" );
289 // Deprecated in favor of UserGroupsChanged hook
290 Hooks::run( 'UserRights', [ &$user, $add, $remove ], '1.26' );
291
292 if ( $newGroups != $oldGroups ) {
293 $this->addLogEntry( $user, $oldGroups, $newGroups, $reason, $tags );
294 }
295
296 return [ $add, $remove ];
297 }
298
299 /**
300 * Add a rights log entry for an action.
301 * @param User $user
302 * @param array $oldGroups
303 * @param array $newGroups
304 * @param array $reason
305 * @param array $tags
306 */
307 function addLogEntry( $user, $oldGroups, $newGroups, $reason, $tags ) {
308 $logEntry = new ManualLogEntry( 'rights', 'rights' );
309 $logEntry->setPerformer( $this->getUser() );
310 $logEntry->setTarget( $user->getUserPage() );
311 $logEntry->setComment( $reason );
312 $logEntry->setParameters( [
313 '4::oldgroups' => $oldGroups,
314 '5::newgroups' => $newGroups,
315 ] );
316 $logid = $logEntry->insert();
317 if ( count( $tags ) ) {
318 $logEntry->setTags( $tags );
319 }
320 $logEntry->publish( $logid );
321 }
322
323 /**
324 * Edit user groups membership
325 * @param string $username Name of the user.
326 */
327 function editUserGroupsForm( $username ) {
328 $status = $this->fetchUser( $username, true );
329 if ( !$status->isOK() ) {
330 $this->getOutput()->addWikiText( $status->getWikiText() );
331
332 return;
333 } else {
334 $user = $status->value;
335 }
336
337 $groups = $user->getGroups();
338
339 $this->showEditUserGroupsForm( $user, $groups );
340
341 // This isn't really ideal logging behavior, but let's not hide the
342 // interwiki logs if we're using them as is.
343 $this->showLogFragment( $user, $this->getOutput() );
344 }
345
346 /**
347 * Normalize the input username, which may be local or remote, and
348 * return a user (or proxy) object for manipulating it.
349 *
350 * Side effects: error output for invalid access
351 * @param string $username
352 * @param bool $writing
353 * @return Status
354 */
355 public function fetchUser( $username, $writing = true ) {
356 $parts = explode( $this->getConfig()->get( 'UserrightsInterwikiDelimiter' ), $username );
357 if ( count( $parts ) < 2 ) {
358 $name = trim( $username );
359 $database = '';
360 } else {
361 list( $name, $database ) = array_map( 'trim', $parts );
362
363 if ( $database == wfWikiID() ) {
364 $database = '';
365 } else {
366 if ( $writing && !$this->getUser()->isAllowed( 'userrights-interwiki' ) ) {
367 return Status::newFatal( 'userrights-no-interwiki' );
368 }
369 if ( !UserRightsProxy::validDatabase( $database ) ) {
370 return Status::newFatal( 'userrights-nodatabase', $database );
371 }
372 }
373 }
374
375 if ( $name === '' ) {
376 return Status::newFatal( 'nouserspecified' );
377 }
378
379 if ( $name[0] == '#' ) {
380 // Numeric ID can be specified...
381 // We'll do a lookup for the name internally.
382 $id = intval( substr( $name, 1 ) );
383
384 if ( $database == '' ) {
385 $name = User::whoIs( $id );
386 } else {
387 $name = UserRightsProxy::whoIs( $database, $id );
388 }
389
390 if ( !$name ) {
391 return Status::newFatal( 'noname' );
392 }
393 } else {
394 $name = User::getCanonicalName( $name );
395 if ( $name === false ) {
396 // invalid name
397 return Status::newFatal( 'nosuchusershort', $username );
398 }
399 }
400
401 if ( $database == '' ) {
402 $user = User::newFromName( $name );
403 } else {
404 $user = UserRightsProxy::newFromName( $database, $name );
405 }
406
407 if ( !$user || $user->isAnon() ) {
408 return Status::newFatal( 'nosuchusershort', $username );
409 }
410
411 return Status::newGood( $user );
412 }
413
414 /**
415 * @since 1.15
416 *
417 * @param array $ids
418 *
419 * @return string
420 */
421 public function makeGroupNameList( $ids ) {
422 if ( empty( $ids ) ) {
423 return $this->msg( 'rightsnone' )->inContentLanguage()->text();
424 } else {
425 return implode( ', ', $ids );
426 }
427 }
428
429 /**
430 * Output a form to allow searching for a user
431 */
432 function switchForm() {
433 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
434
435 $this->getOutput()->addHTML(
436 Html::openElement(
437 'form',
438 [
439 'method' => 'get',
440 'action' => wfScript(),
441 'name' => 'uluser',
442 'id' => 'mw-userrights-form1'
443 ]
444 ) .
445 Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
446 Xml::fieldset( $this->msg( 'userrights-lookup-user' )->text() ) .
447 Xml::inputLabel(
448 $this->msg( 'userrights-user-editname' )->text(),
449 'user',
450 'username',
451 30,
452 str_replace( '_', ' ', $this->mTarget ),
453 [
454 'class' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
455 ] + (
456 // Set autofocus on blank input and error input
457 $this->mFetchedUser === null ? [ 'autofocus' => '' ] : []
458 )
459 ) . ' ' .
460 Xml::submitButton(
461 $this->msg( 'editusergroup' )->text()
462 ) .
463 Html::closeElement( 'fieldset' ) .
464 Html::closeElement( 'form' ) . "\n"
465 );
466 }
467
468 /**
469 * Go through used and available groups and return the ones that this
470 * form will be able to manipulate based on the current user's system
471 * permissions.
472 *
473 * @param array $groups List of groups the given user is in
474 * @return array Tuple of addable, then removable groups
475 */
476 protected function splitGroups( $groups ) {
477 list( $addable, $removable, $addself, $removeself ) = array_values( $this->changeableGroups() );
478
479 $removable = array_intersect(
480 array_merge( $this->isself ? $removeself : [], $removable ),
481 $groups
482 ); // Can't remove groups the user doesn't have
483 $addable = array_diff(
484 array_merge( $this->isself ? $addself : [], $addable ),
485 $groups
486 ); // Can't add groups the user does have
487
488 return [ $addable, $removable ];
489 }
490
491 /**
492 * Show the form to edit group memberships.
493 *
494 * @param User|UserRightsProxy $user User or UserRightsProxy you're editing
495 * @param array $groups Array of groups the user is in
496 */
497 protected function showEditUserGroupsForm( $user, $groups ) {
498 $list = [];
499 $membersList = [];
500 foreach ( $groups as $group ) {
501 $list[] = self::buildGroupLink( $group );
502 $membersList[] = self::buildGroupMemberLink( $group );
503 }
504
505 $autoList = [];
506 $autoMembersList = [];
507 if ( $user instanceof User ) {
508 foreach ( Autopromote::getAutopromoteGroups( $user ) as $group ) {
509 $autoList[] = self::buildGroupLink( $group );
510 $autoMembersList[] = self::buildGroupMemberLink( $group );
511 }
512 }
513
514 $language = $this->getLanguage();
515 $displayedList = $this->msg( 'userrights-groupsmember-type' )
516 ->rawParams(
517 $language->listToText( $list ),
518 $language->listToText( $membersList )
519 )->escaped();
520 $displayedAutolist = $this->msg( 'userrights-groupsmember-type' )
521 ->rawParams(
522 $language->listToText( $autoList ),
523 $language->listToText( $autoMembersList )
524 )->escaped();
525
526 $grouplist = '';
527 $count = count( $list );
528 if ( $count > 0 ) {
529 $grouplist = $this->msg( 'userrights-groupsmember' )
530 ->numParams( $count )
531 ->params( $user->getName() )
532 ->parse();
533 $grouplist = '<p>' . $grouplist . ' ' . $displayedList . "</p>\n";
534 }
535
536 $count = count( $autoList );
537 if ( $count > 0 ) {
538 $autogrouplistintro = $this->msg( 'userrights-groupsmember-auto' )
539 ->numParams( $count )
540 ->params( $user->getName() )
541 ->parse();
542 $grouplist .= '<p>' . $autogrouplistintro . ' ' . $displayedAutolist . "</p>\n";
543 }
544
545 $userToolLinks = Linker::userToolLinks(
546 $user->getId(),
547 $user->getName(),
548 false, /* default for redContribsWhenNoEdits */
549 Linker::TOOL_LINKS_EMAIL /* Add "send e-mail" link */
550 );
551
552 list( $groupCheckboxes, $canChangeAny ) = $this->groupCheckboxes( $groups, $user );
553 $this->getOutput()->addHTML(
554 Xml::openElement(
555 'form',
556 [
557 'method' => 'post',
558 'action' => $this->getPageTitle()->getLocalURL(),
559 'name' => 'editGroup',
560 'id' => 'mw-userrights-form2'
561 ]
562 ) .
563 Html::hidden( 'user', $this->mTarget ) .
564 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken( $this->mTarget ) ) .
565 Html::hidden(
566 'conflictcheck-originalgroups',
567 implode( ',', $user->getGroups() )
568 ) . // Conflict detection
569 Xml::openElement( 'fieldset' ) .
570 Xml::element(
571 'legend',
572 [],
573 $this->msg(
574 $canChangeAny ? 'userrights-editusergroup' : 'userrights-viewusergroup',
575 $user->getName()
576 )->text()
577 ) .
578 $this->msg(
579 $canChangeAny ? 'editinguser' : 'viewinguserrights'
580 )->params( wfEscapeWikiText( $user->getName() ) )
581 ->rawParams( $userToolLinks )->parse()
582 );
583 if ( $canChangeAny ) {
584 $this->getOutput()->addHTML(
585 $this->msg( 'userrights-groups-help', $user->getName() )->parse() .
586 $grouplist .
587 $groupCheckboxes .
588 Xml::openElement( 'table', [ 'id' => 'mw-userrights-table-outer' ] ) .
589 "<tr>
590 <td class='mw-label'>" .
591 Xml::label( $this->msg( 'userrights-reason' )->text(), 'wpReason' ) .
592 "</td>
593 <td class='mw-input'>" .
594 Xml::input( 'user-reason', 60, $this->getRequest()->getVal( 'user-reason', false ),
595 [ 'id' => 'wpReason', 'maxlength' => 255 ] ) .
596 "</td>
597 </tr>
598 <tr>
599 <td></td>
600 <td class='mw-submit'>" .
601 Xml::submitButton( $this->msg( 'saveusergroups', $user->getName() )->text(),
602 [ 'name' => 'saveusergroups' ] +
603 Linker::tooltipAndAccesskeyAttribs( 'userrights-set' )
604 ) .
605 "</td>
606 </tr>" .
607 Xml::closeElement( 'table' ) . "\n"
608 );
609 } else {
610 $this->getOutput()->addHTML( $grouplist );
611 }
612 $this->getOutput()->addHTML(
613 Xml::closeElement( 'fieldset' ) .
614 Xml::closeElement( 'form' ) . "\n"
615 );
616 }
617
618 /**
619 * Format a link to a group description page
620 *
621 * @param string $group
622 * @return string
623 */
624 private static function buildGroupLink( $group ) {
625 return User::makeGroupLinkHTML( $group, User::getGroupName( $group ) );
626 }
627
628 /**
629 * Format a link to a group member description page
630 *
631 * @param string $group
632 * @return string
633 */
634 private static function buildGroupMemberLink( $group ) {
635 return User::makeGroupLinkHTML( $group, User::getGroupMember( $group ) );
636 }
637
638 /**
639 * Returns an array of all groups that may be edited
640 * @return array Array of groups that may be edited.
641 */
642 protected static function getAllGroups() {
643 return User::getAllGroups();
644 }
645
646 /**
647 * Adds a table with checkboxes where you can select what groups to add/remove
648 *
649 * @todo Just pass the username string?
650 * @param array $usergroups Groups the user belongs to
651 * @param User $user
652 * @return Array with 2 elements: the XHTML table element with checkxboes, and
653 * whether any groups are changeable
654 */
655 private function groupCheckboxes( $usergroups, $user ) {
656 $allgroups = $this->getAllGroups();
657 $ret = '';
658
659 // Put all column info into an associative array so that extensions can
660 // more easily manage it.
661 $columns = [ 'unchangeable' => [], 'changeable' => [] ];
662
663 foreach ( $allgroups as $group ) {
664 $set = in_array( $group, $usergroups );
665 // Should the checkbox be disabled?
666 $disabled = !(
667 ( $set && $this->canRemove( $group ) ) ||
668 ( !$set && $this->canAdd( $group ) ) );
669 // Do we need to point out that this action is irreversible?
670 $irreversible = !$disabled && (
671 ( $set && !$this->canAdd( $group ) ) ||
672 ( !$set && !$this->canRemove( $group ) ) );
673
674 $checkbox = [
675 'set' => $set,
676 'disabled' => $disabled,
677 'irreversible' => $irreversible
678 ];
679
680 if ( $disabled ) {
681 $columns['unchangeable'][$group] = $checkbox;
682 } else {
683 $columns['changeable'][$group] = $checkbox;
684 }
685 }
686
687 // Build the HTML table
688 $ret .= Xml::openElement( 'table', [ 'class' => 'mw-userrights-groups' ] ) .
689 "<tr>\n";
690 foreach ( $columns as $name => $column ) {
691 if ( $column === [] ) {
692 continue;
693 }
694 // Messages: userrights-changeable-col, userrights-unchangeable-col
695 $ret .= Xml::element(
696 'th',
697 null,
698 $this->msg( 'userrights-' . $name . '-col', count( $column ) )->text()
699 );
700 }
701
702 $ret .= "</tr>\n<tr>\n";
703 foreach ( $columns as $column ) {
704 if ( $column === [] ) {
705 continue;
706 }
707 $ret .= "\t<td style='vertical-align:top;'>\n";
708 foreach ( $column as $group => $checkbox ) {
709 $attr = $checkbox['disabled'] ? [ 'disabled' => 'disabled' ] : [];
710
711 $member = User::getGroupMember( $group, $user->getName() );
712 if ( $checkbox['irreversible'] ) {
713 $text = $this->msg( 'userrights-irreversible-marker', $member )->text();
714 } else {
715 $text = $member;
716 }
717 $checkboxHtml = Xml::checkLabel( $text, "wpGroup-" . $group,
718 "wpGroup-" . $group, $checkbox['set'], $attr );
719 $ret .= "\t\t" . ( $checkbox['disabled']
720 ? Xml::tags( 'span', [ 'class' => 'mw-userrights-disabled' ], $checkboxHtml )
721 : $checkboxHtml
722 ) . "<br />\n";
723 }
724 $ret .= "\t</td>\n";
725 }
726 $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
727
728 return [ $ret, (bool)$columns['changeable'] ];
729 }
730
731 /**
732 * @param string $group The name of the group to check
733 * @return bool Can we remove the group?
734 */
735 private function canRemove( $group ) {
736 $groups = $this->changeableGroups();
737
738 return in_array(
739 $group,
740 $groups['remove'] ) || ( $this->isself && in_array( $group, $groups['remove-self'] )
741 );
742 }
743
744 /**
745 * @param string $group The name of the group to check
746 * @return bool Can we add the group?
747 */
748 private function canAdd( $group ) {
749 $groups = $this->changeableGroups();
750
751 return in_array(
752 $group,
753 $groups['add'] ) || ( $this->isself && in_array( $group, $groups['add-self'] )
754 );
755 }
756
757 /**
758 * Returns $this->getUser()->changeableGroups()
759 *
760 * @return array Array(
761 * 'add' => array( addablegroups ),
762 * 'remove' => array( removablegroups ),
763 * 'add-self' => array( addablegroups to self ),
764 * 'remove-self' => array( removable groups from self )
765 * )
766 */
767 function changeableGroups() {
768 return $this->getUser()->changeableGroups();
769 }
770
771 /**
772 * Show a rights log fragment for the specified user
773 *
774 * @param User $user User to show log for
775 * @param OutputPage $output OutputPage to use
776 */
777 protected function showLogFragment( $user, $output ) {
778 $rightsLogPage = new LogPage( 'rights' );
779 $output->addHTML( Xml::element( 'h2', null, $rightsLogPage->getName()->text() ) );
780 LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage() );
781 }
782
783 /**
784 * Return an array of subpages beginning with $search that this special page will accept.
785 *
786 * @param string $search Prefix to search for
787 * @param int $limit Maximum number of results to return (usually 10)
788 * @param int $offset Number of results to skip (usually 0)
789 * @return string[] Matching subpages
790 */
791 public function prefixSearchSubpages( $search, $limit, $offset ) {
792 $user = User::newFromName( $search );
793 if ( !$user ) {
794 // No prefix suggestion for invalid user
795 return [];
796 }
797 // Autocomplete subpage as user list - public to allow caching
798 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
799 }
800
801 protected function getGroupName() {
802 return 'users';
803 }
804 }