<?php
namespace App\Entity\Security;
use App\Entity\Abstract\Entity;
use App\Entity\User;
use App\Repository\Security\UserRoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: UserRoleRepository::class)]
class UserRole extends Entity
{
/*
* ACCESS DEFINITIONS
*
* SEE security/function_definition.yaml
*
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private ?string $name = null;
#[ORM\Column(type: Types::JSON)]
#[Assert\NotBlank]
private array $access = [];
#[ORM\Column(length: 255, unique: true)]
#[Assert\NotBlank]
private ?string $identifier = null;
#[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'userRoles')]
private Collection $users;
#[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'parentUserRoles')]
private Collection $defaultSubRole;
#[ORM\ManyToMany(targetEntity: self::class, mappedBy: 'defaultSubRole')]
private Collection $parentUserRoles;
#[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'parentGroupTicketUserRoles')]
#[ORM\JoinTable(name: 'user_role_user_role_group_ticket')]
private Collection $groupTicketSubRoles;
#[ORM\ManyToMany(targetEntity: self::class, mappedBy: 'groupTicketSubRoles')]
private Collection $parentGroupTicketUserRoles;
public function __construct()
{
$this->users = new ArrayCollection();
$this->defaultSubRole = new ArrayCollection();
$this->parentUserRoles = new ArrayCollection();
$this->groupTicketSubRoles = new ArrayCollection();
$this->parentGroupTicketUserRoles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getAccess(): array
{
return $this->access;
}
public function setAccess(array $access): static
{
$this->access = $access;
return $this;
}
public function getIdentifier(): ?string
{
return $this->identifier;
}
public function setIdentifier(string $identifier): static
{
$this->identifier = $identifier;
return $this;
}
/**
* @return Collection<int, User>
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): static
{
if (!$this->users->contains($user)) {
$this->users->add($user);
$user->addUserRole($this);
}
return $this;
}
public function removeUser(User $user): static
{
if ($this->users->removeElement($user)) {
$user->removeUserRole($this);
}
return $this;
}
public function hasAccess(string $function): bool
{
return in_array($function, $this->access);
}
/**
* @return Collection<int, self>
*/
public function getDefaultSubRole(): Collection
{
return $this->defaultSubRole;
}
public function addDefaultSubRole(self $defaultSubRole): static
{
if (!$this->defaultSubRole->contains($defaultSubRole)) {
$this->defaultSubRole->add($defaultSubRole);
}
return $this;
}
public function removeDefaultSubRole(self $defaultSubRole): static
{
$this->defaultSubRole->removeElement($defaultSubRole);
return $this;
}
/**
* @return Collection<int, self>
*/
public function getParentUserRoles(): Collection
{
return $this->parentUserRoles;
}
public function addParentUserRole(self $parentUserRole): static
{
if (!$this->parentUserRoles->contains($parentUserRole)) {
$this->parentUserRoles->add($parentUserRole);
$parentUserRole->addDefaultSubRole($this);
}
return $this;
}
public function removeParentUserRole(self $parentUserRole): static
{
if ($this->parentUserRoles->removeElement($parentUserRole)) {
$parentUserRole->removeDefaultSubRole($this);
}
return $this;
}
/**
* @return Collection<int, self>
*/
public function getGroupTicketSubRoles(): Collection
{
return $this->groupTicketSubRoles;
}
public function addGroupTicketSubRole(self $groupTicketSubRole): static
{
if (!$this->groupTicketSubRoles->contains($groupTicketSubRole)) {
$this->groupTicketSubRoles->add($groupTicketSubRole);
}
return $this;
}
public function removeGroupTicketSubRole(self $groupTicketSubRole): static
{
$this->groupTicketSubRoles->removeElement($groupTicketSubRole);
return $this;
}
/**
* @return Collection<int, self>
*/
public function getParentGroupTicketUserRoles(): Collection
{
return $this->parentGroupTicketUserRoles;
}
public function addParentGroupTicketUserRole(self $parentGroupTicketUserRole): static
{
if (!$this->parentGroupTicketUserRoles->contains($parentGroupTicketUserRole)) {
$this->parentGroupTicketUserRoles->add($parentGroupTicketUserRole);
$parentGroupTicketUserRole->addGroupTicketSubRole($this);
}
return $this;
}
public function removeParentGroupTicketUserRole(self $parentGroupTicketUserRole): static
{
if ($this->parentGroupTicketUserRoles->removeElement($parentGroupTicketUserRole)) {
$parentGroupTicketUserRole->removeGroupTicketSubRole($this);
}
return $this;
}
}