6

For determining whether user is a super user, we do something like this :

$user = JFactory::getUser();
$isAdmin = $user->get('isRoot');
if ($isAdmin) {
echo 'You are an Administrator';

What would be the best way to know that the logged in user belongs to a specific usergroup or has some specific viewing access level?

1 Answer 1

11

To get an array of groups the currently logged in user belong to, you can use the following:

$user   = JFactory::getUser();
$groups = JAccess::getGroupsByUser($user->id);

var_dump($groups);

To get the names of these groups, you can run a database query (add after the code above)

$user   = JFactory::getUser();
$groups = JAccess::getGroupsByUser($user->id);
$groups_list = '(' . implode(',', $groups) . ')';

$db = JFactory::getDbo();
$query = $db->getQuery(true)
    ->select($db->qn('title'))
    ->from('#__usergroups')
    ->where($db->qn('id') . ' IN ' . $groups_list);
$db->setQuery($query);
$rows = $db->loadObjectList();

var_dump($rows);

To get the user's access levels, you can use the following:

$user   = JFactory::getUser();
$levels = JAccess::getAuthorisedViewLevels($user->id);

var_dump($levels);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.