10 defined(
'JPATH_PLATFORM') or die;
12 jimport('joomla.utilities.arrayhelper');
29 protected static $viewLevels = array();
37 protected static $assetRules = array();
45 protected static $userGroups = array();
53 protected static $userGroupPaths = array();
61 protected static $groupsByUser = array();
70 public static function clearStatics()
72 self::$viewLevels = array();
73 self::$assetRules = array();
74 self::$userGroups = array();
75 self::$userGroupPaths = array();
76 self::$groupsByUser = array();
90 public static function check($userId, $action, $asset = null)
93 $userId = (int) $userId;
95 $action = strtolower(preg_replace(
'#[\s\-]+#',
'.', trim($action)));
96 $asset = strtolower(preg_replace(
'#[\s\-]+#',
'.', trim($asset)));
103 $rootId = $assets->getRootId();
108 if (empty(self::$assetRules[$asset]))
110 self::$assetRules[$asset] = self::getAssetRules($asset,
true);
114 $identities = self::getGroupsByUser($userId);
115 array_unshift($identities, $userId * -1);
117 return self::$assetRules[$asset]->allow($action, $identities);
131 public static function checkGroup($groupId, $action, $asset = null)
134 $groupId = (int) $groupId;
135 $action = strtolower(preg_replace(
'#[\s\-]+#',
'.', trim($action)));
136 $asset = strtolower(preg_replace(
'#[\s\-]+#',
'.', trim($asset)));
139 $groupPath = self::getGroupPath($groupId);
147 $rootId = $assets->getRootId();
151 if (empty(self::$assetRules[$asset]))
153 self::$assetRules[$asset] = self::getAssetRules($asset,
true);
156 return self::$assetRules[$asset]->allow($action, $groupPath);
169 protected static function getGroupPath($groupId)
172 if (empty(self::$userGroups))
175 $query = $db->getQuery(
true)
176 ->select(
'parent.id, parent.lft, parent.rgt')
177 ->from(
'#__usergroups AS parent')
178 ->order(
'parent.lft');
179 $db->setQuery($query);
180 self::$userGroups = $db->loadObjectList(
'id');
184 if (!array_key_exists($groupId, self::$userGroups))
190 if (!isset(self::$userGroupPaths[$groupId]))
192 self::$userGroupPaths[$groupId] = array();
194 foreach (self::$userGroups as $group)
196 if ($group->lft <= self::$userGroups[$groupId]->lft && $group->rgt >= self::$userGroups[$groupId]->rgt)
198 self::$userGroupPaths[$groupId][] = $group->id;
203 return self::$userGroupPaths[$groupId];
218 public static function getAssetRules($asset, $recursive =
false)
224 $query = $db->getQuery(
true)
225 ->select($recursive ?
'b.rules' :
'a.rules')
226 ->from(
'#__assets AS a');
229 $query->group($recursive ?
'b.id, b.rules, b.lft' :
'a.id, a.rules, a.lft');
232 if (is_numeric($asset))
234 $query->where(
'(a.id = ' . (
int) $asset .
')');
238 $query->where(
'(a.name = ' . $db->quote($asset) .
')');
244 $query->join(
'LEFT',
'#__assets AS b ON b.lft <= a.lft AND b.rgt >= a.rgt')
249 $db->setQuery($query);
250 $result = $db->loadColumn();
257 $rootId = $assets->getRootId();
261 ->where(
'id = ' . $db->quote($rootId));
262 $db->setQuery($query);
263 $result = $db->loadResult();
264 $result = array($result);
285 public static function getGroupsByUser($userId, $recursive =
true)
288 $storeId = $userId .
':' . (int) $recursive;
290 if (!isset(self::$groupsByUser[$storeId]))
293 if (class_exists(
'JComponentHelper'))
295 $guestUsergroup = JComponentHelper::getParams(
'com_users')->get(
'guest_usergroup', 1);
303 if (empty($userId) && !$recursive)
305 $result = array($guestUsergroup);
313 $query = $db->getQuery(
true)
314 ->select($recursive ?
'b.id' :
'a.id');
318 $query->from(
'#__usergroups AS a')
319 ->where(
'a.id = ' . (
int) $guestUsergroup);
323 $query->from(
'#__user_usergroup_map AS map')
324 ->where(
'map.user_id = ' . (
int) $userId)
325 ->join(
'LEFT',
'#__usergroups AS a ON a.id = map.group_id');
331 $query->join(
'LEFT',
'#__usergroups AS b ON b.lft <= a.lft AND b.rgt >= a.rgt');
335 $db->setQuery($query);
336 $result = $db->loadColumn();
343 $result = array(
'1');
347 $result = array_unique($result);
351 self::$groupsByUser[$storeId] = $result;
354 return self::$groupsByUser[$storeId];
368 public static function getUsersByGroup($groupId, $recursive =
false)
373 $test = $recursive ?
'>=' :
'=';
376 $query = $db->getQuery(
true)
377 ->select(
'DISTINCT(user_id)')
378 ->from(
'#__usergroups as ug1')
379 ->join(
'INNER',
'#__usergroups AS ug2 ON ug2.lft' . $test .
'ug1.lft AND ug1.rgt' . $test .
'ug2.rgt')
380 ->join(
'INNER',
'#__user_usergroup_map AS m ON ug2.id=m.group_id')
381 ->where(
'ug1.id=' . $db->quote($groupId));
383 $db->setQuery($query);
385 $result = $db->loadColumn();
402 public static function getAuthorisedViewLevels($userId)
405 $groups = self::getGroupsByUser($userId);
408 if (empty(self::$viewLevels))
414 $query = $db->getQuery(
true)
415 ->select(
'id, rules')
416 ->from($db->quoteName(
'#__viewlevels'));
419 $db->setQuery($query);
422 foreach ($db->loadAssocList() as $level)
424 self::$viewLevels[$level[
'id']] = (array) json_decode($level[
'rules']);
429 $authorised = array(1);
432 foreach (self::$viewLevels as $level => $rule)
434 foreach ($rule as $id)
436 if (($id < 0) && (($id * -1) == $userId))
438 $authorised[] = $level;
442 elseif (($id >= 0) && in_array($id, $groups))
444 $authorised[] = $level;
465 public static function getActions($component, $section =
'component')
467 JLog::add(__METHOD__ .
' is deprecated. Use JAccess::getActionsFromFile or JAccess::getActionsFromData instead.',
JLog::WARNING,
'deprecated');
469 $actions = self::getActionsFromFile(
470 JPATH_ADMINISTRATOR .
'/components/' . $component .
'/access.xml',
471 "/access/section[@name='" . $section .
"']/"
494 public static function getActionsFromFile($file, $xpath =
"/access/section[@name='component']/")
496 if (!is_file($file) || !is_readable($file))
504 $xml = simplexml_load_file($file);
506 return self::getActionsFromData($xml, $xpath);
520 public static function getActionsFromData($data, $xpath =
"/access/section[@name='component']/")
523 if ((!($data instanceof SimpleXMLElement)) && (!is_string($data)))
529 if (is_string($data))
533 $data =
new SimpleXMLElement($data);
551 $elements = $data->xpath($xpath .
'action[@name][@title][@description]');
554 if (!empty($elements))
556 foreach ($elements as $action)
559 $actions[] = (object) array(
560 'name' => (
string) $action[
'name'],
561 'title' => (string) $action[
'title'],
562 'description' => (
string) $action[
'description']