how to add a user to a group

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
User avatar
mtrivino
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Aug 19, 2009 9:47 pm
Location: Bogotá, Colombia
Contact:

how to add a user to a group

Post by mtrivino » Tue Apr 03, 2012 5:07 pm

Hello everyone,

I wanna add a user to a group after he's completed a task, so I was wondering if there's a method for that, i.e., given a user and a group, I want to add the user to the group.

I know I could just go ahead and write a database query, but I'm guessing there's an elegant way... ;)

Any ideas?

Thanks.

M.
Last edited by mtrivino on Fri Apr 06, 2012 1:13 pm, edited 1 time in total.

User avatar
Ratmil
Joomla! Explorer
Joomla! Explorer
Posts: 318
Joined: Sat Dec 06, 2008 7:07 pm

Re: how to add a user to a group

Post by Ratmil » Wed Apr 04, 2012 12:53 pm

The following should work:
JUserHelper::addUserToGroup($userId, $groupId);

User avatar
Ratmil
Joomla! Explorer
Joomla! Explorer
Posts: 318
Joined: Sat Dec 06, 2008 7:07 pm

Re: how to add a user to a group

Post by Ratmil » Wed Apr 04, 2012 1:25 pm

Actually for the changes to be saved you would have to this:

Code: Select all

$user = JFactory::getUser($user_id);
JUserHelper::addUserToGroup($user_id, $group_id);
$user->save();
This has been tested.

User avatar
mtrivino
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Aug 19, 2009 9:47 pm
Location: Bogotá, Colombia
Contact:

Re: how to add a user to a group

Post by mtrivino » Wed Apr 04, 2012 1:42 pm

Hello Ratmil,

thanks a lot. I guess I just didn't know where to look. :D

Cheers,

M.


Locked

Return to “Joomla! 2.5 Coding”