* $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf: allow users to add or remove
authorRiver Tarnell <river@users.mediawiki.org>
Mon, 14 Jan 2008 23:57:43 +0000 (23:57 +0000)
committerRiver Tarnell <river@users.mediawiki.org>
Mon, 14 Jan 2008 23:57:43 +0000 (23:57 +0000)
  themselves from specified groups via Special:Userrights.

RELEASE-NOTES
includes/DefaultSettings.php
includes/SpecialUserrights.php
languages/messages/MessagesEn.php

index 8502cd7..5a38b43 100644 (file)
@@ -35,6 +35,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * New permission userrights-interwiki for changing user rights on foreign wikis.
 * $wgImplictGroups for groups that are hidden from Special:Listusers, etc.
 * $wgAutopromote: automatically promote users who match specified criteria
+* $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf: allow users to add or remove
+  themselves from specified groups via Special:Userrights.
 
 === New features in 1.12 ===
 * (bug 10735) Add a warning for non-descriptive filenames at Special:Upload
index 2916a82..636719f 100644 (file)
@@ -1143,6 +1143,13 @@ $wgGroupPermissions['bureaucrat']['userrights'] = true;
  */
 $wgImplicitGroups = array( '*', 'user', 'autoconfirmed', 'emailconfirmed' );
 
+/**
+ * These are the groups that users are allowed to add to or remove from
+ * their own account via Special:Userrights.
+ */
+$wgGroupsAddToSelf = array();
+$wgGroupsRemoveFromSelf = array();
+
 /**
  * Set of available actions that can be restricted via action=protect
  * You probably shouldn't change this.
index 5089978..52ece54 100644 (file)
@@ -17,6 +17,7 @@ class UserrightsPage extends SpecialPage {
        # 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' );
@@ -28,7 +29,11 @@ class UserrightsPage extends SpecialPage {
 
        public function userCanExecute( $user ) {
                $available = $this->changeableGroups();
-               return !empty( $available['add'] ) or !empty( $available['remove'] );
+               return !empty( $available['add'] ) 
+                       or !empty( $available['remove'] )
+                       or ($this->isself and 
+                               (!empty( $available['add-self'] )
+                                or !empty( $available['remove-self'] )));
        }
 
        /**
@@ -40,7 +45,28 @@ class UserrightsPage extends SpecialPage {
        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;
+               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;
@@ -53,13 +79,6 @@ class UserrightsPage extends SpecialPage {
 
                $this->outputHeader();
 
-               global $wgRequest;
-               if( $par ) {
-                       $this->mTarget = $par;
-               } else {
-                       $this->mTarget = $wgRequest->getVal( 'user' );
-               }
-
                $this->setHeaders();
 
                // show the general form
@@ -97,6 +116,8 @@ class UserrightsPage extends SpecialPage {
         * @return null
         */
        function saveUserGroups( $username, $removegroup, $addgroup, $reason = '') {
+               global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
+
                $user = $this->fetchUser( $username );
                if( !$user ) {
                        return;
@@ -104,10 +125,18 @@ class UserrightsPage extends SpecialPage {
                
                // 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'];
+               }
+
                $removegroup = array_unique(
-                       array_intersect( (array)$removegroup, $changeable['remove'] ) );
+                       array_intersect( (array)$removegroup, $removable ) );
                $addgroup = array_unique(
-                       array_intersect( (array)$addgroup, $changeable['add'] ) );
+                       array_intersect( (array)$addgroup, $addable ) );
 
                $oldGroups = $user->getGroups();
                $newGroups = $oldGroups;
@@ -263,9 +292,15 @@ class UserrightsPage extends SpecialPage {
         * @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 );
        }
@@ -358,12 +393,20 @@ class UserrightsPage extends SpecialPage {
                global $wgUser, $wgLang;
 
                $out = array();
-               list( $add, $remove ) = array_values( $this->changeableGroups() );
+               list( $add, $remove, $addself, $rmself ) = array_values( $this->changeableGroups() );
 
                if( count( $add ) > 0 )
-                       $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', $wgLang->listToText( $add ), count( $add ) );
+                       $out[] = wfMsgExt( 'userrights-available-add', 'parseinline', 
+                                       $wgLang->listToText( $add ), count( $add ) );
                if( count( $remove ) > 0 )
-                       $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', $wgLang->listToText( $remove ), count( $add ) );
+                       $out[] = wfMsgExt( 'userrights-available-remove', 'parseinline', 
+                                       $wgLang->listToText( $remove ), count( $add ) );
+               if( count( $addself ) > 0 )
+                       $out[] = wfMsgExt( 'userrights-available-add-self', 'parseinline',
+                                       $wgLang->listToText( $addself ), count( $addself ) );
+               if( count( $rmself ) > 0 )
+                       $out[] = wfMsgExt( 'userrights-available-remove-self', 'parseinline',
+                                       $wgLang->listToText( $rmself ), count( $rmself ) );
 
                return count( $out ) > 0
                        ? implode( '<br />', $out )
@@ -439,7 +482,7 @@ class UserrightsPage extends SpecialPage {
         * @return Array array( 'add' => array( addablegroups ), 'remove' => array( removablegroups ) )
         */
        function changeableGroups() {
-               global $wgUser;
+               global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
 
                if( $wgUser->isAllowed( 'userrights' ) ) {
                        // This group gives the right to modify everything (reverse-
@@ -454,7 +497,11 @@ class UserrightsPage extends SpecialPage {
                }
 
                // Okay, it's not so simple, we will have to go through the arrays
-               $groups = array( 'add' => array(), 'remove' => array() );
+               $groups = array( 
+                               'add' => array(), 
+                               'remove' => array(),
+                               'add-self' => $wgGroupsAddToSelf,
+                               'remove-self' => $wgGroupsRemoveFromSelf);
                $addergroups = $wgUser->getEffectiveGroups();
 
                foreach ($addergroups as $addergroup) {
index 7b9f3f0..679cb37 100644 (file)
@@ -1340,7 +1340,8 @@ Unselected groups will not be changed. You can deselect a group with CTRL + Left
 'userrights-reason'           => 'Reason for change:',
 'userrights-available-none'   => 'You may not alter group membership.',
 'userrights-available-add'    => 'You can add users to {{PLURAL:$2|this group|these groups}}: $1.',
-'userrights-available-remove' => 'You can remove users from {{PLURAL:$2|this group|these groups}}: $1.',
+'userrights-available-add-self'    => 'You can add yourself to {{PLURAL:$2|this group|these groups}}: $1.',
+'userrights-available-remove-self' => 'You can remove yourself from {{PLURAL:$2|this group|these groups}}: $1.',
 'userrights-no-interwiki'     => 'You do not have permission to edit user rights on other wikis.',
 'userrights-nodatabase'       => 'Database $1 does not exist or is not local.',
 'userrights-nologin'          => 'You must [[Special:Userlogin|log in]] with an administrator account to assign user rights.',