resq/catalyst/api-bundle/Entity/Role.php
2019-03-19 04:15:03 -04:00

120 lines
2.3 KiB
PHP

<?php
namespace Catalyst\APIBundle\Entity;
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;
/**
* @ORM\Entity
* @ORM\Table(name="api_role")
* @UniqueEntity("id")
* @UniqueEntity("name")
*/
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;
/**
* @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;
}
}