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