From 57f1eb138673c2c84f2cbbf87d15247d5f71598e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 29 Mar 2019 06:35:37 +0000 Subject: [PATCH] Remove duplicate code from classes that extend Role. Rename UserFixtures to RoleFixtures. Create APIRoleFixtures file. #194 --- .../DataFixtures/APIRoleFixtures.php | 20 ++++ catalyst/api-bundle/Entity/Role.php | 99 ------------------- catalyst/auth-bundle/Entity/Role.php | 18 ++++ .../{UserFixtures.php => RoleFixtures.php} | 2 +- src/Entity/Role.php | 99 ------------------- 5 files changed, 39 insertions(+), 199 deletions(-) create mode 100644 catalyst/api-bundle/DataFixtures/APIRoleFixtures.php rename src/DataFixtures/{UserFixtures.php => RoleFixtures.php} (92%) diff --git a/catalyst/api-bundle/DataFixtures/APIRoleFixtures.php b/catalyst/api-bundle/DataFixtures/APIRoleFixtures.php new file mode 100644 index 00000000..00b4c3e0 --- /dev/null +++ b/catalyst/api-bundle/DataFixtures/APIRoleFixtures.php @@ -0,0 +1,20 @@ +setID(Role::SUPER_ADMIN) + ->setName('Super Administrator'); + $em->persist($role); + $em->flush(); + } +} diff --git a/catalyst/api-bundle/Entity/Role.php b/catalyst/api-bundle/Entity/Role.php index c2bfbab3..ed2607b1 100644 --- a/catalyst/api-bundle/Entity/Role.php +++ b/catalyst/api-bundle/Entity/Role.php @@ -6,7 +6,6 @@ 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; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; /** @@ -17,106 +16,8 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; */ class Role extends BaseRole { - const SUPER_ADMIN = 'ROLE_SUPER_ADMIN'; - - /** - * @ORM\Id - * @ORM\Column(type="string", length=80) - * @Assert\NotBlank() - */ - protected $id; - - /** - * @ORM\Column(type="string", length=80) - * @Assert\NotBlank() - */ - protected $name; - /** * @ORM\ManyToMany(targetEntity="User", mappedBy="roles", fetch="EXTRA_LAZY") */ protected $users; - - /** - * @ORM\Column(type="json_array") - */ - protected $acl_attributes; - - public function __construct() - { - $this->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/catalyst/auth-bundle/Entity/Role.php b/catalyst/auth-bundle/Entity/Role.php index 3be9ff6b..8cb126eb 100644 --- a/catalyst/auth-bundle/Entity/Role.php +++ b/catalyst/auth-bundle/Entity/Role.php @@ -2,15 +2,33 @@ namespace Catalyst\AuthBundle\Entity; +use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; +use Symfony\Component\Validator\Constraints as Assert; abstract class Role { const SUPER_ADMIN = 'ROLE_SUPER_ADMIN'; + /** + * @ORM\Id + * @ORM\Column(type="string", length=80) + * @Assert\NotBlank() + */ protected $id; + + /** + * @ORM\Column(type="string", length=80) + * @Assert\NotBlank() + */ protected $name; + + // annotation should be in the extending class protected $users; + + /** + * @ORM\Column(type="json_array") + */ protected $acl_attributes; public function __construct() diff --git a/src/DataFixtures/UserFixtures.php b/src/DataFixtures/RoleFixtures.php similarity index 92% rename from src/DataFixtures/UserFixtures.php rename to src/DataFixtures/RoleFixtures.php index 7ac1c849..8e88499b 100644 --- a/src/DataFixtures/UserFixtures.php +++ b/src/DataFixtures/RoleFixtures.php @@ -6,7 +6,7 @@ use App\Entity\Role; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Common\Persistence\ObjectManager; -class UserFixtures extends Fixture +class RoleFixtures extends Fixture { public function load(ObjectManager $em) { diff --git a/src/Entity/Role.php b/src/Entity/Role.php index 21327eb6..1da5efe4 100644 --- a/src/Entity/Role.php +++ b/src/Entity/Role.php @@ -6,7 +6,6 @@ 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; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; /** @@ -17,106 +16,8 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; */ class Role extends BaseRole { - const SUPER_ADMIN = 'ROLE_SUPER_ADMIN'; - - /** - * @ORM\Id - * @ORM\Column(type="string", length=80) - * @Assert\NotBlank() - */ - protected $id; - - /** - * @ORM\Column(type="string", length=80) - * @Assert\NotBlank() - */ - protected $name; - /** * @ORM\ManyToMany(targetEntity="User", mappedBy="roles", fetch="EXTRA_LAZY") */ protected $users; - - /** - * @ORM\Column(type="json_array") - */ - protected $acl_attributes; - - public function __construct() - { - $this->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; - } }