JAccess/getAuthorisedViewLevels
From Joomla! Documentation
< JAccess
Contents |
Description
Method to return a list of view levels for which the user is authorised.
Syntax
static JAccess::getAuthorisedViewLevels($userId)
| Parameter Name | Default Value | Description |
|---|---|---|
| $userId | Id of the user for which to get the list of authorised view levels. |
Returns
array List of view levels for which the user is authorised.
Defined in
libraries/joomla/access/access.php
Importing
jimport( 'joomla.access.access' );
Source Body
public static function getAuthorisedViewLevels($userId)
{
// Get all groups that the user is mapped to recursively.
$groups = self::getGroupsByUser($userId);
// Only load the view levels once.
if (empty(self::$viewLevels))
{
// Get a database object.
$db = JFactory::getDBO();
// Build the base query.
$query = new JQuery;
$query->select('id, rules');
$query->from('`#__viewlevels`');
// Set the query for execution.
$db->setQuery((string) $query);
// Build the view levels array.
foreach ($db->loadAssocList() as $level)
{
self::$viewLevels[$level['id']] = (array) json_decode($level['rules']);
}
}
// Initialize the authorised array.
$authorised = array(1);
// Find the authorized levels.
foreach (self::$viewLevels as $level => $rule)
{
foreach ($rule as $id)
{
if (($id < 0) && (($id * -1) == $userId))
{
$authorised[] = $level;
break;
}
// Check to see if the group is mapped to the level.
elseif (($id >= 0) && in_array($id, $groups))
{
$authorised[] = $level;
break;
}
}
}
return $authorised;
}