Made the Generator and Voter classes in the Auth bundle abstract. Modify the services.yaml to point to the Voter and Generator classes that extended the base classes from the Auth bundle. #194
This commit is contained in:
parent
3862a76d1c
commit
01f57fdc37
10 changed files with 36 additions and 150 deletions
9
catalyst/api-bundle/Access/Generator.php
Normal file
9
catalyst/api-bundle/Access/Generator.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Catalyst\APIBundle\Access;
|
||||
|
||||
use Catalyst\AuthBundle\Service\Generator as BaseGenerator;
|
||||
|
||||
class Generator extends BaseGenerator
|
||||
{
|
||||
}
|
||||
10
catalyst/api-bundle/Access/Voter.php
Normal file
10
catalyst/api-bundle/Access/Voter.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Catalyst\APIBundle\Access;
|
||||
|
||||
use Catalyst\AuthBundle\Service\Voter as BaseVoter;
|
||||
|
||||
class Voter extends BaseVoter
|
||||
{
|
||||
}
|
||||
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Catalyst\AuthBundle\Access;
|
||||
namespace Catalyst\AuthBundle\Service;
|
||||
|
||||
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||
use Symfony\Component\Yaml\Parser as YamlParser;
|
||||
|
|
@ -9,7 +9,7 @@ use Symfony\Component\Config\Resource\FileResource;
|
|||
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
|
||||
class Generator
|
||||
abstract class Generator
|
||||
{
|
||||
// TODO: make api_acl and acl yaml generator have its own bundle
|
||||
protected $router;
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Catalyst\AuthBundle\Access;
|
||||
namespace Catalyst\AuthBundle\Service;
|
||||
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter as BaseVoter;
|
||||
|
||||
class Voter extends BaseVoter
|
||||
abstract class Voter extends BaseVoter
|
||||
{
|
||||
protected $acl_gen;
|
||||
|
||||
|
|
@ -8,8 +8,8 @@ parameters:
|
|||
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'
|
||||
app_acl_file: 'acl.yaml'
|
||||
app_access_key: 'access_keys'
|
||||
|
||||
services:
|
||||
# default configuration for services in *this* file
|
||||
|
|
@ -45,6 +45,8 @@ services:
|
|||
$router: "@router.default"
|
||||
$cache_dir: "%kernel.cache_dir%"
|
||||
$config_dir: "%kernel.root_dir%/../config"
|
||||
$acl_file: "%app_acl_file%"
|
||||
$acl_key: "%app_access_key%"
|
||||
|
||||
App\Access\Voter:
|
||||
arguments:
|
||||
|
|
@ -98,12 +100,12 @@ services:
|
|||
Catalyst\APIBundle\Command\TestAPICommand:
|
||||
tags: ['console.command']
|
||||
|
||||
Catalyst\AuthBundle\Access\Voter:
|
||||
Catalyst\APIBundle\Access\Voter:
|
||||
arguments:
|
||||
$acl_gen: "@Catalyst\\AuthBundle\\Access\\Generator"
|
||||
$acl_gen: "@Catalyst\\APIBundle\\Access\\Generator"
|
||||
tags: ['security.voter']
|
||||
|
||||
Catalyst\AuthBundle\Access\Generator:
|
||||
Catalyst\APIBundle\Access\Generator:
|
||||
arguments:
|
||||
$router: "@router.default"
|
||||
$cache_dir: "%kernel.cache_dir%"
|
||||
|
|
|
|||
|
|
@ -2,108 +2,8 @@
|
|||
|
||||
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 Catalyst\AuthBundle\Service\Generator as BaseGenerator;
|
||||
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
|
||||
class Generator
|
||||
class Generator extends BaseGenerator
|
||||
{
|
||||
// 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 = '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->parseACL($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 parseACL($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
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,43 +2,8 @@
|
|||
|
||||
namespace App\Access;
|
||||
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter as BaseVoter;
|
||||
use Catalyst\AuthBundle\Service\Voter as BaseVoter;
|
||||
|
||||
class Voter extends BaseVoter
|
||||
{
|
||||
protected $acl_gen;
|
||||
|
||||
public function __construct(Generator $acl_gen)
|
||||
{
|
||||
$this->acl_gen = $acl_gen;
|
||||
}
|
||||
|
||||
protected function supports($attribute, $subject)
|
||||
{
|
||||
$acl_data = $this->acl_gen->getACL();
|
||||
|
||||
// check if the attribute is in our acl key index
|
||||
if (isset($acl_data['index'][$attribute]))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
|
||||
{
|
||||
$user = $token->getUser();
|
||||
|
||||
// check if any of the user's roles have access
|
||||
$roles = $user->getRoleObjects();
|
||||
|
||||
foreach ($roles as $role)
|
||||
{
|
||||
// NOTE: ideally, we separate acl from the role object, but this will do for now
|
||||
if ($role->hasACLAccess($attribute))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ use App\Entity\SAPBattery;
|
|||
use App\Entity\SAPBatterySize;
|
||||
use App\Entity\SAPBatteryBrand;
|
||||
|
||||
use Catalyst\AuthBundle\Access\Generator as ACLGenerator;
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class BatteryController extends APIController
|
||||
{
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use Catalyst\APIBundle\Response\APIResponse;
|
|||
use App\Entity\Vehicle;
|
||||
use App\Entity\VehicleManufacturer;
|
||||
|
||||
use Catalyst\AuthBundle\Access\Generator as ACLGenerator;
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class VehicleController extends APIController
|
||||
{
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ use App\Ramcar\WarrantyClass;
|
|||
use App\Ramcar\WarrantyStatus;
|
||||
use DateTime;
|
||||
|
||||
use Catalyst\AuthBundle\Access\Generator as ACLGenerator;
|
||||
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||
|
||||
class WarrantyController extends APIController
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue