0

In my Joomla system there is an access level that grants permission to an area of the system.

This access level is granted to several different user groups.

In plugin code, I am at a position where I know that a user has been granted the access level, but I now need to know which of those groups he is a member of in order to have been granted it.

Is there an easy way to query for this?

  • 2
    Do any of these 2 have the answer for what you are looking for? -Echo Joomla 3.4 GroupId, and -How to display user profile information? – FFrewin Jan 21 '16 at 20:37
  • @FFrewin - I know about $user->groups, and that is useful but not what I'm asking for here. I'm looking for how to work out which one of $user->groups is linked to the access level that gave the user access to the current page. I know what access level it is, but I can't see how to get at the list of groups that is linked to the access level, which would then allow me to use array_intersect() or similar to tie it back to the user's groups. – Simba Jan 22 '16 at 8:58
1

Try this:

$user = JFactory::getUser();
$usergroups = JAccess::getGroupsByUser($user->id,false);

$db = JFactory::getDBO();
$query = $db->getQuery(true);   
$query->select('title, rules');
$query->from('#__viewlevels');
$db->setQuery($query);
$rows = $db->loadObjectList();

$viewlevels = array();
$levels = array();

foreach($rows as $viewlevel){
    $viewlevels[$viewlevel->title] = json_decode($viewlevel->rules);
}

foreach($viewlevels as $level => $viewgroups){
    $result = array_intersect($usergroups, $viewgroups);
    if(!empty($result)){
        $levels[] = $level;
    }
}

echo '<pre>';
print_r($levels);
echo '</pre>';

This will give you an array of view levels that the user has access to, you can then compare that to the view level of your current article, menu item, etc. The false flag sets getGroupsByUser to only return important groups (strips out public, guest, etc)

ALso if you want to filter out default access levels just add this to the query for the #__viewlevels table right after the "$query->from" statement on the next line:

$query->where("title NOT IN ('Guest','Public','Registered','Special','Super Users')");
  • Hi Terry. Thanks for the answer. This is pretty close to what I eventually wrote to solve this for myself, so I'm marking it as accepted. When I asked the question I had been hoping there would be a helper method already built into Joomla for this kind of thing but it seems not. – Simba Feb 8 '16 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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