<?phpnamespace App\Entity;use App\Repository\GroupUserRepository;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;/** * @ORM\Entity(repositoryClass=GroupUserRepository::class) */class GroupUser{ /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=255) */ private $name; /** * @ORM\Column(type="string", length=255) */ private $code; /** * @ORM\ManyToMany(targetEntity=Right::class, mappedBy="groups") */ private $rights; /** * @ORM\OneToMany(targetEntity=User::class, mappedBy="groupUser") */ private $users; /** * @ORM\ManyToMany(targetEntity=User::class, mappedBy="groupUsers") */ private $usersMultiGroups; public function __construct() { $this->rights = new ArrayCollection(); $this->users = new ArrayCollection(); $this->usersMultiGroups = new ArrayCollection(); } private function ensureCollectionsInitialized(): void { if ($this->rights === null) { $this->rights = new ArrayCollection(); } if ($this->users === null) { $this->users = new ArrayCollection(); } if ($this->usersMultiGroups === null) { $this->usersMultiGroups = new ArrayCollection(); } } public function getId(): ?int { return $this->id; } public function getName(): ?string { return $this->name; } public function setName(string $name): self { $this->name = $name; return $this; } public function getCode(): ?string { return $this->code; } public function setCode(string $code): self { $this->code = $code; return $this; } /** * @return Collection|Right[] */ public function getRights(): Collection { $this->ensureCollectionsInitialized(); return $this->rights; } public function addRight(Right $right): self { $this->ensureCollectionsInitialized(); if( !$this->rights->contains($right)) { $this->rights[] = $right; $right->addGroup($this); } return $this; } public function removeRight(Right $right): self { $this->ensureCollectionsInitialized(); if( $this->rights->removeElement($right)) { $right->removeGroup($this); } return $this; } /** * @return Collection|User[] */ public function getUsers(): Collection { $this->ensureCollectionsInitialized(); return $this->users; } public function addUser(User $user): self { $this->ensureCollectionsInitialized(); if( !$this->users->contains($user)) { $this->users[] = $user; $user->setGroupUser($this); } return $this; } public function removeUser(User $user): self { $this->ensureCollectionsInitialized(); if( $this->users->removeElement($user)) { // set the owning side to null (unless already changed) if( $user->getGroupUser() === $this) { $user->setGroupUser(null); } $user->removeGroupUser($this); } return $this; } public function setRights(iterable $rights): self { $this->ensureCollectionsInitialized(); // On vide d'abord les droits existants $this->rights = new ArrayCollection(); // Puis on ajoute chaque droit individuellement foreach ($rights as $right) { $this->addRight($right); } return $this; } /** * @return Collection|User[] */ public function getUsersMultiGroups(): Collection { $this->ensureCollectionsInitialized(); return $this->usersMultiGroups; } public function addUsersMultiGroup(User $user): self { $this->ensureCollectionsInitialized(); if (!$this->usersMultiGroups->contains($user)) { $this->usersMultiGroups[] = $user; } return $this; } public function removeUsersMultiGroup(User $user): self { $this->ensureCollectionsInitialized(); $this->usersMultiGroups->removeElement($user); return $this; } /** * @return User[] */ public function getEffectiveUsers(): array { $this->ensureCollectionsInitialized(); $users = []; $seen = []; $append = static function (?User $user) use (&$users, &$seen): void { if (!$user) { return; } $key = $user->getId() !== null ? 'id:' . $user->getId() : 'obj:' . spl_object_hash($user); if (isset($seen[$key])) { return; } $seen[$key] = true; $users[] = $user; }; foreach ($this->users as $user) { $append($user); } foreach ($this->usersMultiGroups as $user) { $append($user); } return $users; }}