2

I am trying to add a user to a group. I can run this PHP code without any errors, but the user group is still not changed.

<?php

define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );

$userId = 358;
$groupId = 11;

echo JUserHelper::addUserToGroup($userId, $groupId);

?>
Chris Smith
  • 2,928
  • 4
  • 27
  • 59
  • I am still trying to figure it out. – Chris Smith Apr 15 '15 at 23:37
  • It wasn't the user id? Did you check the table I told you? – borracciaBlu Apr 16 '15 at 00:02
  • Yes I did, every thing is exactly correct. Nothing is put into the DB and the user and group IDs are correct. – Chris Smith Apr 16 '15 at 00:11
  • I update the answer to help you in the debugging. Try that and let me know :) – borracciaBlu Apr 16 '15 at 00:23
  • So dumping the `$user` variable before and after adding the group, before, it says I am in the Registered and Guest groups. After, I am in Those and the group I am trying to add. So for some reason, it is not saving it correctly. One thing to note is that the user is not logged in while this script is running, it's a callback from PayPal to deal with their purchase. – Chris Smith Apr 16 '15 at 10:21

3 Answers3

1

Possible "Easy" Solution:

The code is correct and it should put your $userId and $groupId in the db to be precise in #__user_usergroup_map .

Btw consider that this method is rising an error if you use a wrong groupId but it's not raising any error if you insert a wrong $userId and for wrong I mean that it doesn't exist.

So there are canches that the user with $userId = 358; doesn't exist.

Update - Hard Debugging:

Ok in this case I suggest you to digg in the code of the helper.

The file is :

libraries/joomla/user/helper.php

On line 33 You have JUserHelper::addUserToGroup.

This is the code:

    public static function addUserToGroup($userId, $groupId)
    {
        // Get the user object.
        $user = new JUser((int) $userId);

        // Add the user to the group if necessary.
        if (!in_array($groupId, $user->groups))
        {
            // Get the title of the group.
            $db = JFactory::getDbo();
            $query = $db->getQuery(true)
                ->select($db->quoteName('title'))
                ->from($db->quoteName('#__usergroups'))
                ->where($db->quoteName('id') . ' = ' . (int) $groupId);
            $db->setQuery($query);
            $title = $db->loadResult();

            // If the group does not exist, return an exception.
            if (!$title)
            {
                throw new RuntimeException('Access Usergroup Invalid');
            }

            // Add the group data to the user object.
            $user->groups[$title] = $groupId;

        // Store the user object.
        $user->save();
    }

    if (session_id())
    {
        // Set the group data for any preloaded user objects.
        $temp = JFactory::getUser((int) $userId);
        $temp->groups = $user->groups;

        // Set the group data for the user object in the session.
        $temp = JFactory::getUser();

        if ($temp->id == $userId)
        {
            $temp->groups = $user->groups;
        }
    }

    return true;
}

The bit that save the group is $user->save();.
Try to var_dump() till there and see where is the issue.

borracciaBlu
  • 4,017
  • 3
  • 33
  • 41
1

I run in the same issue with payment callback. I found that user groups save in the database correctly, but are not refreshed in Juser object (becouse You add user to group in different session). When user interacts on a page groups are restored.

Another think that i found is that changing groups in administrator panel works the same way if user is logged in.

To deal with it I made system plugin and in onAfterInitialise function i do:

//get user
$me = JFactory::getUser();
//check if user is logged in
if($me->id){
    //get groups
    $groups = JUserHelper::getUserGroups($me->id);
    //check if current user object has right groups
    if($me->groups != $groups){
        //if not update groups and clear session access levels
        $me->groups = $groups;
        $me->set('_authLevels', null);
    }
}

Hope it will help.

bast
  • 11
  • 2
  • Yes, I the problem now is dealing with logged in users. It correctly updates in the database, but currently logged in users have to relog to apply the changes. I created the plugin that you suggested, and sadly, it does not work. Do you mind posting a full download of the plugin somewhere to see if I did something wrong? I am also using Joomla 3.0 if that is what you are using. – Chris Smith Jul 03 '15 at 22:48
  • I added an echo into the onAfterInitialise function and it gets called. The problem is still that the user groups are not updated. – Chris Smith Jul 03 '15 at 23:20
  • Sorry for my late reply, I am new with stackoverflow account and thought I would get an e-mail when someone comment's my answer (but i did not). Did You check if plugin change anything in $me->groups? Is it wrong when plugin check it against $groups? – bast Feb 16 '16 at 09:12
0

Author already halfish solved this problem so this answer may help to others. It took me hours of debugging with Eclipse and XDebug to find the issue. This error was very tricky because addUserToGroup() was returning true for me and user object was as well with successful changes, but they were not saved in database. The problem was that onUserBeforeSave() method in my plugin was throwing exception whenever addUserToGroup() was trying to save a user. So check your implementation of onUserBeforeSave() if you touched it. If not you must install Eclipse with XDebug and try to debug what is your exact problem.

Lukas Greblikas
  • 649
  • 6
  • 14