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')");
$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 usearray_intersect()
or similar to tie it back to the user's groups.