From b25345a46dc638565abaedc0516b69967331d562 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 7 Jan 2018 00:49:18 +0800 Subject: [PATCH] Add ACL generator component with test controller and route --- config/acl.yaml | 32 +++++++++ config/routes.yaml | 4 ++ config/services.yaml | 9 ++- src/Access/Generator.php | 108 ++++++++++++++++++++++++++++++ src/Controller/TestController.php | 19 ++++++ 5 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 config/acl.yaml create mode 100644 src/Access/Generator.php create mode 100644 src/Controller/TestController.php diff --git a/config/acl.yaml b/config/acl.yaml new file mode 100644 index 00000000..cc59b3fb --- /dev/null +++ b/config/acl.yaml @@ -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 diff --git a/config/routes.yaml b/config/routes.yaml index f2c6674d..e2127d9a 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -17,3 +17,7 @@ logout: user_list: path: /users controller: App\Controller\UserController::index + +test_acl: + path: /test_acl + controller: App\Controller\TestController::index diff --git a/config/services.yaml b/config/services.yaml index 927ff453..6d6cf31a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -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" diff --git a/src/Access/Generator.php b/src/Access/Generator.php new file mode 100644 index 00000000..1ec1d098 --- /dev/null +++ b/src/Access/Generator.php @@ -0,0 +1,108 @@ +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 + ]; + } +} diff --git a/src/Controller/TestController.php b/src/Controller/TestController.php new file mode 100644 index 00000000..04eb7c11 --- /dev/null +++ b/src/Controller/TestController.php @@ -0,0 +1,19 @@ +initParameters('home'); + + $acl_data = $acl_gen->getACL(); + error_log(print_r($acl_data, true)); + + return $this->render('home.html.twig', $params); + } +}