diff --git a/authentication/auth-bundle/AuthBundle.php b/authentication/auth-bundle/AuthBundle.php deleted file mode 100644 index 022e5f0b..00000000 --- a/authentication/auth-bundle/AuthBundle.php +++ /dev/null @@ -1,7 +0,0 @@ -users = new ArrayCollection(); - $this->acl_attributes = []; - } - - public function setID($id) - { - // example ROLE_SUPER_ADMIN, ROLE_CASHIER, etc - $this->id = $id; - return $this; - } - - public function getID() - { - return $this->id; - } - - public function setName($name) - { - $this->name = $name; - return $this; - } - - public function getName() - { - return $this->name; - } - - public function getUsers() - { - return $this->users; - } - - public function getUsersCount() - { - return $this->users->count(); - } - - public function isSuperAdmin() - { - if ($this->id == self::SUPER_ADMIN) - return true; - - return false; - } - - // TODO: shift out ACL stuff to its own class - public function clearACLAttributes() - { - $this->acl_attributes = []; - return $this; - } - - public function getACLAttributes() - { - return $this->acl_attributes; - } - - public function addACLAccess($attribute) - { - $this->acl_attributes[$attribute] = true; - return $this; - } - - public function hasACLAccess($attribute) - { - // if it's super admin, they always have access - if ($this->isSuperAdmin()) - return true; - - // check ACL attributes - if (isset($this->acl_attributes[$attribute]) && $this->acl_attributes[$attribute]) - return true; - - return false; - } -} diff --git a/authentication/auth-bundle/Entity/APIUser.php b/authentication/auth-bundle/Entity/APIUser.php deleted file mode 100644 index 1cfc3291..00000000 --- a/authentication/auth-bundle/Entity/APIUser.php +++ /dev/null @@ -1,156 +0,0 @@ -setAPIKey($this->generateAPIKey()) - ->setSecretKey($this->generateSecretKey()); - - // set date created - $this->date_create = new DateTime(); - - $this->roles = new ArrayCollection(); - } - - public function getID() - { - return $this->id; - } - - public function setAPIKey($api_key) - { - $this->api_key = $api_key; - return $this; - } - - public function getAPIKey() - { - return $this->api_key; - } - - public function setSecretKey($key) - { - $this->secret_key = $key; - return $this; - } - - public function getSecretKey() - { - return $this->secret_key; - } - - public function setName($name) - { - $this->name = $name; - return $this; - } - - public function getName() - { - return $this->name; - } - - public function getRoles() - { - $str_roles = []; - foreach ($this->roles as $role) - $str_roles[] = $role->getID(); - - return $str_roles; - } - - public function getRoleObjects() - { - return $this->roles; - } - - public function getDateCreate() - { - return $this->date_create; - } - - public function getPassword() - { - // we don't need this for API - return 'notneeded'; - } - - public function getSalt() - { - return null; - } - - public function getUsername() - { - // since it's an api, the api key IS the username - return $this->api_key; - } - - public function eraseCredentials() - { - return; - } - - public function generateAPIKey() - { - return $this->generateKey('api'); - } - - public function generateSecretKey() - { - return $this->generateKey('secret'); - } - - protected function generateKey($prefix = '') - { - return md5(uniqid($prefix, true)); - } -} - diff --git a/authentication/auth-bundle/Entity/User.php b/authentication/auth-bundle/Entity/User.php deleted file mode 100644 index 88cb8cc9..00000000 --- a/authentication/auth-bundle/Entity/User.php +++ /dev/null @@ -1,337 +0,0 @@ -roles = new ArrayCollection(); - $this->hubs = new ArrayCollection(); - $this->job_orders_created = new ArrayCollection(); - $this->job_orders_assigned = new ArrayCollection(); - $this->tickets = new ArrayCollection(); - $this->enabled = true; - } - - public function getID() - { - return $this->id; - } - - public function setUsername($username) - { - $this->username = $username; - return $this; - } - - public function getUsername() - { - return $this->username; - } - - public function setPassword($password) - { - $this->password = $password; - return $this; - } - - public function getPassword() - { - return $this->password; - } - - public function setSalt($salt) - { - // do nothing - return $this; - } - - public function getSalt() - { - return null; - } - - public function addRole(Role $role) - { - $this->roles->add($role); - return $this; - } - - public function clearRoles() - { - $this->roles->clear(); - return $this; - } - - public function getRoles() - { - // has to return set of strings because symfony is trying to move away from role objects - $str_roles = []; - foreach ($this->roles as $role) - $str_roles[] = $role->getID(); - - return $str_roles; - } - - public function getRoleObjects() - { - return $this->roles; - } - - public function addHub(Hub $hub) - { - $this->hubs->add($hub); - return $this; - } - - public function clearHubs() - { - $this->hubs->clear(); - return $this; - } - - public function getHubs() - { - $str_hubs = []; - foreach ($this->hubs as $hub) - $str_hubs[] = $hub->getID(); - - return $str_hubs; - } - - public function getHubObjects() - { - return $this->hubs; - } - - public function eraseCredentials() - { - return $this; - } - - public function isAccountNonExpired() - { - return true; - } - - public function isAccountNonLocked() - { - return true; - } - - public function isCredentialsNonExpired() - { - return true; - } - - public function setEnabled($enabled = true) - { - $this->enabled = $enabled; - return $this; - } - - public function isEnabled() - { - return $this->enabled; - } - - public function serialize() - { - return serialize([ - $this->id, - $this->username, - $this->password, - $this->enabled, - ]); - } - - public function unserialize($serial) - { - list ( - $this->id, - $this->username, - $this->password, - $this->enabled, - ) = unserialize($serial); - } - - public function setFirstName($name) - { - $this->first_name = $name; - return $this; - } - - public function getFirstName() - { - return $this->first_name; - } - - public function setLastName($name) - { - $this->last_name = $name; - return $this; - } - - public function getLastName() - { - return $this->last_name; - } - - public function getFullName() - { - return $this->first_name . ' ' . $this->last_name; - } - - public function setContactNumber($num) - { - $this->contact_num = $num; - return $this; - } - - public function getContactNumber() - { - return $this->contact_num; - } - - public function setEmail($email = null) - { - $this->email = $email; - return $this; - } - - public function getEmail() - { - return $this->email; - } - - public function isSuperAdmin() - { - foreach ($this->roles as $role) - { - if ($role->isSuperAdmin()) - return true; - } - - return false; - } - - public function getJobOrdersCreated() - { - return $this->job_orders_created; - } - - public function getJobOrdersAssigned() - { - return $this->job_orders_assigned; - } - - public function getTickets() - { - return $this->tickets; - } - - public function getInvoices() - { - return $this->invoices; - } -} diff --git a/catalyst/api-bundle/Entity/Role.php b/catalyst/api-bundle/Entity/Role.php index 534638b3..c2bfbab3 100644 --- a/catalyst/api-bundle/Entity/Role.php +++ b/catalyst/api-bundle/Entity/Role.php @@ -2,6 +2,8 @@ namespace Catalyst\APIBundle\Entity; +use Catalyst\AuthBundle\Entity\Role as BaseRole; + use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Validator\Constraints as Assert; @@ -13,7 +15,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; * @UniqueEntity("id") * @UniqueEntity("name") */ -class Role +class Role extends BaseRole { const SUPER_ADMIN = 'ROLE_SUPER_ADMIN'; diff --git a/catalyst/api-bundle/Entity/User.php b/catalyst/api-bundle/Entity/User.php index 39a5731d..712b5d5d 100644 --- a/catalyst/api-bundle/Entity/User.php +++ b/catalyst/api-bundle/Entity/User.php @@ -2,6 +2,8 @@ namespace Catalyst\APIBundle\Entity; +use Catalyst\AuthBundle\Entity\User as BaseUser; + use Symfony\Component\Security\Core\User\UserInterface; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\JoinColumn; @@ -12,7 +14,7 @@ use DateTime; * @ORM\Entity * @ORM\Table(name="api_user") */ -class User implements UserInterface +class User extends BaseUser implements UserInterface { // api key /** @@ -97,19 +99,19 @@ class User implements UserInterface return $this->name; } - public function getRoles() - { - $str_roles = []; - foreach ($this->roles as $role) - $str_roles[] = $role->getID(); + //public function getRoles() + //{ + // $str_roles = []; + // foreach ($this->roles as $role) + // $str_roles[] = $role->getID(); - return $str_roles; - } + // return $str_roles; + //} - public function getRoleObjects() - { - return $this->roles; - } + //public function getRoleObjects() + //{ + // return $this->roles; + //} public function getDateCreate() { diff --git a/catalyst/auth-bundle/Access/Generator.php b/catalyst/auth-bundle/Access/Generator.php new file mode 100644 index 00000000..f52c75b1 --- /dev/null +++ b/catalyst/auth-bundle/Access/Generator.php @@ -0,0 +1,111 @@ +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 + ]; + } +} diff --git a/catalyst/api-bundle/Access/Voter.php b/catalyst/auth-bundle/Access/Voter.php similarity index 96% rename from catalyst/api-bundle/Access/Voter.php rename to catalyst/auth-bundle/Access/Voter.php index 5a5776e0..833101ee 100644 --- a/catalyst/api-bundle/Access/Voter.php +++ b/catalyst/auth-bundle/Access/Voter.php @@ -1,6 +1,6 @@ roles = new ArrayCollection(); + } + + public function getRoles() + { + $str_roles = []; + foreach ($this->roles as $role) + $str_roles[] = $role->getID(); + + return $str_roles; + } + + public function getRoleObjects() + { + return $this->roles; + } + +} diff --git a/composer.json b/composer.json index e4a21704..9c7e7ef4 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,8 @@ "psr-4": { "App\\": "src/", "Catalyst\\APIBundle\\": "catalyst/api-bundle/", - "RamcarBattery\\APIBundle\\": "ramcar-batery/api-bundle/" + "RamcarBattery\\APIBundle\\": "ramcar-batery/api-bundle/", + "Catalyst\\AuthBundle\\": "catalyst/auth-bundle/" } }, "autoload-dev": { diff --git a/config/bundles.php b/config/bundles.php index d05a5a86..45c4b8ab 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -14,5 +14,5 @@ return [ Catalyst\APIBundle\CatalystAPIBundle::class => ['all' => true], // DataDog\AuditBundle\DataDogAuditBundle::class => ['all' => true], - Authentication\AuthBundle\Authbundle::class => ['all' => true], + Catalyst\AuthBundle\CatalystAuthBundle::class => ['all' => true], ]; diff --git a/config/services.yaml b/config/services.yaml index 1eb74949..aae29801 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -91,15 +91,15 @@ services: Catalyst\APIBundle\Command\TestCommand: tags: ['console.command'] - Catalyst\APIBundle\Access\Voter: - arguments: - $acl_gen: "@Catalyst\\APIBundle\\Access\\Generator" - tags: ['security.voter'] - Catalyst\APIBundle\Command\TestAPICommand: tags: ['console.command'] - Catalyst\APIBundle\Access\Generator: + Catalyst\AuthBundle\Access\Voter: + arguments: + $acl_gen: "@Catalyst\\AuthBundle\\Access\\Generator" + tags: ['security.voter'] + + Catalyst\AuthBundle\Access\Generator: arguments: $router: "@router.default" $cache_dir: "%kernel.cache_dir%" diff --git a/src/Controller/CAPI/BatteryController.php b/src/Controller/CAPI/BatteryController.php index cff96fb9..bbaee9ac 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\APIBundle\Access\Generator as ACLGenerator; +use Catalyst\AuthBundle\Access\Generator as ACLGenerator; class BatteryController extends APIController { diff --git a/src/Controller/CAPI/VehicleController.php b/src/Controller/CAPI/VehicleController.php index ab0ed286..f95c4d42 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\APIBundle\Access\Generator as ACLGenerator; +use Catalyst\AuthBundle\Access\Generator as ACLGenerator; class VehicleController extends APIController { diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index 739cd15f..fb88f8a4 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\APIBundle\Access\Generator as ACLGenerator; +use Catalyst\AuthBundle\Access\Generator as ACLGenerator; class WarrantyController extends APIController { diff --git a/src/Entity/Role.php b/src/Entity/Role.php index 168b127d..21327eb6 100644 --- a/src/Entity/Role.php +++ b/src/Entity/Role.php @@ -2,6 +2,8 @@ namespace App\Entity; +use Catalyst\AuthBundle\Entity\Role as BaseRole; + use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Validator\Constraints as Assert; @@ -13,7 +15,7 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; * @UniqueEntity("id") * @UniqueEntity("name") */ -class Role +class Role extends BaseRole { const SUPER_ADMIN = 'ROLE_SUPER_ADMIN'; diff --git a/src/Entity/User.php b/src/Entity/User.php index 88cb8cc9..bc106433 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -2,6 +2,8 @@ namespace App\Entity; +use Catalyst\AuthBundle\Entity\User as BaseUser; + use Symfony\Component\Security\Core\User\AdvancedUserInterface; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; @@ -15,7 +17,7 @@ use Serializable; * @UniqueEntity("username") * @UniqueEntity("email") */ -class User implements AdvancedUserInterface, Serializable +class User extends BaseUser implements AdvancedUserInterface, Serializable { /** * @ORM\Id @@ -163,20 +165,20 @@ class User implements AdvancedUserInterface, Serializable return $this; } - public function getRoles() - { - // has to return set of strings because symfony is trying to move away from role objects - $str_roles = []; - foreach ($this->roles as $role) - $str_roles[] = $role->getID(); + //public function getRoles() + //{ + // // has to return set of strings because symfony is trying to move away from role objects + // $str_roles = []; + // foreach ($this->roles as $role) + // $str_roles[] = $role->getID(); - return $str_roles; - } + // return $str_roles; + //} - public function getRoleObjects() - { - return $this->roles; - } + //public function getRoleObjects() + //{ + // return $this->roles; + //} public function addHub(Hub $hub) {