Change Joomla User Group

stevegjacobs

New Member
I need to creat a form where the user only has to read and agree to a statement and then submitting the form with a button labeled 'Agree'. I want this action to trigger a user group change. I think all I need to do is bind the form to the joomla_user_usergroup_map table, use the juser plugin to identify the user and change the group_id in that row to the appropropriate value. I am also using user access levels to change the users permissions. Am I on the right track, has anybody done this, or is there any tutorial on this?

Thanks

Steve
 
The juser plugin is used to register a new Joomla user, so I'm not sure it would work with what you need.
The way I handle this kind of process is use a PHP fom plugin, set onAfterProcess, and using the Joomla method to set a user in a specified group, something like:

PHP:
// Get the connected user
$user = JFactory::getUser();
 
// Add the user in the group ID = x
JUserHelper::addUserToGroup($user->get('id'), x );
 
// reload the user object so the user is connected with its new access rights
$session = JFactory::getSession();
$session->set('user', new JUser($user->get('id')));

Replace "x" by the actual ID of the concerned group.

The third part of the code allows the user to have directly access to his new rights. If you don't include it, he has to log out and log in again to gain the new access.
 
Hi, Thanks for the quick response. I was just trying to do something like that based on code from another thread. I know this is going to sound strange, but what I need to do is to reduce the permissions after the change in group.The person has already filled in an application form, and we want them to declare that they have done so truthfully, and submit it and not be able to edit it again So I don't really want to add the person to a second group, but instead, change them from 'Registered' to 'Guest (or a new group 'ApplicationComplete' with similar privileges to guest). I think what I need to do is edit the jos_user_usergroup_map table rather than add a new record. I have tried this by editing the appropriate record in that table in phpmyadmin. and the permisions change the way I want them too. Can you suggest a bit of code that might do that?
 
There is another function for that:

PHP:
// Get the connected user
$user = JFactory::getUser();
 
// remove the user from group ID = x
JUserHelper::removeUserFromGroup($user->get('id'), x );
 
// reload the user object so the user gets logged out
$session = JFactory::getSession();
$session->set('user', new JUser($user->get('id')));

This is in the case where "guest" is similar to "public".

Otherwise, you can set a user to specific group(s):

PHP:
// Get the connected user
$user = JFactory::getUser();
 
// set the user in group ID = x
JUserHelper::setUserGroups($user->get('id'), array(x) );
 
// reload the user object so the user gets logged out
$session = JFactory::getSession();
$session->set('user', new JUser($user->get('id')));

setUserGroup using an array of groups, you can set a user to multiple groups (x,y,z) using
PHP:
JUserHelper::setUserGroups($user->get('id'), array(x,y,z) );
 
Hi
It is looking promising, but I am now getting these errors when I try to publish the form:
Notice

Sorry, but you are not authorised to add this record
Error

You are not authorised to view this resource.
 
How/where have you implemented the code?
Where do you get the error?
Remember that if you (as a SuperUser), you submit the form (and therefore run the script), you will loose your SuperUser status. You should test it with a "test" user.
 
I have tried to publish the form in the front end using a menu. I am trying to access this as an ordinary registered user, and I want the logged in user to only be able to change his own usergroup this way.
 
I am still struggling with getting this to work. I started getting database errors when trying to view my form, so I decided to delete the list I had created. I then updated Joomla to 3.30 and updated Fabrik from the latest github (joomla3 - this morning) but I still don't seem to be able to get the results I want. I have a few other forms that are working fine, but I need to try to figure out how to create a form that picks up the current user, and when submitted, the current user's group can be changed.

I really need to get this working today if possible.
 
I just ordered professional subscription. It looks like the order is pending still so I can't use the pro support forum.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top