Fixed Doxygen warnings
[lhc/web/wiklou.git] / includes / SpecialUserrights.php
index 6af65c3..bf4d440 100644 (file)
 <?php
-
 /**
  * Special page to allow managing user group membership
  *
- * @addtogroup SpecialPage
- * @todo This code is disgusting and needs a total rewrite
+ * @file
+ * @ingroup SpecialPage
  */
 
-/** */
-require_once( dirname(__FILE__) . '/HTMLForm.php');
-
-/** Entry point */
-function wfSpecialUserrights() {
-       global $wgRequest;
-       $form = new UserrightsForm($wgRequest);
-       $form->execute();
-}
-
 /**
  * A class to manage user levels rights.
- * @addtogroup SpecialPage
+ * @ingroup SpecialPage
  */
-class UserrightsForm extends HTMLForm {
-       var $mPosted, $mRequest, $mSaveprefs;
-       /** Escaped local url name*/
-       var $action;
-
-       /** Constructor*/
-       public function __construct( &$request ) {
-               $this->mPosted = $request->wasPosted();
-               $this->mRequest =& $request;
-               $this->mName = 'userrights';
-
-               $titleObj = SpecialPage::getTitleFor( 'Userrights' );
-               $this->action = $titleObj->escapeLocalURL();
+class UserrightsPage extends SpecialPage {
+       # The target of the local right-adjuster's interest.  Can be gotten from
+       # either a GET parameter or a subpage-style parameter, so have a member
+       # variable for it.
+       protected $mTarget;
+       protected $isself = false;
+
+       public function __construct() {
+               parent::__construct( 'Userrights' );
+       }
+
+       public function isRestricted() {
+               return true;
+       }
+
+       public function userCanExecute( $user ) {
+               $available = $this->changeableGroups();
+               return !empty( $available['add'] )
+                       or !empty( $available['remove'] )
+                       or ($this->isself and
+                               (!empty( $available['add-self'] )
+                                or !empty( $available['remove-self'] )));
        }
 
        /**
         * Manage forms to be shown according to posted data.
         * Depending on the submit button used, call a form or a save function.
+        *
+        * @param $par Mixed: string if any subpage provided, else null
         */
-       function execute() {
+       function execute( $par ) {
+               // If the visitor doesn't have permissions to assign or remove
+               // any groups, it's a bit silly to give them the user search prompt.
+               global $wgUser, $wgRequest;
+
+               if( $par ) {
+                       $this->mTarget = $par;
+               } else {
+                       $this->mTarget = $wgRequest->getVal( 'user' );
+               }
+
+               if (!$this->mTarget) {
+                       /*
+                        * If the user specified no target, and they can only
+                        * edit their own groups, automatically set them as the
+                        * target.
+                        */
+                       $available = $this->changeableGroups();
+                       if (empty($available['add']) && empty($available['remove']))
+                               $this->mTarget = $wgUser->getName();
+               }
+
+               if ($this->mTarget == $wgUser->getName())
+                       $this->isself = true;
+
+               if( !$this->userCanExecute( $wgUser ) ) {
+                       // fixme... there may be intermediate groups we can mention.
+                       global $wgOut;
+                       $wgOut->showPermissionsErrorPage( array(
+                               $wgUser->isAnon()
+                                       ? 'userrights-nologin'
+                                       : 'userrights-notallowed' ) );
+                       return;
+               }
+
+               if ( wfReadOnly() ) {
+                       global $wgOut;
+                       $wgOut->readOnlyPage();
+                       return;
+               }
+
+               $this->outputHeader();
+
+               $this->setHeaders();
+
                // show the general form
                $this->switchForm();
-               if( $this->mPosted ) {
-                       // show some more forms
-                       if( $this->mRequest->getCheck( 'ssearchuser' ) ) {
-                               $this->editUserGroupsForm( $this->mRequest->getVal( 'user-editname' ) );
-                       }
 
+               if( $wgRequest->wasPosted() ) {
                        // save settings
-                       if( $this->mRequest->getCheck( 'saveusergroups' ) ) {
-                               global $wgUser;
-                               $username = $this->mRequest->getVal( 'user-editname' );
-                               $reason = $this->mRequest->getVal( 'user-reason' );
-                               if( $wgUser->matchEditToken( $this->mRequest->getVal( 'wpEditToken' ), $username ) ) {
-                                       $this->saveUserGroups( $username,
-                                               $this->mRequest->getArray( 'member' ),
-                                               $this->mRequest->getArray( 'available' ),
-                                               $reason );
+                       if( $wgRequest->getCheck( 'saveusergroups' ) ) {
+                               $reason = $wgRequest->getVal( 'user-reason' );
+                               if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $this->mTarget ) ) {
+                                       $this->saveUserGroups(
+                                               $this->mTarget,
+                                               $reason
+                                       );
                                }
                        }
                }
+
+               // show some more forms
+               if( $this->mTarget ) {
+                       $this->editUserGroupsForm( $this->mTarget );
+               }
        }
 
        /**
         * Save user groups changes in the database.
         * Data comes from the editUserGroupsForm() form function
         *
-        * @param string $username Username to apply changes to.
-        * @param array $removegroup id of groups to be removed.
-        * @param array $addgroup id of groups to be added.
-        * @param string $reason Reason for group change
-        *
+        * @param $username String: username to apply changes to.
+        * @param $reason String: reason for group change
+        * @return null
         */
-       function saveUserGroups( $username, $removegroup, $addgroup, $reason = '' ) {
-               global $wgOut;
-               $u = User::newFromName($username);
+       function saveUserGroups( $username, $reason = '') {
+               global $wgRequest, $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
 
-               if(is_null($u)) {
-                       $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
+               $user = $this->fetchUser( $username );
+               if( !$user ) {
                        return;
                }
 
-               if($u->getID() == 0) {
-                       $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
-                       return;
+               $allgroups = $this->getAllGroups();
+               $addgroup = array();
+               $removegroup = array();
+
+               // This could possibly create a highly unlikely race condition if permissions are changed between
+               //  when the form is loaded and when the form is saved. Ignoring it for the moment.
+               foreach ($allgroups as $group) {
+                       // We'll tell it to remove all unchecked groups, and add all checked groups.
+                       // Later on, this gets filtered for what can actually be removed
+                       if ($wgRequest->getCheck( "wpGroup-$group" )) {
+                               $addgroup[] = $group;
+                       } else {
+                               $removegroup[] = $group;
+                       }
+               }
+
+               // Validate input set...
+               $changeable = $this->changeableGroups();
+               if ($wgUser->getId() != 0 && $wgUser->getId() == $user->getId()) {
+                       $addable = array_merge($changeable['add'], $wgGroupsAddToSelf);
+                       $removable = array_merge($changeable['remove'], $wgGroupsRemoveFromSelf);
+               } else {
+                       $addable = $changeable['add'];
+                       $removable = $changeable['remove'];
                }
 
-               $oldGroups = $u->getGroups();
+               $removegroup = array_unique(
+                       array_intersect( (array)$removegroup, $removable ) );
+               $addgroup = array_unique(
+                       array_intersect( (array)$addgroup, $addable ) );
+
+               $oldGroups = $user->getGroups();
                $newGroups = $oldGroups;
                // remove then add groups
-               if(isset($removegroup)) {
+               if( $removegroup ) {
                        $newGroups = array_diff($newGroups, $removegroup);
                        foreach( $removegroup as $group ) {
-                               if ( $this->canRemove( $group ) ) {
-                                       $u->removeGroup( $group );
-                               }
+                               $user->removeGroup( $group );
                        }
                }
-               if(isset($addgroup)) {
+               if( $addgroup ) {
                        $newGroups = array_merge($newGroups, $addgroup);
                        foreach( $addgroup as $group ) {
-                               if ( $this->canAdd( $group ) ) {
-                                       $u->addGroup( $group );
-                               }
+                               $user->addGroup( $group );
                        }
                }
                $newGroups = array_unique( $newGroups );
 
+               // Ensure that caches are cleared
+               $user->invalidateCache();
+
                wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
                wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
+               if( $user instanceof User ) {
+                       // hmmm
+                       wfRunHooks( 'UserRights', array( &$user, $addgroup, $removegroup ) );
+               }
 
-               wfRunHooks( 'UserRights', array( &$u, $addgroup, $removegroup ) );      
-               $log = new LogPage( 'rights' );
-               $log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), $reason, array( $this->makeGroupNameList( $oldGroups ),
-                       $this->makeGroupNameList( $newGroups ) ) );
-       }
-
-       function makeGroupNameList( $ids ) {
-               return implode( ', ', $ids );
+               if( $newGroups != $oldGroups ) {
+                       $this->addLogEntry( $user, $oldGroups, $newGroups );
+               }
        }
-
+       
        /**
-        * Output a form to allow searching for a user
+        * Add a rights log entry for an action.
         */
-       function switchForm() {
-               global $wgOut, $wgRequest;
-               $username = $wgRequest->getText( 'user-editname' );
-               $form  = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->action, 'name' => 'uluser' ) );
-               $form .= '<fieldset><legend>' . wfMsgHtml( 'userrights-lookup-user' ) . '</legend>';
-               $form .= '<p>' . Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user-editname', 'username', 30, $username ) . '</p>';
-               $form .= '<p>' . Xml::submitButton( wfMsg( 'editusergroup' ), array( 'name' => 'ssearchuser' ) ) . '</p>';
-               $form .= '</fieldset>';
-               $form .= '</form>';
-               $wgOut->addHTML( $form );
+       function addLogEntry( $user, $oldGroups, $newGroups ) {
+               global $wgRequest;
+               $log = new LogPage( 'rights' );
+
+               $log->addEntry( 'rights',
+                       $user->getUserPage(),
+                       $wgRequest->getText( 'user-reason' ),
+                       array(
+                               $this->makeGroupNameList( $oldGroups ),
+                               $this->makeGroupNameList( $newGroups )
+                       )
+               );
        }
 
        /**
         * Edit user groups membership
-        * @param string $username Name of the user.
+        * @param $username String: name of the user.
         */
-       function editUserGroupsForm($username) {
+       function editUserGroupsForm( $username ) {
                global $wgOut;
-       
-               $user = User::newFromName($username);
-               if( is_null( $user ) ) {
-                       $wgOut->addWikiText( wfMsg( 'nouserspecified' ) );
-                       return;
-               } elseif( $user->getID() == 0 ) {
-                       $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
+
+               $user = $this->fetchUser( $username );
+               if( !$user ) {
                        return;
                }
 
-               $this->showEditUserGroupsForm( $username, $user->getGroups() );
+               $groups = $user->getGroups();
+
+               $this->showEditUserGroupsForm( $user, $groups );
+
+               // This isn't really ideal logging behavior, but let's not hide the
+               // interwiki logs if we're using them as is.
                $this->showLogFragment( $user, $wgOut );
        }
-       
+
+       /**
+        * Normalize the input username, which may be local or remote, and
+        * return a user (or proxy) object for manipulating it.
+        *
+        * Side effects: error output for invalid access
+        * @return mixed User, UserRightsProxy, or null
+        */
+       function fetchUser( $username ) {
+               global $wgOut, $wgUser;
+
+               $parts = explode( '@', $username );
+               if( count( $parts ) < 2 ) {
+                       $name = trim( $username );
+                       $database = '';
+               } else {
+                       list( $name, $database ) = array_map( 'trim', $parts );
+
+                       if( !$wgUser->isAllowed( 'userrights-interwiki' ) ) {
+                               $wgOut->addWikiMsg( 'userrights-no-interwiki' );
+                               return null;
+                       }
+                       if( !UserRightsProxy::validDatabase( $database ) ) {
+                               $wgOut->addWikiMsg( 'userrights-nodatabase', $database );
+                               return null;
+                       }
+               }
+
+               if( $name == '' ) {
+                       $wgOut->addWikiMsg( 'nouserspecified' );
+                       return false;
+               }
+
+               if( $name{0} == '#' ) {
+                       // Numeric ID can be specified...
+                       // We'll do a lookup for the name internally.
+                       $id = intval( substr( $name, 1 ) );
+
+                       if( $database == '' ) {
+                               $name = User::whoIs( $id );
+                       } else {
+                               $name = UserRightsProxy::whoIs( $database, $id );
+                       }
+
+                       if( !$name ) {
+                               $wgOut->addWikiMsg( 'noname' );
+                               return null;
+                       }
+               }
+
+               if( $database == '' ) {
+                       $user = User::newFromName( $name );
+               } else {
+                       $user = UserRightsProxy::newFromName( $database, $name );
+               }
+
+               if( !$user || $user->isAnon() ) {
+                       $wgOut->addWikiMsg( 'nosuchusershort', $username );
+                       return null;
+               }
+
+               return $user;
+       }
+
+       function makeGroupNameList( $ids ) {
+               return implode( ', ', $ids );
+       }
+
+       /**
+        * Output a form to allow searching for a user
+        */
+       function switchForm() {
+               global $wgOut, $wgScript;
+               $wgOut->addHTML(
+                       Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'name' => 'uluser', 'id' => 'mw-userrights-form1' ) ) .
+                       Xml::hidden( 'title',  $this->getTitle()->getPrefixedText() ) .
+                       Xml::openElement( 'fieldset' ) .
+                       Xml::element( 'legend', array(), wfMsg( 'userrights-lookup-user' ) ) .
+                       Xml::inputLabel( wfMsg( 'userrights-user-editname' ), 'user', 'username', 30, $this->mTarget ) . ' ' .
+                       Xml::submitButton( wfMsg( 'editusergroup' ) ) .
+                       Xml::closeElement( 'fieldset' ) .
+                       Xml::closeElement( 'form' ) . "\n"
+               );
+       }
+
        /**
         * Go through used and available groups and return the ones that this
         * form will be able to manipulate based on the current user's system
@@ -166,151 +317,181 @@ class UserrightsForm extends HTMLForm {
         * @return Array:  Tuple of addable, then removable groups
         */
        protected function splitGroups( $groups ) {
+               global $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
                list($addable, $removable) = array_values( $this->changeableGroups() );
-               $removable = array_intersect($removable, $groups ); // Can't remove groups the user doesn't have
-               $addable   = array_diff(     $addable,   $groups ); // Can't add groups the user does have
-               
+
+               $removable = array_intersect(
+                               array_merge($this->isself ? $wgGroupsRemoveFromSelf : array(), $removable),
+                               $groups ); // Can't remove groups the user doesn't have
+               $addable   = array_diff(
+                               array_merge($this->isself ? $wgGroupsAddToSelf : array(), $addable),
+                               $groups ); // Can't add groups the user does have
+
                return array( $addable, $removable );
        }
 
        /**
         * Show the form to edit group memberships.
         *
-        * @todo make all CSS-y and semantic
-        * @param $username  String: Name of user you're editing
+        * @param $user      User or UserRightsProxy you're editing
         * @param $groups    Array:  Array of groups the user is in
         */
-       protected function showEditUserGroupsForm( $username, $groups ) {
+       protected function showEditUserGroupsForm( $user, $groups ) {
                global $wgOut, $wgUser;
-               
+
                list( $addable, $removable ) = $this->splitGroups( $groups );
 
+               $list = array();
+               foreach( $user->getGroups() as $group )
+                       $list[] = self::buildGroupLink( $group );
+
+               $grouplist = '';
+               if( count( $list ) > 0 ) {
+                       $grouplist = Xml::tags( 'p', null, wfMsgHtml( 'userrights-groupsmember' ) . ' ' . implode( ', ', $list ) );
+               }
                $wgOut->addHTML(
-                       Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->action, 'name' => 'editGroup' ) ) .
-                       Xml::hidden( 'user-editname', $username ) .
-                       Xml::hidden( 'wpEditToken', $wgUser->editToken( $username ) ) .
+                       Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalURL(), 'name' => 'editGroup', 'id' => 'mw-userrights-form2' ) ) .
+                       Xml::hidden( 'user', $this->mTarget ) .
+                       Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->mTarget ) ) .
                        Xml::openElement( 'fieldset' ) .
                        Xml::element( 'legend', array(), wfMsg( 'userrights-editusergroup' ) ) .
-                       $wgOut->parse( wfMsg( 'editinguser', $username ) ) .
-                       $this->explainRights() .
-                       "<table border='0'>
-                       <tr>
-                               <td></td>
-                               <td>
-                               <table width='400'>
-                                       <tr>
-                                               <td width='50%'>" . $this->removeSelect( $removable ) . "</td>
-                                               <td width='50%'>" . $this->addSelect( $addable ) . "</td>
-                                       </tr>
-                               </table>
-                       </tr>
-                       <tr>
-                               <td colspan='2'>" .
-                                       $wgOut->parse( wfMsg('userrights-groupshelp') ) .
-                               "</td>
-                       </tr>
-                       <tr>
-                               <td>" .
-                                       Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
-                               "</td>
-                               <td>" .
-                                       Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
-                               "</td>
-                       </tr>
-                       <tr>
-                               <td></td>
-                               <td>" .
-                               Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
-                               "</td>
-                       </tr>
-                       </table>\n" .
+                       wfMsgExt( 'editinguser', array( 'parse' ), wfEscapeWikiText( $user->getName() ) ) .
+                       wfMsgExt( 'userrights-groups-help', array( 'parse' ) ) .
+                       $grouplist .
+                       Xml::tags( 'p', null, $this->groupCheckboxes( $groups ) ) .
+                       Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-userrights-table-outer' ) ) .
+                               "<tr>
+                                       <td class='mw-label'>" .
+                                               Xml::label( wfMsg( 'userrights-reason' ), 'wpReason' ) .
+                                       "</td>
+                                       <td class='mw-input'>" .
+                                               Xml::input( 'user-reason', 60, false, array( 'id' => 'wpReason', 'maxlength' => 255 ) ) .
+                                       "</td>
+                               </tr>
+                               <tr>
+                                       <td></td>
+                                       <td class='mw-submit'>" .
+                                               Xml::submitButton( wfMsg( 'saveusergroups' ), array( 'name' => 'saveusergroups' ) ) .
+                                       "</td>
+                               </tr>" .
+                       Xml::closeElement( 'table' ) . "\n" .
                        Xml::closeElement( 'fieldset' ) .
                        Xml::closeElement( 'form' ) . "\n"
                );
        }
 
        /**
-        * Prepare a list of groups the user is able to add and remove
+        * Format a link to a group description page
         *
+        * @param $group string
         * @return string
         */
-       private function explainRights() {
-               global $wgUser, $wgLang;
-               
-               $out = array();
-               list( $add, $remove ) = array_values( $this->changeableGroups() );
-               
-               if( count( $add ) > 0 )
-                       $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', $wgLang->listToText( $add ) );
-               if( count( $remove ) > 0 )
-                       $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', $wgLang->listToText( $remove ) );
-                       
-               return count( $out ) > 0
-                       ? implode( ' ', $out )
-                       : wfMsgExt( 'userrights-available-none', 'parseinline' );
+       private static function buildGroupLink( $group ) {
+               static $cache = array();
+               if( !isset( $cache[$group] ) )
+                       $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupName( $group ) );
+               return $cache[$group];
        }
-
+       
        /**
-        * Adds the <select> thingie where you can select what groups to remove
-        *
-        * @param array $groups The groups that can be removed
-        * @return string XHTML <select> element
+        * Returns an array of all groups that may be edited
+        * @return array Array of groups that may be edited.
         */
-       private function removeSelect( $groups ) {
-               return $this->doSelect( $groups, 'member' );
-       }
+        protected static function getAllGroups() {
+               return User::getAllGroups();
+        }
 
        /**
-        * Adds the <select> thingie where you can select what groups to add
+        * Adds a table with checkboxes where you can select what groups to add/remove
         *
-        * @param array $groups The groups that can be added
-        * @return string XHTML <select> element
+        * @param $usergroups Array: groups the user belongs to
+        * @return string XHTML table element with checkboxes
         */
-       private function addSelect( $groups ) {
-               return $this->doSelect( $groups, 'available' );
-       }
+       private function groupCheckboxes( $usergroups ) {
+               $allgroups = $this->getAllGroups();
+               $ret = '';
 
-       /**
-        * Adds the <select> thingie where you can select what groups to add/remove
-        *
-        * @param array  $groups The groups that can be added/removed
-        * @param string $name   'member' or 'available'
-        * @return string XHTML <select> element
-        */
-       private function doSelect( $groups, $name ) {
-               $ret = wfMsgHtml( "{$this->mName}-groups$name" ) .
-               Xml::openElement( 'select', array(
-                               'name' => "{$name}[]",
-                               'multiple' => 'multiple',
-                               'size' => '6',
-                               'style' => 'width: 100%;'
-                       )
-               );
-               foreach ($groups as $group) {
-                       $ret .= Xml::element( 'option', array( 'value' => $group ), User::getGroupName( $group ) );
+               $column = 1;
+               $settable_col = '';
+               $unsettable_col = '';
+
+               foreach ($allgroups as $group) {
+                       $set = in_array( $group, $usergroups );
+                       # Should the checkbox be disabled?
+                       $disabled = !(
+                               ( $set && $this->canRemove( $group ) ) ||
+                               ( !$set && $this->canAdd( $group ) ) );
+                       # Do we need to point out that this action is irreversible?
+                       $irreversible = !$disabled && (
+                               ($set && !$this->canAdd( $group )) ||
+                               (!$set && !$this->canRemove( $group ) ) );
+
+                       $attr = $disabled ? array( 'disabled' => 'disabled' ) : array();
+                       $text = $irreversible
+                               ? wfMsgHtml( 'userrights-irreversible-marker', User::getGroupMember( $group ) )
+                               : User::getGroupMember( $group );
+                       $checkbox = Xml::checkLabel( $text, "wpGroup-$group",
+                               "wpGroup-$group", $set, $attr );
+                       $checkbox = $disabled ? Xml::tags( 'span', array( 'class' => 'mw-userrights-disabled' ), $checkbox ) : $checkbox;
+
+                       if ($disabled) {
+                               $unsettable_col .= "$checkbox<br />\n";
+                       } else {
+                               $settable_col .= "$checkbox<br />\n";
+                       }
                }
-               $ret .= Xml::closeElement( 'select' );
+
+               if ($column) {
+                       $ret .= Xml::openElement( 'table', array( 'border' => '0', 'class' => 'mw-userrights-groups' ) ) .
+                               "<tr>
+";
+                       if( $settable_col !== '' ) {
+                               $ret .= xml::element( 'th', null, wfMsg( 'userrights-changeable-col' ) );
+                       }
+                       if( $unsettable_col !== '' ) {
+                               $ret .= xml::element( 'th', null, wfMsg( 'userrights-unchangeable-col' ) );
+                       }
+                       $ret.= "</tr>
+                               <tr>
+";
+                       if( $settable_col !== '' ) {
+                               $ret .=
+"                                      <td style='vertical-align:top;'>
+                                               $settable_col
+                                       </td>
+";
+                       }
+                       if( $unsettable_col !== '' ) {
+                               $ret .=
+"                                      <td style='vertical-align:top;'>
+                                               $unsettable_col
+                                       </td>
+";
+                       }
+                       $ret .= Xml::closeElement( 'tr' ) . Xml::closeElement( 'table' );
+               }
+
                return $ret;
        }
 
        /**
-        * @param  string $group The name of the group to check
+        * @param  $group String: the name of the group to check
         * @return bool Can we remove the group?
         */
        private function canRemove( $group ) {
                // $this->changeableGroups()['remove'] doesn't work, of course. Thanks,
                // PHP.
                $groups = $this->changeableGroups();
-               return in_array( $group, $groups['remove'] );
+               return in_array( $group, $groups['remove'] ) || ($this->isself && in_array( $group, $groups['remove-self'] ));
        }
 
        /**
-        * @param  string $group The name of the group to check
+        * @param $group string: the name of the group to check
         * @return bool Can we add the group?
         */
        private function canAdd( $group ) {
                $groups = $this->changeableGroups();
-               return in_array( $group, $groups['add'] );
+               return in_array( $group, $groups['add'] ) || ($this->isself && in_array( $group, $groups['add-self'] ));
        }
 
        /**
@@ -318,10 +499,29 @@ class UserrightsForm extends HTMLForm {
         *
         * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
         */
-       private function changeableGroups() {
-               global $wgUser;
+       function changeableGroups() {
+               global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
 
-               $groups = array( 'add' => array(), 'remove' => array() );
+               if( $wgUser->isAllowed( 'userrights' ) ) {
+                       // This group gives the right to modify everything (reverse-
+                       // compatibility with old "userrights lets you change
+                       // everything")
+                       // Using array_merge to make the groups reindexed
+                       $all = array_merge( User::getAllGroups() );
+                       return array(
+                               'add' => $all,
+                               'remove' => $all,
+                               'add-self' => array(),
+                               'remove-self' => array()
+                       );
+               }
+
+               // Okay, it's not so simple, we will have to go through the arrays
+               $groups = array(
+                               'add' => array(),
+                               'remove' => array(),
+                               'add-self' => $wgGroupsAddToSelf,
+                               'remove-self' => $wgGroupsRemoveFromSelf);
                $addergroups = $wgUser->getEffectiveGroups();
 
                foreach ($addergroups as $addergroup) {
@@ -337,27 +537,12 @@ class UserrightsForm extends HTMLForm {
        /**
         * Returns an array of the groups that a particular group can add/remove.
         *
-        * @param String $group The group to check for whether it can add/remove
+        * @param $group String: the group to check for whether it can add/remove
         * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
-        */     
+        */
        private function changeableByGroup( $group ) {
-               global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups;
-       
-               if( empty($wgGroupPermissions[$group]['userrights']) ) {
-                       // This group doesn't give the right to modify anything
-                       return array( 'add' => array(), 'remove' => array() );
-               }
-               if( empty($wgAddGroups[$group]) and empty($wgRemoveGroups[$group]) ) {
-                       // This group gives the right to modify everything (reverse-
-                       // compatibility with old "userrights lets you change
-                       // everything")
-                       return array(
-                               'add' => User::getAllGroups(),
-                               'remove' => User::getAllGroups()
-                       );
-               }
-               
-               // Okay, it's not so simple, we have to go through the arrays
+               global $wgAddGroups, $wgRemoveGroups;
+
                $groups = array( 'add' => array(), 'remove' => array() );
                if( empty($wgAddGroups[$group]) ) {
                        // Don't add anything to $groups
@@ -367,7 +552,7 @@ class UserrightsForm extends HTMLForm {
                } elseif( is_array($wgAddGroups[$group]) ) {
                        $groups['add'] = $wgAddGroups[$group];
                }
-               
+
                // Same thing for remove
                if( empty($wgRemoveGroups[$group]) ) {
                } elseif($wgRemoveGroups[$group] === true ) {
@@ -377,26 +562,15 @@ class UserrightsForm extends HTMLForm {
                }
                return $groups;
        }
-       
+
        /**
         * Show a rights log fragment for the specified user
         *
-        * @param User $user User to show log for
-        * @param OutputPage $output OutputPage to use
+        * @param $user User to show log for
+        * @param $output OutputPage to use
         */
        protected function showLogFragment( $user, $output ) {
-               $viewer = new LogViewer(
-                       new LogReader(
-                               new FauxRequest(
-                                       array(
-                                               'type' => 'rights',
-                                               'page' => $user->getUserPage()->getPrefixedUrl(),
-                                       )
-                               )
-                       )
-               );
-               $output->addHtml( "<h2>" . htmlspecialchars( LogPage::logName( 'rights' ) ) . "</h2>\n" );
-               $viewer->showList( $output );
+               $output->addHtml( Xml::element( 'h2', null, LogPage::logName( 'rights' ) . "\n" ) );
+               LogEventsList::showLogExtract( $output, 'rights', $user->getUserPage()->getPrefixedText() );
        }
-       
 }