Translations of this page?:

Managing User Groups

On the site I run and am implementing Dokuwiki, we need three classes of users:

  • Anonymous users that can read all the public areas.
  • Registered users that have a login and may (perhaps?) public level pages.
  • Members who have access to “Members only” materials.

There are actually a few other groups, but I (as webmaster) manage them more manually since they change about once in three years for a half dozen people.

We have one user who determines who is allowed into the “members only” group and that person has been using a web form to add and remove permissions for many users frequently as memberships are added and lapse.

I have built a webbased form that returned a list of users “in” the members group with a button to remove them. Another list of members not in the members group has a button to add the user. At the top of the form there is a script that:

  • determines if the remove button was pressed and removes the selected member if it was.
  • determines if the add button was pressed and adds the selected user if it was.
  • the form is then regenerated from the altered password file.

While I was fighting with some ugly code to do a ton of check and recheck and recheck again as the password file was re-written, I looked at the code in inc/auth.php and saw how it performed a similar operation to change passwords. I immediated rewrote my functions into a single, simpler function that still accomplishes all my goals. Hopefully, someone else will have a use for this or, perhaps it can be imported into the Wiki engine itself.

Kite's Function

Nov 18/2005

function SetUserGroup($userid, $group_name, $grp_state) {
   //echo "\t\t<p>DEBUG SetUserGroup($userid, $group_name, $grp_state)</p>\n";
  //Load the entire file
  $pro = file('conf/users.auth.php');

  // Get the current data for the user ID
  $lines = preg_grep("/^$userid:/", $pro);
  //echo "\t\t<p>DEBUG count(\$lines)=".count($lines)."</p>\n";
     
  $match_pattern = "/^($userid):([^:]*):([^:]*):([^:]*):([^\n\r$]*)[\n\r$]+/";
  
  $key = key($lines);
  //echo "\t\t<p>DEBUG \$lines[\$key]=$lines[key]</p>\n";
  $matched = preg_match($match_pattern, $lines[$key], $matches);

  if($matched) {
	  // Remove the old user data from the file contents
	  $prodel = preg_grep("/^$userid:/", $pro, PREG_GREP_INVERT);
	  // Make the output into a single string
	  $prodeljoin = join("",$prodel);
	  
	  //generate the list of groups the user belongs to
	  foreach(explode(",", $matches[5]) as $grp) {
		// Use the group name as a key to ensure a group never exists twice
		$grp_list[$grp] = 1;
	  }
	  $grp_list[$group_name] = $grp_state;
	  
	  $grp_string = "";
	  foreach($grp_list as $grp=>$active) {
		 if ($active) {
			 $grp_string .= $grp . ",";
		 }
	  }
	  // remove trailing commas
	  $grp_string = rtrim($grp_string,",");
	  
	  // Build e new user line
	  $pronew = $matches[1] .':'.   //user
				$matches[2] .':'.   //pwd
				$matches[3] .':'.   //name
				$matches[4] .':'.   //email
				$grp_string;        //groupe
	 
	  //echo "\t\t<p>DEBUG \$pronew=\"$pronew\"</p>\n";
	  $prosav = $prodeljoin . $pronew ."\n";
	 
	  return (io_saveFile("conf/users.auth.php", $prosav));
   } else {
      echo "\t\t<p class=\"error\">User was not found</p>\n";
      return false;
   }
}

kite [at] puzzlers [dot] org

Discussion

Hi Kite, The auth system has been revised. The updated auth interface provides methods for managing groups and group memberships. The usermanager handles most of the common user management functions. However there is (currently) no specific group membership only handling. If you would like to take a look at both of the above and suggest extensions to the user manager for group membership handling that would be great :-)Christopher Smith 2005-11-19 13:56

 
wiki/tips/manage_user_groups.txt · Last modified: 2007/10/13 22:38 by ach
 
Imprint Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsTranslate