4 * Special page to allow managing user group membership
6 * @addtogroup SpecialPage
7 * @todo This code is disgusting and needs a total rewrite
11 require_once( dirname(__FILE__
) . '/HTMLForm.php');
14 function wfSpecialUserrights() {
16 $form = new UserrightsForm($wgRequest);
21 * A class to manage user levels rights.
22 * @addtogroup SpecialPage
24 class UserrightsForm
extends HTMLForm
{
25 var $mPosted, $mRequest, $mSaveprefs;
26 /** Escaped local url name*/
30 public function __construct( &$request ) {
31 $this->mPosted
= $request->wasPosted();
32 $this->mRequest
=& $request;
33 $this->mName
= 'userrights';
35 $titleObj = SpecialPage
::getTitleFor( 'Userrights' );
36 $this->action
= $titleObj->escapeLocalURL();
40 * Manage forms to be shown according to posted data.
41 * Depending on the submit button used, call a form or a save function.
44 // show the general form
46 if( $this->mPosted
) {
47 // show some more forms
48 if( $this->mRequest
->getCheck( 'ssearchuser' ) ) {
49 $this->editUserGroupsForm( $this->mRequest
->getVal( 'user-editname' ) );
53 if( $this->mRequest
->getCheck( 'saveusergroups' ) ) {
55 $username = $this->mRequest
->getVal( 'user-editname' );
56 $reason = $this->mRequest
->getVal( 'user-reason' );
57 if( $wgUser->matchEditToken( $this->mRequest
->getVal( 'wpEditToken' ), $username ) ) {
58 $this->saveUserGroups( $username,
59 $this->mRequest
->getArray( 'member' ),
60 $this->mRequest
->getArray( 'available' ),
67 /** Back-end for saveUserGroups()
69 * @param array $removegroup
70 * @param array $addgroup
71 * @param string $reason
74 function doSaveUserGroups($u, $removegroup, $addgroup, $reason)
76 $oldGroups = $u->getGroups();
77 $newGroups = $oldGroups;
78 // remove then add groups
79 if(isset($removegroup)) {
80 $newGroups = array_diff($newGroups, $removegroup);
81 foreach( $removegroup as $group ) {
82 if ( $this->canRemove( $group ) ) {
83 $u->removeGroup( $group );
87 if(isset($addgroup)) {
88 $newGroups = array_merge($newGroups, $addgroup);
89 foreach( $addgroup as $group ) {
90 if ( $this->canAdd( $group ) ) {
91 $u->addGroup( $group );
95 $newGroups = array_unique( $newGroups );
97 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
98 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
100 wfRunHooks( 'UserRights', array( &$u, $addgroup, $removegroup ) );
101 $log = new LogPage( 'rights' );
102 $log->addEntry( 'rights', Title
::makeTitle( NS_USER
, $u->getName() ), $reason, array( $this->makeGroupNameList( $oldGroups ),
103 $this->makeGroupNameList( $newGroups ) ) );
107 * Save user groups changes in the database.
108 * Data comes from the editUserGroupsForm() form function
110 * @param string $username Username to apply changes to.
111 * @param array $removegroup id of groups to be removed.
112 * @param array $addgroup id of groups to be added.
113 * @param string $reason Reason for group change
116 function saveUserGroups( $username, $removegroup, $addgroup, $reason = '' ) {
118 $u = User
::newFromName($username);
120 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
122 if($u->getID() == 0) {
123 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
126 $this->doSaveUserGroups($u, $removegroup, $addgroup, $reason);
129 function makeGroupNameList( $ids ) {
130 return implode( ', ', $ids );
134 * Output a form to allow searching for a user
136 function switchForm() {
137 global $wgOut, $wgRequest;
138 $username = $wgRequest->getText( 'user-editname' );
139 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->action
, 'name' => 'uluser' ) );
140 $form .= '<fieldset><legend>' . wfMsgHtml( 'userrights-lookup-user' ) . '</legend>';
141 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'userrights-user-editname' ), 'user-editname', 'username', 30, $username ) . '</p>';
142 $form .= '<p>' . Xml
::submitButton( wfMsg( 'editusergroup' ), array( 'name' => 'ssearchuser' ) ) . '</p>';
143 $form .= '</fieldset>';
145 $wgOut->addHTML( $form );
149 * Edit user groups membership
150 * @param string $username Name of the user.
152 function editUserGroupsForm($username) {
155 $user = User
::newFromName($username);
156 if( is_null( $user ) ) {
157 $wgOut->addWikiText( wfMsg( 'nouserspecified' ) );
159 } elseif( $user->getID() == 0 ) {
160 $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
164 $this->showEditUserGroupsForm( $username, $user->getGroups() );
165 $this->showLogFragment( $user, $wgOut );
169 * Go through used and available groups and return the ones that this
170 * form will be able to manipulate based on the current user's system
173 * @param $groups Array: list of groups the given user is in
174 * @return Array: Tuple of addable, then removable groups
176 protected function splitGroups( $groups ) {
177 list($addable, $removable) = array_values( $this->changeableGroups() );
178 $removable = array_intersect($removable, $groups ); // Can't remove groups the user doesn't have
179 $addable = array_diff( $addable, $groups ); // Can't add groups the user does have
181 return array( $addable, $removable );
185 * Show the form to edit group memberships.
187 * @todo make all CSS-y and semantic
188 * @param $username String: Name of user you're editing
189 * @param $groups Array: Array of groups the user is in
191 protected function showEditUserGroupsForm( $username, $groups ) {
192 global $wgOut, $wgUser;
194 list( $addable, $removable ) = $this->splitGroups( $groups );
197 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->action
, 'name' => 'editGroup' ) ) .
198 Xml
::hidden( 'user-editname', $username ) .
199 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $username ) ) .
200 Xml
::openElement( 'fieldset' ) .
201 Xml
::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
202 $wgOut->parse( wfMsg( 'editinguser', $username ) ) .
203 $this->explainRights() .
210 <td width='50%'>" . $this->removeSelect( $removable ) . "</td>
211 <td width='50%'>" . $this->addSelect( $addable ) . "</td>
217 $wgOut->parse( wfMsg('userrights-groupshelp') ) .
222 Xml
::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
225 Xml
::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
231 Xml
::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
235 Xml
::closeElement( 'fieldset' ) .
236 Xml
::closeElement( 'form' ) . "\n"
241 * Prepare a list of groups the user is able to add and remove
245 private function explainRights() {
246 global $wgUser, $wgLang;
249 list( $add, $remove ) = array_values( $this->changeableGroups() );
251 if( count( $add ) > 0 )
252 $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', $wgLang->listToText( $add ) );
253 if( count( $remove ) > 0 )
254 $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', $wgLang->listToText( $remove ) );
256 return count( $out ) > 0
257 ?
implode( ' ', $out )
258 : wfMsgExt( 'userrights-available-none', 'parseinline' );
262 * Adds the <select> thingie where you can select what groups to remove
264 * @param array $groups The groups that can be removed
265 * @return string XHTML <select> element
267 private function removeSelect( $groups ) {
268 return $this->doSelect( $groups, 'member' );
272 * Adds the <select> thingie where you can select what groups to add
274 * @param array $groups The groups that can be added
275 * @return string XHTML <select> element
277 private function addSelect( $groups ) {
278 return $this->doSelect( $groups, 'available' );
282 * Adds the <select> thingie where you can select what groups to add/remove
284 * @param array $groups The groups that can be added/removed
285 * @param string $name 'member' or 'available'
286 * @return string XHTML <select> element
288 private function doSelect( $groups, $name ) {
289 $ret = wfMsgHtml( "{$this->mName}-groups$name" ) .
290 Xml
::openElement( 'select', array(
291 'name' => "{$name}[]",
292 'multiple' => 'multiple',
294 'style' => 'width: 100%;'
297 foreach ($groups as $group) {
298 $ret .= Xml
::element( 'option', array( 'value' => $group ), User
::getGroupName( $group ) );
300 $ret .= Xml
::closeElement( 'select' );
305 * @param string $group The name of the group to check
306 * @return bool Can we remove the group?
308 private function canRemove( $group ) {
309 // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
311 $groups = $this->changeableGroups();
312 return in_array( $group, $groups['remove'] );
316 * @param string $group The name of the group to check
317 * @return bool Can we add the group?
319 private function canAdd( $group ) {
320 $groups = $this->changeableGroups();
321 return in_array( $group, $groups['add'] );
325 * Returns an array of the groups that the user can add/remove.
327 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
329 function changeableGroups() {
332 $groups = array( 'add' => array(), 'remove' => array() );
333 $addergroups = $wgUser->getEffectiveGroups();
335 foreach ($addergroups as $addergroup) {
336 $groups = array_merge_recursive(
337 $groups, $this->changeableByGroup($addergroup)
339 $groups['add'] = array_unique( $groups['add'] );
340 $groups['remove'] = array_unique( $groups['remove'] );
346 * Returns an array of the groups that a particular group can add/remove.
348 * @param String $group The group to check for whether it can add/remove
349 * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
351 private function changeableByGroup( $group ) {
352 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups;
354 if( empty($wgGroupPermissions[$group]['userrights']) ) {
355 // This group doesn't give the right to modify anything
356 return array( 'add' => array(), 'remove' => array() );
358 if( empty($wgAddGroups[$group]) and empty($wgRemoveGroups[$group]) ) {
359 // This group gives the right to modify everything (reverse-
360 // compatibility with old "userrights lets you change
363 'add' => User
::getAllGroups(),
364 'remove' => User
::getAllGroups()
368 // Okay, it's not so simple, we have to go through the arrays
369 $groups = array( 'add' => array(), 'remove' => array() );
370 if( empty($wgAddGroups[$group]) ) {
371 // Don't add anything to $groups
372 } elseif( $wgAddGroups[$group] === true ) {
373 // You get everything
374 $groups['add'] = User
::getAllGroups();
375 } elseif( is_array($wgAddGroups[$group]) ) {
376 $groups['add'] = $wgAddGroups[$group];
379 // Same thing for remove
380 if( empty($wgRemoveGroups[$group]) ) {
381 } elseif($wgRemoveGroups[$group] === true ) {
382 $groups['remove'] = User
::getAllGroups();
383 } elseif( is_array($wgRemoveGroups[$group]) ) {
384 $groups['remove'] = $wgRemoveGroups[$group];
390 * Show a rights log fragment for the specified user
392 * @param User $user User to show log for
393 * @param OutputPage $output OutputPage to use
395 protected function showLogFragment( $user, $output ) {
396 $viewer = new LogViewer(
401 'page' => $user->getUserPage()->getPrefixedText(),
406 $output->addHtml( "<h2>" . htmlspecialchars( LogPage
::logName( 'rights' ) ) . "</h2>\n" );
407 $viewer->showList( $output );