Add the access key and yaml file as parameters for the Generator class. #194
This commit is contained in:
parent
eaac1dfb7d
commit
3862a76d1c
3 changed files with 16 additions and 115 deletions
|
|
@ -1,111 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace Catalyst\APIBundle\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
|
|
||||||
{
|
|
||||||
// TODO: make api_acl and acl yaml generator have its own bundle
|
|
||||||
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 = 'api_access_keys';
|
|
||||||
|
|
||||||
// cache config
|
|
||||||
$cache_file = $this->cache_dir . '/' . $key . '.serial';
|
|
||||||
$cache = new ConfigCache($cache_file, true);
|
|
||||||
|
|
||||||
// check if cache is fresh
|
|
||||||
if (!$cache->isFresh())
|
|
||||||
{
|
|
||||||
$files = [];
|
|
||||||
$resources = [];
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// get location of api_acl.yaml
|
|
||||||
$path = $this->config_dir . '/api_acl.yaml';
|
|
||||||
|
|
||||||
$files[] = $path;
|
|
||||||
$resources[] = new FileResource($path);
|
|
||||||
|
|
||||||
// process api acl config file
|
|
||||||
$data = $this->parseACL($path, $key);
|
|
||||||
}
|
|
||||||
catch (\InvalidArgumentException $e)
|
|
||||||
{
|
|
||||||
error_log($e->getMessage());
|
|
||||||
error_log($key . ' key not found in api_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 parseACL($path, $key)
|
|
||||||
{
|
|
||||||
|
|
||||||
$parser = new YamlParser();
|
|
||||||
$config = $parser->parse(file_get_contents($path));
|
|
||||||
|
|
||||||
// check if we have access keys
|
|
||||||
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
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -15,17 +15,22 @@ class Generator
|
||||||
protected $router;
|
protected $router;
|
||||||
protected $cache_dir;
|
protected $cache_dir;
|
||||||
protected $config_dir;
|
protected $config_dir;
|
||||||
|
protected $acl_file;
|
||||||
|
protected $acl_key;
|
||||||
|
|
||||||
public function __construct(RouterInterface $router, string $cache_dir, string $config_dir)
|
public function __construct(RouterInterface $router, string $cache_dir, string $config_dir,
|
||||||
|
string $acl_file, string $acl_key)
|
||||||
{
|
{
|
||||||
$this->router = $router;
|
$this->router = $router;
|
||||||
$this->cache_dir = $cache_dir;
|
$this->cache_dir = $cache_dir;
|
||||||
$this->config_dir = $config_dir;
|
$this->config_dir = $config_dir;
|
||||||
|
$this->acl_file = $acl_file;
|
||||||
|
$this->acl_key = $acl_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getACL()
|
public function getACL()
|
||||||
{
|
{
|
||||||
$key = 'api_access_keys';
|
$key = $this->acl_key;
|
||||||
|
|
||||||
// cache config
|
// cache config
|
||||||
$cache_file = $this->cache_dir . '/' . $key . '.serial';
|
$cache_file = $this->cache_dir . '/' . $key . '.serial';
|
||||||
|
|
@ -40,7 +45,8 @@ class Generator
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// get location of api_acl.yaml
|
// get location of api_acl.yaml
|
||||||
$path = $this->config_dir . '/api_acl.yaml';
|
// $path = $this->config_dir . '/api_acl.yaml';
|
||||||
|
$path = $this->config_dir . '/' . $this->acl_file;
|
||||||
|
|
||||||
$files[] = $path;
|
$files[] = $path;
|
||||||
$resources[] = new FileResource($path);
|
$resources[] = new FileResource($path);
|
||||||
|
|
@ -51,7 +57,7 @@ class Generator
|
||||||
catch (\InvalidArgumentException $e)
|
catch (\InvalidArgumentException $e)
|
||||||
{
|
{
|
||||||
error_log($e->getMessage());
|
error_log($e->getMessage());
|
||||||
error_log($key . ' key not found in api_acl.yaml file.');
|
error_log($key . ' key not found in ' . $this->acl_file . 'file.');
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@ parameters:
|
||||||
longitude: 121.0223
|
longitude: 121.0223
|
||||||
image_upload_directory: '%kernel.project_dir%/public/uploads'
|
image_upload_directory: '%kernel.project_dir%/public/uploads'
|
||||||
job_order_refresh_interval: 300000
|
job_order_refresh_interval: 300000
|
||||||
|
api_acl_file: 'api_acl.yaml'
|
||||||
|
api_access_key: 'api_access_keys'
|
||||||
|
site_acl_file: 'acl.yaml'
|
||||||
|
site_access_key: 'access_keys'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
# default configuration for services in *this* file
|
# default configuration for services in *this* file
|
||||||
|
|
@ -104,4 +108,6 @@ services:
|
||||||
$router: "@router.default"
|
$router: "@router.default"
|
||||||
$cache_dir: "%kernel.cache_dir%"
|
$cache_dir: "%kernel.cache_dir%"
|
||||||
$config_dir: "%kernel.root_dir%/../config"
|
$config_dir: "%kernel.root_dir%/../config"
|
||||||
|
$acl_file: "%api_acl_file%"
|
||||||
|
$acl_key: "%api_access_key%"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue