From 01f57fdc37fa9690bdd1d9cb634a587e923e7c89 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 28 Mar 2019 07:03:09 +0000 Subject: [PATCH] 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 --- catalyst/api-bundle/Access/Generator.php | 9 ++ catalyst/api-bundle/Access/Voter.php | 10 ++ .../{Access => Service}/Generator.php | 4 +- .../auth-bundle/{Access => Service}/Voter.php | 4 +- config/services.yaml | 12 +- src/Access/Generator.php | 104 +----------------- src/Access/Voter.php | 37 +------ src/Controller/CAPI/BatteryController.php | 2 +- src/Controller/CAPI/VehicleController.php | 2 +- src/Controller/CAPI/WarrantyController.php | 2 +- 10 files changed, 36 insertions(+), 150 deletions(-) create mode 100644 catalyst/api-bundle/Access/Generator.php create mode 100644 catalyst/api-bundle/Access/Voter.php rename catalyst/auth-bundle/{Access => Service}/Generator.php (98%) rename catalyst/auth-bundle/{Access => Service}/Voter.php (93%) diff --git a/catalyst/api-bundle/Access/Generator.php b/catalyst/api-bundle/Access/Generator.php new file mode 100644 index 00000000..a4926a51 --- /dev/null +++ b/catalyst/api-bundle/Access/Generator.php @@ -0,0 +1,9 @@ +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 - ]; - } } diff --git a/src/Access/Voter.php b/src/Access/Voter.php index b61ea968..35f678c7 100644 --- a/src/Access/Voter.php +++ b/src/Access/Voter.php @@ -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; - } } diff --git a/src/Controller/CAPI/BatteryController.php b/src/Controller/CAPI/BatteryController.php index bbaee9ac..cff96fb9 100644 --- a/src/Controller/CAPI/BatteryController.php +++ b/src/Controller/CAPI/BatteryController.php @@ -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 { diff --git a/src/Controller/CAPI/VehicleController.php b/src/Controller/CAPI/VehicleController.php index f95c4d42..ab0ed286 100644 --- a/src/Controller/CAPI/VehicleController.php +++ b/src/Controller/CAPI/VehicleController.php @@ -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 { diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index fb88f8a4..739cd15f 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -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 {