MediaWiki r29765 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r29764‎ | r29765 (on ViewVC)‎ | r29766 >
Date:23:57, 14 January 2008
Author:river
Status:old
Tags:
Comment:
* $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf: allow users to add or remove
themselves from specified groups via Special:Userrights.
Modified paths:

Diff [purge]

Index: trunk/phase3/includes/SpecialUserrights.php
===================================================================
--- trunk/phase3/includes/SpecialUserrights.php	(revision 29764)
+++ trunk/phase3/includes/SpecialUserrights.php	(revision 29765)
@@ -17,6 +17,7 @@
 	# 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 @@
 
 	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 @@
 	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 @@
 
 		$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 @@
 	 * @return null
 	 */
 	function saveUserGroups( $username, $removegroup, $addgroup, $reason = '') {
+		global $wgUser, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
+
 		$user = $this->fetchUser( $username );
 		if( !$user ) {
 			return;
@@ -104,10 +125,18 @@
 		
 		// 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,10 +292,16 @@
 	 * @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 @@
 		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 @@
 	 * @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 @@
 		}
 
 		// 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: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 29764)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 29765)
@@ -1144,6 +1144,13 @@
 $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.
  * Translated trough restriction-* messages.
Index: trunk/phase3/languages/messages/MessagesEn.php
===================================================================
--- trunk/phase3/languages/messages/MessagesEn.php	(revision 29764)
+++ trunk/phase3/languages/messages/MessagesEn.php	(revision 29765)
@@ -1340,7 +1340,8 @@
 '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.',
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 29764)
+++ trunk/phase3/RELEASE-NOTES	(revision 29765)
@@ -35,6 +35,8 @@
 * 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

Follow-up revisions

Rev.Commit summaryAuthorDate
r29779Another fix for r29765: Avoid PHP notices...raymond07:51, 15 January 2008

Status & tagging log

  • 15:23, 12 September 2011 Meno25 (Talk | contribs) changed the status of r29765 [removed: ok added: old]
Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox