src/Entity/GroupUser.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\GroupUserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=GroupUserRepository::class)
  9.  */
  10. class GroupUser
  11. {
  12.     /**
  13.      * @ORM\Id
  14.      * @ORM\GeneratedValue
  15.      * @ORM\Column(type="integer")
  16.      */
  17.     private $id;
  18.     /**
  19.      * @ORM\Column(type="string", length=255)
  20.      */
  21.     private $name;
  22.     /**
  23.      * @ORM\Column(type="string", length=255)
  24.      */
  25.     private $code;
  26.     /**
  27.      * @ORM\ManyToMany(targetEntity=Right::class, mappedBy="groups")
  28.      */
  29.     private $rights;
  30.     /**
  31.      * @ORM\OneToMany(targetEntity=User::class, mappedBy="groupUser")
  32.      */
  33.     private $users;
  34.     /**
  35.      * @ORM\ManyToMany(targetEntity=User::class, mappedBy="groupUsers")
  36.      */
  37.     private $usersMultiGroups;
  38.     public function __construct()
  39.     {
  40.         $this->rights = new ArrayCollection();
  41.         $this->users = new ArrayCollection();
  42.         $this->usersMultiGroups = new ArrayCollection();
  43.     }
  44.     private function ensureCollectionsInitialized(): void
  45.     {
  46.         if ($this->rights === null) {
  47.             $this->rights = new ArrayCollection();
  48.         }
  49.         if ($this->users === null) {
  50.             $this->users = new ArrayCollection();
  51.         }
  52.         if ($this->usersMultiGroups === null) {
  53.             $this->usersMultiGroups = new ArrayCollection();
  54.         }
  55.     }
  56.     public function getId(): ?int
  57.     {
  58.         return $this->id;
  59.     }
  60.     public function getName(): ?string
  61.     {
  62.         return $this->name;
  63.     }
  64.     public function setName(string $name): self
  65.     {
  66.         $this->name $name;
  67.         return $this;
  68.     }
  69.     public function getCode(): ?string
  70.     {
  71.         return $this->code;
  72.     }
  73.     public function setCode(string $code): self
  74.     {
  75.         $this->code $code;
  76.         return $this;
  77.     }
  78.     /**
  79.      * @return Collection|Right[]
  80.      */
  81.     public function getRights(): Collection
  82.     {
  83.         $this->ensureCollectionsInitialized();
  84.         return $this->rights;
  85.     }
  86.     public function addRight(Right $right): self
  87.     {
  88.         $this->ensureCollectionsInitialized();
  89.         if( !$this->rights->contains($right)) {
  90.             $this->rights[] = $right;
  91.             $right->addGroup($this);
  92.         }
  93.         return $this;
  94.     }
  95.     public function removeRight(Right $right): self
  96.     {
  97.         $this->ensureCollectionsInitialized();
  98.         if( $this->rights->removeElement($right)) {
  99.             $right->removeGroup($this);
  100.         }
  101.         return $this;
  102.     }
  103.     /**
  104.      * @return Collection|User[]
  105.      */
  106.     public function getUsers(): Collection
  107.     {
  108.         $this->ensureCollectionsInitialized();
  109.         return $this->users;
  110.     }
  111.     public function addUser(User $user): self
  112.     {
  113.         $this->ensureCollectionsInitialized();
  114.         if( !$this->users->contains($user)) {
  115.             $this->users[] = $user;
  116.             $user->setGroupUser($this);
  117.         }
  118.         return $this;
  119.     }
  120.     public function removeUser(User $user): self
  121.     {
  122.         $this->ensureCollectionsInitialized();
  123.         if( $this->users->removeElement($user)) {
  124.             // set the owning side to null (unless already changed)
  125.             if( $user->getGroupUser() === $this) {
  126.                 $user->setGroupUser(null);
  127.             }
  128.             $user->removeGroupUser($this);
  129.         }
  130.         return $this;
  131.     }
  132.     public function setRights(iterable $rights): self
  133.     {
  134.         $this->ensureCollectionsInitialized();
  135.         // On vide d'abord les droits existants
  136.         $this->rights = new ArrayCollection();
  137.         // Puis on ajoute chaque droit individuellement
  138.         foreach ($rights as $right) {
  139.             $this->addRight($right);
  140.         }
  141.         return $this;
  142.     }
  143.     /**
  144.      * @return Collection|User[]
  145.      */
  146.     public function getUsersMultiGroups(): Collection
  147.     {
  148.         $this->ensureCollectionsInitialized();
  149.         return $this->usersMultiGroups;
  150.     }
  151.     public function addUsersMultiGroup(User $user): self
  152.     {
  153.         $this->ensureCollectionsInitialized();
  154.         if (!$this->usersMultiGroups->contains($user)) {
  155.             $this->usersMultiGroups[] = $user;
  156.         }
  157.         return $this;
  158.     }
  159.     public function removeUsersMultiGroup(User $user): self
  160.     {
  161.         $this->ensureCollectionsInitialized();
  162.         $this->usersMultiGroups->removeElement($user);
  163.         return $this;
  164.     }
  165.     /**
  166.      * @return User[]
  167.      */
  168.     public function getEffectiveUsers(): array
  169.     {
  170.         $this->ensureCollectionsInitialized();
  171.         $users = [];
  172.         $seen = [];
  173.         $append = static function (?User $user) use (&$users, &$seen): void {
  174.             if (!$user) {
  175.                 return;
  176.             }
  177.             $key $user->getId() !== null
  178.                 'id:' $user->getId()
  179.                 : 'obj:' spl_object_hash($user);
  180.             if (isset($seen[$key])) {
  181.                 return;
  182.             }
  183.             $seen[$key] = true;
  184.             $users[] = $user;
  185.         };
  186.         foreach ($this->users as $user) {
  187.             $append($user);
  188.         }
  189.         foreach ($this->usersMultiGroups as $user) {
  190.             $append($user);
  191.         }
  192.         return $users;
  193.     }
  194. }