<?php
namespace Platform\SecurityBundle\Entity\Access;
use App\Entity\System\School;
use Cms\ContainerBundle\Entity\Container;
use Cms\TenantBundle\Entity\TenantedEntity;
use Doctrine\ORM\Mapping as ORM;
use Platform\SecurityBundle\Entity\Access\Role\CmsRole;
use Platform\SecurityBundle\Entity\Access\Role\NotificationRole;
use Products\NotificationsBundle\Entity\AbstractList;
/**
* Class RoleAssociation
* @package Platform\SecurityBundle\Entity\Access
*
* @ORM\Entity(repositoryClass = "Platform\SecurityBundle\Doctrine\Access\RoleAssociationRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name = "_discr", type = "string")
* @ORM\DiscriminatorMap({
* "account" = "Platform\SecurityBundle\Entity\Access\RoleAssociation\AccountRoleAssociation",
* "group" = "Platform\SecurityBundle\Entity\Access\RoleAssociation\GroupRoleAssociation"
* })
* @ORM\Table(name = "cms__security__access__role_association")
*/
abstract class RoleAssociation extends TenantedEntity
{
public const INHERITANCES = array(
self::INHERITANCES__ME_ONLY,
//self::INHERITANCES__IMMEDIATE_DESCENDANTS_ONLY,
self::INHERITANCES__ALL_DESCENDANTS_ONLY,
//self::INHERITANCES__ME_AND_IMMEDIATE_DESCENDANTS,
self::INHERITANCES__ME_AND_ALL_DESCENDENTS,
);
public const INHERITANCES_CHOICES = array(
'Single department' => self::INHERITANCES__ME_ONLY,
//'Immediate sub-departments only' => self::INHERITANCES__IMMEDIATE_DESCENDANTS_ONLY,
'All sub-departments only' => self::INHERITANCES__ALL_DESCENDANTS_ONLY,
//'Single department and immediate sub-departments' => self::INHERITANCES__ME_AND_IMMEDIATE_DESCENDANTS,
'Single department and all sub-departments' => self::INHERITANCES__ME_AND_ALL_DESCENDENTS,
);
public const INHERITANCES__ME_ONLY = null;
//const INHERITANCES__IMMEDIATE_DESCENDANTS_ONLY = 'immediate';
public const INHERITANCES__ALL_DESCENDANTS_ONLY = 'all';
//const INHERITANCES__ME_AND_IMMEDIATE_DESCENDANTS = '+immediate';
public const INHERITANCES__ME_AND_ALL_DESCENDENTS = '+all';
/**
* @var Role|null
*
* @ORM\ManyToOne(targetEntity = "Role", inversedBy = "associations")
* @ORM\JoinColumn(name = "role", referencedColumnName = "id", onDelete = "CASCADE")
*/
protected ?Role $role = null;
/**
* @var Container|null
*
* @ORM\ManyToOne(targetEntity = "Cms\ContainerBundle\Entity\Container")
* @ORM\JoinColumn(name = "container", referencedColumnName = "id", onDelete = "CASCADE")
*/
protected ?Container $container = null;
/**
* @var string|null
*
* @ORM\Column(
* type = "string",
* nullable = true,
* )
*/
protected ?string $inheritance = self::INHERITANCES__ME_ONLY;
/**
* @var School|null
*
* @ORM\ManyToOne(targetEntity = "App\Entity\System\School")
* @ORM\JoinColumn(name = "school", referencedColumnName = "id", onDelete = "CASCADE")
*/
protected ?School $school = null;
/**
* @var AbstractList|null
*
* @ORM\ManyToOne(targetEntity = "Products\NotificationsBundle\Entity\AbstractList")
* @ORM\JoinColumn(name = "list", referencedColumnName = "id", onDelete = "CASCADE")
*/
protected ?AbstractList $list = null;
/**
* @return Role|null
*/
public function getRole(): ?Role
{
return $this->role;
}
/**
* @return Container|null
*/
public function getContainer(): ?Container
{
return $this->container;
}
/**
* @return string|null
*/
public function getInheritance(): ?string
{
return $this->inheritance;
}
/**
* @param Role|null $role
* @return $this
*/
public function setRole(?Role $role): self
{
if ($role instanceof CmsRole && ($this->getSchool() || $this->getList())) {
throw new \LogicException('Cannot set a website role to an association using a notifications context.');
}
if ($role instanceof NotificationRole && $this->getContainer()) {
throw new \LogicException('Cannot set a notifications role to an association using a website context.');
}
$this->role = $role;
return $this;
}
/**
* @param Container|null $department
* @return $this
*/
public function setContainer(?Container $department): self
{
if ($department && ($this->getList() || $this->getSchool())) {
throw new \LogicException('Cannot assign a department to an association when a list or school has already been set.');
}
if ($department && $this->getRole() instanceof NotificationRole) {
throw new \LogicException('Cannot assign a department to an association using a notifications role.');
}
$this->container = $department;
return $this;
}
/**
* @param string|null $inheritance
* @return $this
*/
public function setInheritance(?string $inheritance): self
{
if ( ! in_array($inheritance, self::INHERITANCES, true)) {
throw new \LogicException();
}
$this->inheritance = $inheritance;
return $this;
}
/**
* @return School|null
*/
public function getSchool(): ?School
{
return $this->school;
}
/**
* @param School|null $school
* @return self
*/
public function setSchool(?School $school): self
{
if ($school && ($this->getContainer() || $this->getList())) {
throw new \LogicException('Cannot assign a department to an association when a department or list has already been set.');
}
if ($school && $this->getRole() instanceof CmsRole) {
throw new \LogicException('Cannot assign a school to an association using a website role.');
}
$this->school = $school;
return $this;
}
/**
* @return AbstractList|null
*/
public function getList(): ?AbstractList
{
return $this->list;
}
/**
* @param AbstractList|null $list
* @return self
*/
public function setList(?AbstractList $list): self
{
if ($list && ($this->getContainer() || $this->getSchool())) {
throw new \LogicException('Cannot assign a list when a department or school is already set.');
}
if ($list && $this->getRole() instanceof CmsRole) {
throw new \LogicException('Cannot assign a list to an association using a website role.');
}
$this->list = $list;
return $this;
}
}