Skip to content

Instantly share code, notes, and snippets.

@rvsjoen
Created June 21, 2012 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvsjoen/2965715 to your computer and use it in GitHub Desktop.
Save rvsjoen/2965715 to your computer and use it in GitHub Desktop.
ACL Actions with inheritance
<?xml version="1.0" encoding="utf-8"?>
<access component="com_test">
<section name="component">
<action name="core.admin" title="JACTION_ADMIN" description="JACTION_ADMIN_COMPONENT_DESC" />
<action name="core.manage" title="JACTION_MANAGE" description="JACTION_MANAGE_COMPONENT_DESC" />
<action name="core.create" title="JACTION_CREATE" description="JACTION_CREATE_COMPONENT_DESC" />
<action name="core.delete" title="JACTION_DELETE" description="JACTION_DELETE_COMPONENT_DESC" />
<action name="core.edit" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" />
<action name="test.manage" title="JACTION_EDIT" description="JACTION_EDIT_COMPONENT_DESC" inherit="core.delete"/>
</section>
</access>
<?php defined('_JEXEC') or die ;
class TestHelper
{
protected static $actions = null;
public static function authorise($action, $asset = 'com_test')
{
// We should cache this
if(is_null(self::$actions)){
self::$actions = JAccess::getActionsFromFile(JPATH_COMPONENT_ADMINISTRATOR.'/access.xml');
}
$user = JFactory::getUser();
$result = $user->authorize($action, $asset);
$actionObj = null;
// Check if we inherit from something
if(is_null($result)) {
$filter = array_filter(self::$actions,
function ($var) use ($action) {
return ($var->name == $action);
}
);
$actionObj = array_shift($filter);
}
// Now loop down the hierarchy to see if we have a match somewhere
while(is_null($result) && $actionObj->inherit != '') {
$action = $actionObj->inherit;
$filter = array_filter(self::$actions,
function ($var) use ($action) {
return ($var->name == $action);
}
);
$actionObj = array_shift($filter);
$result = $user->authorize($action, $asset);
}
return $result;
}
}
<?php
$actions = JAccess::getActionsFromFile(JPATH_COMPONENT_ADMINISTRATOR.'/access.xml');
$user = JFactory::getUser(398);
foreach($actions as $action) {
$result = $user->authorize($action->name, 'com_test');
while(is_null($result) && $action->inherit != ''){
$filter = array_filter($actions,
function ($var) use ($action) {
return ($var->name == $action->inherit);
}
);
$action = array_shift($filter);
$result = $user->authorize($action->name, 'com_test');
}
var_dump($action, $result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment