如何检查用户是否在 Joomla 3 的用户组 "Friends" 中?

How can I check if user is in usergroup "Friends" in Joomla 3?

如果当前用户在 Joomla 3 中名为 "Friends"(标题)的用户组中,我该如何检查?

        $user = JUser::getInstance($result->id);

        $groups = $user->getAuthorisedGroups(); // just IDs, not title

谢谢。

查看此代码,希望对您有所帮助。

$AllGroups = $user->get('groups');
// getting all the groups

foreach($AllGroups as $groupId) {
    $user   = JFactory::getUser(); // getting user data
    $db     = JFactory::getDBO();
    $userid = $user->get('id'); // logged in user id
    $groups = JAccess::getGroupsByUser($userid); 
    $groupid_list      = '(' . implode(',', $groups) . ')';
    $query  = $db->getQuery(true);
    $query->select('id, title');
    $query->from('#__usergroups');
    $query->where('id IN ' .$groupid_list);
    $db->setQuery($query);
    $rows   = $db->loadRowList();
    $grouplist   = '';
    foreach($rows as $group)
    {
       if ($groupId == $group[0]) { // checking the logged in user group
            $grouplist = $group[1];
       }
    }
var_dump($grouplist); // it will give you the usergroup name
}
        $user = JUser::getInstance($result->id);

        $AllGroups = $user->get('groups');

        $groupNames = array();
        $userid = $user->get('id'); // logged in user id
        $groups = JAccess::getGroupsByUser($userid);
        $groupid_list = '(' . implode(',', $groups) . ')';

        foreach ($AllGroups as $groupId) {
            $query = $db->getQuery(true);
            $query->select('id, title');
            $query->from('#__usergroups');
            $query->where('id IN ' . $groupid_list);
            $db->setQuery($query);
            $rows = $db->loadRowList();
            $grouplist = '';
            foreach ($rows as $group) {
                if ($groupId == $group[0]) { // checking the logged in user group
                    $grouplist = $group[1];
                }
            }
            array_push($groupNames, $grouplist);
        }

        $query = $db->getQuery(true)
            ->select('*')
            ->from('#__myTable');

        if (in_array("Friends", $groupNames)) {
            // User is in group "Friends"

        }