Source for file access.php
Documentation is available at access.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
jimport('joomla.utilities.arrayhelper');
* Class that handles all access authorisation routines.
* @package Joomla.Platform
protected static $viewLevels =
array();
* Array of rules for the asset
protected static $assetRules =
array();
protected static $userGroups =
array();
* Array of user group paths.
protected static $userGroupPaths =
array();
* Array of cached groups by user.
protected static $groupsByUser =
array();
* Method for clearing static caches.
self::$viewLevels =
array();
self::$assetRules =
array();
self::$userGroups =
array();
self::$userGroupPaths =
array();
self::$groupsByUser =
array();
* Method to check if a user is authorised to perform an action, optionally on an asset.
* @param integer $userId Id of the user for which to check authorisation.
* @param string $action The name of the action to authorise.
* @param mixed $asset Integer asset id or the name of the asset as a string. Defaults to the global asset node.
* @return boolean True if authorised.
public static function check($userId, $action, $asset =
null)
// Default to the root asset node.
$rootId =
$assets->getRootId();
// Get the rules for the asset recursively to root if not already retrieved.
if (empty(self::$assetRules[$asset]))
self::$assetRules[$asset] =
self::getAssetRules($asset, true);
// Get all groups against which the user is mapped.
$identities =
self::getGroupsByUser($userId);
return self::$assetRules[$asset]->allow($action, $identities);
* Method to check if a group is authorised to perform an action, optionally on an asset.
* @param integer $groupId The path to the group for which to check authorisation.
* @param string $action The name of the action to authorise.
* @param mixed $asset Integer asset id or the name of the asset as a string. Defaults to the global asset node.
* @return boolean True if authorised.
public static function checkGroup($groupId, $action, $asset =
null)
$groupId = (int)
$groupId;
// Get group path for group
$groupPath =
self::getGroupPath($groupId);
// Default to the root asset node.
// TODO: $rootId doesn't seem to be used!
$rootId =
$assets->getRootId();
// Get the rules for the asset recursively to root if not already retrieved.
if (empty(self::$assetRules[$asset]))
self::$assetRules[$asset] =
self::getAssetRules($asset, true);
return self::$assetRules[$asset]->allow($action, $groupPath);
* Gets the parent groups that a leaf group belongs to in its branch back to the root of the tree
* (including the leaf group id).
* @param mixed $groupId An integer or array of integers representing the identities to check.
* @return mixed True if allowed, false for an explicit deny, null for an implicit deny.
if (empty(self::$userGroups))
$db =
JFactory::getDbo();
$query =
$db->getQuery(true)
->select('parent.id, parent.lft, parent.rgt')
->from('#__usergroups AS parent')
self::$userGroups =
$db->loadObjectList('id');
// Make sure groupId is valid
if (!array_key_exists($groupId, self::$userGroups))
// Get parent groups and leaf group
if (!isset
(self::$userGroupPaths[$groupId]))
self::$userGroupPaths[$groupId] =
array();
foreach (self::$userGroups as $group)
if ($group->lft <=
self::$userGroups[$groupId]->lft &&
$group->rgt >=
self::$userGroups[$groupId]->rgt)
self::$userGroupPaths[$groupId][] =
$group->id;
return self::$userGroupPaths[$groupId];
* Method to return the JAccessRules object for an asset. The returned object can optionally hold
* only the rules explicitly set for the asset or the summation of all inherited rules from
* parent assets and explicit rules.
* @param mixed $asset Integer asset id or the name of the asset as a string.
* @param boolean $recursive True to return the rules object with inherited rules.
* @return JAccessRules JAccessRules object for the asset.
// Get the database connection object.
$db =
JFactory::getDbo();
// Build the database query to get the rules for the asset.
$query =
$db->getQuery(true)
->select($recursive ?
'b.rules' :
'a.rules')
->from('#__assets AS a');
$query->group($recursive ?
'b.id, b.rules, b.lft' :
'a.id, a.rules, a.lft');
// If the asset identifier is numeric assume it is a primary key, else lookup by name.
$query->where('(a.id = ' . (int)
$asset .
')');
$query->where('(a.name = ' .
$db->quote($asset) .
')');
// If we want the rules cascading up to the global asset node we need a self-join.
$query->join('LEFT', '#__assets AS b ON b.lft <= a.lft AND b.rgt >= a.rgt')
// Execute the query and load the rules from the result.
$result =
$db->loadColumn();
// Get the root even if the asset is not found and in recursive mode
$rootId =
$assets->getRootId();
->where('id = ' .
$db->quote($rootId));
$result =
$db->loadResult();
$result =
array($result);
// Instantiate and return the JAccessRules object for the asset rules.
$rules->mergeCollection($result);
* Method to return a list of user groups mapped to a user. The returned list can optionally hold
* only the groups explicitly mapped to the user or all groups both explicitly mapped and inherited
* @param integer $userId Id of the user for which to get the list of groups.
* @param boolean $recursive True to include inherited user groups.
* @return array List of user group ids to which the user is mapped.
// Creates a simple unique string for each parameter combination:
$storeId =
$userId .
':' . (int)
$recursive;
if (!isset
(self::$groupsByUser[$storeId]))
// TODO: Uncouple this from JComponentHelper and allow for a configuration setting or value injection.
if (class_exists('JComponentHelper'))
$guestUsergroup =
JComponentHelper::getParams('com_users')->get('guest_usergroup', 1);
// Guest user (if only the actually assigned group is requested)
if (empty($userId) &&
!$recursive)
$result =
array($guestUsergroup);
// Registered user and guest if all groups are requested
// Build the database query to get the rules for the asset.
$query =
$db->getQuery(true)
->select($recursive ?
'b.id' :
'a.id');
$query->from('#__usergroups AS a')
->where('a.id = ' . (int)
$guestUsergroup);
$query->from('#__user_usergroup_map AS map')
->where('map.user_id = ' . (int)
$userId)
->join('LEFT', '#__usergroups AS a ON a.id = map.group_id');
// If we want the rules cascading up to the global asset node we need a self-join.
$query->join('LEFT', '#__usergroups AS b ON b.lft <= a.lft AND b.rgt >= a.rgt');
// Execute the query and load the rules from the result.
$result =
$db->loadColumn();
// Clean up any NULL or duplicate values, just in case
self::$groupsByUser[$storeId] =
$result;
return self::$groupsByUser[$storeId];
* Method to return a list of user Ids contained in a Group
* @param integer $groupId The group Id
* @param boolean $recursive Recursively include all child groups (optional)
* @todo This method should move somewhere else
// Get a database object.
$db =
JFactory::getDbo();
$test =
$recursive ?
'>=' :
'=';
// First find the users contained in the group
$query =
$db->getQuery(true)
->select('DISTINCT(user_id)')
->from('#__usergroups as ug1')
->join('INNER', '#__usergroups AS ug2 ON ug2.lft' .
$test .
'ug1.lft AND ug1.rgt' .
$test .
'ug2.rgt')
->join('INNER', '#__user_usergroup_map AS m ON ug2.id=m.group_id')
->where('ug1.id=' .
$db->quote($groupId));
$result =
$db->loadColumn();
// Clean up any NULL values, just in case
* Method to return a list of view levels for which the user is authorised.
* @param integer $userId Id of the user for which to get the list of authorised view levels.
* @return array List of view levels for which the user is authorised.
// 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();
$query =
$db->getQuery(true)
->from($db->quoteName('#__viewlevels'));
// Set the query for execution.
// Build the view levels array.
foreach ($db->loadAssocList() as $level)
self::$viewLevels[$level['id']] = (array)
json_decode($level['rules']);
// Initialise the authorised array.
// Find the authorised levels.
foreach (self::$viewLevels as $level =>
$rule)
if (($id <
0) &&
(($id * -
1) ==
$userId))
// Check to see if the group is mapped to the level.
elseif (($id >=
0) &&
in_array($id, $groups))
* Method to return a list of actions for which permissions can be set given a component and section.
* @param string $component The component from which to retrieve the actions.
* @param string $section The name of the section within the component from which to retrieve the actions.
* @return array List of actions available for the given component and section.
* @deprecated 12.3 (Platform) & 4.0 (CMS) Use JAccess::getActionsFromFile or JAccess::getActionsFromData instead.
public static function getActions($component, $section =
'component')
JLog::add(__METHOD__ .
' is deprecated. Use JAccess::getActionsFromFile or JAccess::getActionsFromData instead.', JLog::WARNING, 'deprecated');
$actions =
self::getActionsFromFile(
"/access/section[@name='" .
$section .
"']/"
* Method to return a list of actions from a file for which permissions can be set.
* @param string $file The path to the XML file.
* @param string $xpath An optional xpath to search for the fields.
* @return boolean|array False if case of error or the list of actions available.
public static function getActionsFromFile($file, $xpath =
"/access/section[@name='component']/")
// If unable to find the file return false.
// Else return the actions from the xml.
return self::getActionsFromData($xml, $xpath);
* Method to return a list of actions from a string or from an xml for which permissions can be set.
* @param string|SimpleXMLElement $data The XML string or an XML element.
* @param string $xpath An optional xpath to search for the fields.
* @return boolean|array False if case of error or the list of actions available.
public static function getActionsFromData($data, $xpath =
"/access/section[@name='component']/")
// If the data to load isn't already an XML element or string return false.
if ((!($data instanceof
SimpleXMLElement)) &&
(!is_string($data)))
// Attempt to load the XML if a string.
$data =
new SimpleXMLElement($data);
// Make sure the XML loaded correctly.
// Initialise the actions array
// Get the elements from the xpath
$elements =
$data->xpath($xpath .
'action[@name][@title][@description]');
// If there some elements, analyse them
foreach ($elements as $action)
// Add the action to the actions array
$actions[] = (object)
array(
'name' => (string)
$action['name'],
'title' => (string)
$action['title'],
'description' => (string)
$action['description']
// Finally return the actions array
Documentation generated on Tue, 19 Nov 2013 14:53:15 +0100 by phpDocumentor 1.4.3