Rod Farrell
Rod Farrell

Reputation: 31

How to change a Joomla User Group with php

In a Joomla 2.5 site I want to assign a registered user to another group after they complete a form. The user would be logged in when they complete the form which will be created in Fabrik so that I can add a PHP script to run on save. My php skills are rudimentary. What would the lines in php look like that adds the current user to a group?

Upvotes: 3

Views: 6603

Answers (5)

cwgso
cwgso

Reputation: 421

I'm using Joomla 3.7.5, and this method worked for me:

JUserHelper::setUserGroups($joomlaId, array(10));

Where, $joomlaId is the id of the user I want to change. This sets the user to only be a member of group 10, and saves the changes to the user DB immediately.

Upvotes: 2

Kostas
Kostas

Reputation: 11

You may also want to look at the addUserToGroup method (since Joomla Platform 11.1)

JUserHelper::addUserToGroup (JFactory::getUser()->id, 3); // 3 Is the group number

So for example if your user was previously in the 'registered' group (id=2), he/she will also be assigned to group 3 after this.

Similarly there is also the removeUserFromGroup method which does the opposite.

As per the documentation, it may be useful to note that the setUserGroups method supports an array for the groups, whereas the other 2 methods I mention here support an integer.

setUserGroups(integer $userId, array $groups) : boolean

addUserToGroup(integer $userId, integer $groupId) : boolean

removeUserFromGroup(integer $userId, integer $groupId) : boolean

https://api.joomla.org/cms-3/classes/JUserHelper.html

Upvotes: 1

Jeremy Warne
Jeremy Warne

Reputation: 3429

I couldn't find any useful way to do this in joomla 3, so here's a quickie for people who just need this to work:

$db = JFactory::getDbo();
$db->setQuery("replace into xo6uf_user_usergroup_map values ($user_id, $group_id)");
$db->query();

Upvotes: 0

Bandula Dharmadasa
Bandula Dharmadasa

Reputation: 867

Here I used a different approach as JUserHelper::setUserGroups method always failed with my Joomla 3.3.1 installation.

$user = JFactory::getUser();//get current user
$g = array('2'=>2,'3'=>3);//2 for registered user, 3 for author
$user->groups = $g;
$user->save();

Upvotes: 2

Brian
Brian

Reputation: 1823

You can use JUserHelper::setUserGroups to set the group(s) of a user.

To retrieve the current user you can use JFactory::getUser.

When you have the user, you can (I haven't confirmed this) probably get the id right away. Some non-tested code which will hopefully get you on the way:

 $user = JFactory::getUser();
 $userId = $user->id;
 JUserHelper::setUserGroups($userId, 3);

Or in short:

 JUserHelper::setUserGroups(JFactory::getUser()->id, 3); // 3 Is the group number

You can find the group ID's by going into the table-prefix_usergroups table and retrieve the primary key. I haven't found a way to retrieve a list of groups without going into the database, but this may help.

A screenshot of my (non-edited) database with usergroups:

Usergroups joomla 2.5

Upvotes: 4

Related Questions