2

我正在尝试将用户添加到组中。我可以运行这个 PHP 代码而没有任何错误,但用户组仍然没有改变。

<?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);

?>
4

3 回答 3

1

可能的“简单”解决方案:

代码是正确的,它应该把你的$userIdand$groupId放在数据库中,以准确地放在#__user_usergroup_map.

顺便说一句,如果您使用错误,则认为此方法会引发错误,groupId但如果您插入错误,则不会引发任何错误$userId,而对于错误,我的意思是它不存在。

所以有些用户$userId = 358;不存在的canches。

更新 - 硬调试:

好的,在这种情况下,我建议您挖掘助手的代码。

该文件是:

libraries/joomla/user/helper.php

在第 33 行你有JUserHelper::addUserToGroup.

这是代码:

    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;
}

保存组的位是$user->save();
尝试var_dump()直到那里,看看问题出在哪里。

于 2015-04-12T07:45:59.910 回答
1

我在支付回调中遇到了同样的问题。我发现用户组正确保存在数据库中,但没有在Juser对象中刷新(因为您将用户添加到不同会话中的组)。当用户在页面上进行交互时,组将被恢复。

我发现的另一个想法是,如果用户登录,则在管理员面板中更改组的工作方式相同。

为了处理它,我制作了系统插件,并且在onAfterInitialise功能上我做了:

//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);
    }
}

希望它会有所帮助。

于 2015-07-03T19:29:14.907 回答
0

作者已经 halfish 解决了这个问题,所以这个答案可能对其他人有所帮助。我用 Eclipse 和 XDebug 调试了几个小时才发现问题。这个错误非常棘手,因为addUserToGroup()正在为我返回true,并且用户对象也成功更改,但它们没有保存在数据库中。问题是onUserBeforeSave()我的插件中的方法在addUserToGroup()尝试保存用户时抛出异常。因此,请检查您onUserBeforeSave()是否触及它的实现。如果不是,您必须使用 XDebug 安装 Eclipse 并尝试调试您的确切问题。

于 2015-11-27T20:09:47.797 回答