Remove duplicate code from classes that extend Role. Rename UserFixtures to RoleFixtures. Create APIRoleFixtures file. #194
This commit is contained in:
parent
01f57fdc37
commit
57f1eb1386
5 changed files with 39 additions and 199 deletions
20
catalyst/api-bundle/DataFixtures/APIRoleFixtures.php
Normal file
20
catalyst/api-bundle/DataFixtures/APIRoleFixtures.php
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Catalyst\APIBundle\DataFixtures;
|
||||||
|
|
||||||
|
use Catalyst\APIBundle\Entity\Role;
|
||||||
|
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||||
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
|
class APIRoleFixtures extends Fixture
|
||||||
|
{
|
||||||
|
public function load(ObjectManager $em)
|
||||||
|
{
|
||||||
|
// setup super user account
|
||||||
|
$role = new Role();
|
||||||
|
$role->setID(Role::SUPER_ADMIN)
|
||||||
|
->setName('Super Administrator');
|
||||||
|
$em->persist($role);
|
||||||
|
$em->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,6 @@ use Catalyst\AuthBundle\Entity\Role as BaseRole;
|
||||||
|
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
|
||||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -17,106 +16,8 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
*/
|
*/
|
||||||
class Role extends BaseRole
|
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")
|
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles", fetch="EXTRA_LAZY")
|
||||||
*/
|
*/
|
||||||
protected $users;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,33 @@
|
||||||
|
|
||||||
namespace Catalyst\AuthBundle\Entity;
|
namespace Catalyst\AuthBundle\Entity;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
use Symfony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
abstract class Role
|
abstract class Role
|
||||||
{
|
{
|
||||||
const SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
|
const SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Id
|
||||||
|
* @ORM\Column(type="string", length=80)
|
||||||
|
* @Assert\NotBlank()
|
||||||
|
*/
|
||||||
protected $id;
|
protected $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="string", length=80)
|
||||||
|
* @Assert\NotBlank()
|
||||||
|
*/
|
||||||
protected $name;
|
protected $name;
|
||||||
|
|
||||||
|
// annotation should be in the extending class
|
||||||
protected $users;
|
protected $users;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="json_array")
|
||||||
|
*/
|
||||||
protected $acl_attributes;
|
protected $acl_attributes;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use App\Entity\Role;
|
||||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||||
use Doctrine\Common\Persistence\ObjectManager;
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
class UserFixtures extends Fixture
|
class RoleFixtures extends Fixture
|
||||||
{
|
{
|
||||||
public function load(ObjectManager $em)
|
public function load(ObjectManager $em)
|
||||||
{
|
{
|
||||||
|
|
@ -6,7 +6,6 @@ use Catalyst\AuthBundle\Entity\Role as BaseRole;
|
||||||
|
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Symfony\Component\Validator\Constraints as Assert;
|
|
||||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -17,106 +16,8 @@ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||||
*/
|
*/
|
||||||
class Role extends BaseRole
|
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")
|
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles", fetch="EXTRA_LAZY")
|
||||||
*/
|
*/
|
||||||
protected $users;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue