Mithrandir Mithrandir
  • XOOPS is my life!
  • XOOPS is my life!
  • Joined: 2003/6/21 12:37
  • From Copenhagen, Denmark
  • Group: Registered Users
  • Posts: 6320
  • Posted on: 2004/10/12 9:23
Manage User's Group Membership from Edit User #1
How to add a multi-select of groups on the administration-side "edit user" page:

modules/system/admin/users/users.php line 148:
$groups array_values($user->getGroups()); 
//This is a new line that must come before the existing        include XOOPS_ROOT_PATH."/modules/system/admin/users/userform.php";


modules/system/admin/users/userform.php line 114:
$group_select = new XoopsFormSelectGroup(_US_GROUPS'groups'false$groups5true);


same file a little lower:
$form->addElement($mailok_radio); //existing line
$form->addElement($group_select); //new line
$form->addElement($fct_hidden); //existing line


Now it will show the select element, but we need it to actually do something as well, so add another parameter to the updateUser() function in modules/system/admin/users/users.php so it looks like this:
function updateUser($uid$uname$name$url$email$user_icq$user_aim$user_yim$user_msnm$user_from$user_occ$user_intrest$user_viewemail$user_avatar$user_sig$attachsig$theme$pass$pass2$rank$bio$uorder$umode$notify_method$notify_mode$timezone_offset$user_mailok$groups = array())

(Only added $groups = array() as the last parameter)

Right, but it will have to use this extra parameter, too, so change this in modules/system/admin/users/users.php - updateUser() function:
if (!$member_handler->insertUser($edituser)) {
     
xoops_cp_header();
     echo 
$edituser->getHtmlErrors();
     
xoops_cp_footer();
} else {
     
redirect_header("admin.php?fct=users",1,_AM_DBUPDATED);
}

to
if (!$member_handler->insertUser($edituser)) {
    
xoops_cp_header();
    echo 
$edituser->getHtmlErrors();
    
xoops_cp_footer();
} else {
    if (
$groups != array()) {
        
//Get user's old groups
        
$oldgroups $edituser->getGroups();
        
$member_handler =& xoops_gethandler('member');
        foreach (
$oldgroups as $groupid) {
            
//remove user's membership of these groups
            
$member_handler->removeUsersFromGroup($groupid, array($edituser->getVar('uid')));
        }
        foreach (
$groups as $groupid) {
            
//Add the user to the new groups
            
$member_handler->addUserToGroup($groupid$edituser->getVar('uid'));
        }
    }
    
redirect_header("admin.php?fct=users",1,_AM_DBUPDATED);
}


And finally, we need to call the updateUser() function with the proper parameters

modules/system/admin/users/main.php line 55:
updateUser($uid$username$name$url$email$user_icq$user_aim$user_yim$user_msnm$user_from$user_occ$user_intrest$user_viewemail$user_avatar$user_sig$attachsig$theme$password$pass2$rank$bio$uorder$umode$notify_method$notify_mode$timezone_offset$user_mailok$groups);

(just added $groups as a last parameter)

Now there's only one thing left - and that is to add the language constant, we use in the userform to the proper language file:
xoops-root/language/[language]/user.php:
define('_US_GROUPS''User's Groups'); //or whatever translation, you want
Mithrandir Mithrandir
  • XOOPS is my life!
  • XOOPS is my life!
  • Joined: 2003/6/21 12:37
  • From Copenhagen, Denmark
  • Group: Registered Users
  • Posts: 6320
  • Posted on: 2004/10/14 6:02
Re: Manage User's Group Membership from Edit User #2
Alright, Take 2 on this one:

modules/system/admin/users/userform.php:
// Groups administration addition XOOPS 2.0.9: Mith
global $xoopsUser;
$gperm_handler =& xoops_gethandler('groupperm');
//If user has admin rights on groups
if ($gperm_handler->checkRight("system_admin"XOOPS_SYSTEM_GROUP$xoopsUser->getGroups(), 1)) {
    
//add group selection
    
$group_select = new XoopsFormSelectGroup(_US_GROUPS'groups'false$groups5true);
}
else {
    
//add empty variable
    
$group_select = new XoopsFormHidden('groups', array());
}


instead of the previous addition there - we want only users, who have system admin rights over groups to be able to use this.

Also, we do not want accidents, where users are accidently removing themselves from the webmaster's group, so:

modules/system/admin/users/users.php - updateUsers() function:
if ($groups != array()) {
   global 
$xoopsUser;
   
$oldgroups $edituser->getGroups();
   
//If the edited user is the current user and the current
   //user WAS in the webmaster's group and is NOT in the new groups array
   
if ($edituser->getVar('uid') == $xoopsUser->getVar('uid') && (in_array(XOOPS_GROUP_ADMIN$oldgroups)) && !(in_array(XOOPS_GROUP_ADMIN$groups))) {
       
//Add the webmaster's group to the groups array to prevent accidentally 
       //removing oneself from the webmaster's group
       
array_push($groupsXOOPS_GROUP_ADMIN);
    }                
    
$member_handler =& xoops_gethandler('member');
    foreach (
$oldgroups as $groupid) {
       
$member_handler->removeUsersFromGroup($groupid, array($edituser->getVar('uid')));
    }
    foreach (
$groups as $groupid) {
       
$member_handler->addUserToGroup($groupid$edituser->getVar('uid'));
    }
}
redirect_header("admin.php?fct=users",1,_AM_DBUPDATED);
pepeli pepeli
  • Just popping in
  • Just popping in
  • Joined: 2004/1/16 18:06
  • Group: Registered Users
  • Posts: 7
  • Posted on: 2004/12/11 10:16
Re: Manage User's Group Membership from Edit User #3
It`s works

Thank you

GREAT WORK
Franki Franki
  • Just popping in
  • Just popping in
  • Joined: 2004/10/18 4:42
  • Group: Registered Users
  • Posts: 8
  • Posted on: 2006/4/6 8:31
Re: Manage User's Group Membership from Edit User #4
I know I'm digging this up from the grave, but I think I found a bug but i cannot figure out to fix it (and this seems to be the only related thread I've found after many hours of searching). I hope a guru here can help:

[version 2.0.13.2]

When the Admin logs in and uses "Add User" (under Edit/Delete Users), even if a different group(s) is selected by the admin through this process, this new user is NEVER ADDED to the group(s) selected - the only group the new user is added to is "Registered" (groupid = 2). Even if the Registered group is not selected, new user is added to this group anyway.

I've tried looking through the codes, but am only a newbie. Please help!
Tailzehhhh Tailzehhhh
  • Just popping in
  • Just popping in
  • Joined: 2006/4/6 1:33
  • Group: Registered Users
  • Posts: 20
  • Posted on: 2006/4/6 14:49
Re: Manage User's Group Membership from Edit User #5
going to use it. and then report back my feedback.
Franki Franki
  • Just popping in
  • Just popping in
  • Joined: 2004/10/18 4:42
  • Group: Registered Users
  • Posts: 8
  • Posted on: 2006/4/7 23:59
Re: Manage User's Group Membership from Edit User #6
thanks Tailzehhhh.

Anyone else having this problem?

I can't even find the function that create/add the new user (to the database) through the admin area...
drummond drummond
  • Just popping in
  • Just popping in
  • Joined: 2004/6/18 22:33
  • Group: Registered Users
  • Posts: 36
  • Posted on: 2006/6/7 11:54
Re: Manage User's Group Membership from Edit User #7
Yep, I've always had this problem with XOOPS with every version I have ever used. Still doesn't work in 2.0.13.2. And I've tried it on a couple different servers and browsers.

Anyone know how to fix this? Sounds like a system bug to me.
How to effectively post a question in the Xoops forums? - Read here...
Design by: XOOPS UI/UX Team