Add ACL generator component with test controller and route

This commit is contained in:
Kendrick Chan 2018-01-07 00:49:18 +08:00
parent 8b76707997
commit b25345a46d
5 changed files with 171 additions and 1 deletions

32
config/acl.yaml Normal file
View file

@ -0,0 +1,32 @@
access_keys:
- id: dashboard
label: Dashboard Access
acls:
- id: dashboard.menu
label: Menu
- id: user
label: User Access
acls:
- id: user.menu
label: Menu
- id: user.list
label: List
- id: user.add
label: Add
- id: user.update
label: Update
- id: user.delete
label: Delete
- id: role
label: Role Access
acls:
- id: role.menu
label: Menu
- id: role.list
label: List
- id: role.add
label: Add
- id: role.update
label: Update
- id: role.delete
label: Delete

View file

@ -17,3 +17,7 @@ logout:
user_list:
path: /users
controller: App\Controller\UserController::index
test_acl:
path: /test_acl
controller: App\Controller\TestController::index

View file

@ -15,7 +15,7 @@ services:
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Menu}'
exclude: '../src/{Entity,Migrations,Tests,Menu,Access}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
@ -31,3 +31,10 @@ services:
$router: "@router.default"
$cache_dir: "%kernel.cache_dir%"
$config_dir: "%kernel.root_dir%/../config"
App\Access\Generator:
class: App\Access\Generator
arguments:
$router: "@router.default"
$cache_dir: "%kernel.cache_dir%"
$config_dir: "%kernel.root_dir%/../config"

108
src/Access/Generator.php Normal file
View file

@ -0,0 +1,108 @@
<?php
namespace App\Access;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\RouterInterface;
class Generator
{
protected $router;
protected $cache_dir;
protected $config_dir;
public function __construct(RouterInterface $router, string $cache_dir, string $config_dir)
{
$this->router = $router;
$this->cache_dir = $cache_dir;
$this->config_dir = $config_dir;
}
public function getACL()
{
$key = 'access_keys';
// cache config
$cache_file = $this->cache_dir . '/' . $key . '.serial';
$cache = new ConfigCache($cache_file, true);
// cache not fresh
if (!$cache->isFresh())
{
$files = [];
$resources = [];
try
{
// get location of acl.yaml
$path = $this->config_dir . '/acl.yaml';
$files[] = $path;
$resources[] = new FileResource($path);
// process acl config file
$data = $this->parseMenu($path, $key);
}
catch (\InvalidArgumentException $e)
{
error_log($e->getMessage());
error_log($key . ' key not found in acl.yaml file.');
return $data;
}
$acl_serial = serialize($data);
$cache->write($acl_serial, $resources);
}
else
{
$acl_serial = file_get_contents($cache_file);
$data = unserialize($acl_serial);
}
return $data;
}
protected function parseMenu($path, $key)
{
$parser = new YamlParser();
$config = $parser->parse(file_get_contents($path));
// check if we have menu items
if (!isset($config[$key]))
{
error_log('No ' . $key . ' found for ' . $path);
return;
}
$acl_hierarchy = [];
$acl_index = [];
// go through each one
foreach ($config[$key] as $acl_data)
{
// build hierarchy
$acl_hierarchy[$acl_data['id']] = [
'label' => $acl_data['label'],
'acls' => []
];
foreach ($acl_data['acls'] as $acl)
{
$id = $acl['id'];
$label = $acl['label'];
// set hierarchy and index
$acl_hierarchy[$acl_data['id']]['acls'][$id] = $label;
$acl_index[$id] = $label;
}
}
return [
'hierarchy' => $acl_hierarchy,
'index' => $acl_index
];
}
}

View file

@ -0,0 +1,19 @@
<?php
namespace App\Controller;
use App\Ramcar\BaseController;
use App\Access\Generator;
class TestController extends BaseController
{
public function index(Generator $acl_gen)
{
$params = $this->initParameters('home');
$acl_data = $acl_gen->getACL();
error_log(print_r($acl_data, true));
return $this->render('home.html.twig', $params);
}
}