src/Entity/Security/UserRole.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Security;
  3. use App\Entity\Abstract\Entity;
  4. use App\Entity\User;
  5. use App\Repository\Security\UserRoleRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. #[ORM\Entity(repositoryClass: UserRoleRepository::class)]
  12. class UserRole extends Entity
  13. {
  14. /*
  15. * ACCESS DEFINITIONS
  16. *
  17. * SEE security/function_definition.yaml
  18. *
  19. */
  20. #[ORM\Id]
  21. #[ORM\GeneratedValue]
  22. #[ORM\Column]
  23. private ?int $id = null;
  24. #[ORM\Column(length: 255)]
  25. #[Assert\NotBlank]
  26. private ?string $name = null;
  27. #[ORM\Column(type: Types::JSON)]
  28. #[Assert\NotBlank]
  29. private array $access = [];
  30. #[ORM\Column(length: 255, unique: true)]
  31. #[Assert\NotBlank]
  32. private ?string $identifier = null;
  33. #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'userRoles')]
  34. private Collection $users;
  35. #[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'parentUserRoles')]
  36. private Collection $defaultSubRole;
  37. #[ORM\ManyToMany(targetEntity: self::class, mappedBy: 'defaultSubRole')]
  38. private Collection $parentUserRoles;
  39. #[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'parentGroupTicketUserRoles')]
  40. #[ORM\JoinTable(name: 'user_role_user_role_group_ticket')]
  41. private Collection $groupTicketSubRoles;
  42. #[ORM\ManyToMany(targetEntity: self::class, mappedBy: 'groupTicketSubRoles')]
  43. private Collection $parentGroupTicketUserRoles;
  44. public function __construct()
  45. {
  46. $this->users = new ArrayCollection();
  47. $this->defaultSubRole = new ArrayCollection();
  48. $this->parentUserRoles = new ArrayCollection();
  49. $this->groupTicketSubRoles = new ArrayCollection();
  50. $this->parentGroupTicketUserRoles = new ArrayCollection();
  51. }
  52. public function getId(): ?int
  53. {
  54. return $this->id;
  55. }
  56. public function getName(): ?string
  57. {
  58. return $this->name;
  59. }
  60. public function setName(string $name): static
  61. {
  62. $this->name = $name;
  63. return $this;
  64. }
  65. public function getAccess(): array
  66. {
  67. return $this->access;
  68. }
  69. public function setAccess(array $access): static
  70. {
  71. $this->access = $access;
  72. return $this;
  73. }
  74. public function getIdentifier(): ?string
  75. {
  76. return $this->identifier;
  77. }
  78. public function setIdentifier(string $identifier): static
  79. {
  80. $this->identifier = $identifier;
  81. return $this;
  82. }
  83. /**
  84. * @return Collection<int, User>
  85. */
  86. public function getUsers(): Collection
  87. {
  88. return $this->users;
  89. }
  90. public function addUser(User $user): static
  91. {
  92. if (!$this->users->contains($user)) {
  93. $this->users->add($user);
  94. $user->addUserRole($this);
  95. }
  96. return $this;
  97. }
  98. public function removeUser(User $user): static
  99. {
  100. if ($this->users->removeElement($user)) {
  101. $user->removeUserRole($this);
  102. }
  103. return $this;
  104. }
  105. public function hasAccess(string $function): bool
  106. {
  107. return in_array($function, $this->access);
  108. }
  109. /**
  110. * @return Collection<int, self>
  111. */
  112. public function getDefaultSubRole(): Collection
  113. {
  114. return $this->defaultSubRole;
  115. }
  116. public function addDefaultSubRole(self $defaultSubRole): static
  117. {
  118. if (!$this->defaultSubRole->contains($defaultSubRole)) {
  119. $this->defaultSubRole->add($defaultSubRole);
  120. }
  121. return $this;
  122. }
  123. public function removeDefaultSubRole(self $defaultSubRole): static
  124. {
  125. $this->defaultSubRole->removeElement($defaultSubRole);
  126. return $this;
  127. }
  128. /**
  129. * @return Collection<int, self>
  130. */
  131. public function getParentUserRoles(): Collection
  132. {
  133. return $this->parentUserRoles;
  134. }
  135. public function addParentUserRole(self $parentUserRole): static
  136. {
  137. if (!$this->parentUserRoles->contains($parentUserRole)) {
  138. $this->parentUserRoles->add($parentUserRole);
  139. $parentUserRole->addDefaultSubRole($this);
  140. }
  141. return $this;
  142. }
  143. public function removeParentUserRole(self $parentUserRole): static
  144. {
  145. if ($this->parentUserRoles->removeElement($parentUserRole)) {
  146. $parentUserRole->removeDefaultSubRole($this);
  147. }
  148. return $this;
  149. }
  150. /**
  151. * @return Collection<int, self>
  152. */
  153. public function getGroupTicketSubRoles(): Collection
  154. {
  155. return $this->groupTicketSubRoles;
  156. }
  157. public function addGroupTicketSubRole(self $groupTicketSubRole): static
  158. {
  159. if (!$this->groupTicketSubRoles->contains($groupTicketSubRole)) {
  160. $this->groupTicketSubRoles->add($groupTicketSubRole);
  161. }
  162. return $this;
  163. }
  164. public function removeGroupTicketSubRole(self $groupTicketSubRole): static
  165. {
  166. $this->groupTicketSubRoles->removeElement($groupTicketSubRole);
  167. return $this;
  168. }
  169. /**
  170. * @return Collection<int, self>
  171. */
  172. public function getParentGroupTicketUserRoles(): Collection
  173. {
  174. return $this->parentGroupTicketUserRoles;
  175. }
  176. public function addParentGroupTicketUserRole(self $parentGroupTicketUserRole): static
  177. {
  178. if (!$this->parentGroupTicketUserRoles->contains($parentGroupTicketUserRole)) {
  179. $this->parentGroupTicketUserRoles->add($parentGroupTicketUserRole);
  180. $parentGroupTicketUserRole->addGroupTicketSubRole($this);
  181. }
  182. return $this;
  183. }
  184. public function removeParentGroupTicketUserRole(self $parentGroupTicketUserRole): static
  185. {
  186. if ($this->parentGroupTicketUserRoles->removeElement($parentGroupTicketUserRole)) {
  187. $parentGroupTicketUserRole->removeGroupTicketSubRole($this);
  188. }
  189. return $this;
  190. }
  191. }