如何检查用户是否在Joomla 3的用户组“朋友”中?

时间:2016-04-04 05:15:19

标签: joomla joomla3.4 usergroups

如果当前用户在Joomla 3中被称为“好友”(标题)的用户组中,我该如何检查?

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

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

感谢。

2 个答案:

答案 0 :(得分:1)

检查此代码,我希望它能为您提供帮助。

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

答案 1 :(得分:0)

        $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"

        }