src/Entity/User.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. #[ORM\Entity(repositoryClassUserRepository::class)]
  12. #[ORM\Table(name'`user`')]
  13. #[UniqueEntity(fields: ['email'], message"Cet email est déjà utilisé")]
  14. class User implements UserInterfacePasswordAuthenticatedUserInterface
  15. {
  16.     #[ORM\Id]
  17.     #[ORM\GeneratedValue]
  18.     #[ORM\Column]
  19.     private ?int $id null;
  20.     #[ORM\Column(length180uniquetrue)]
  21.     #[Assert\Email]
  22.     #[Assert\Length(max180)]
  23.     private ?string $email null;
  24.     #[ORM\Column]
  25.     private array $roles = [];
  26.     /**
  27.      * @var string The hashed password
  28.      */
  29.     #[ORM\Column]
  30.     private ?string $password null;
  31.     #[ORM\OneToMany(mappedBy'user'targetEntityUserAccess::class, orphanRemovaltrue)]
  32.     private Collection $accesses;
  33.     public function __construct()
  34.     {
  35.         $this->accesses = new ArrayCollection();
  36.     }
  37.     /**
  38.      * @param object $entity Resource to access
  39.      * @param string[] $requiredRoles Should have at least one of these roles. Ex: "role1" OR "role2"
  40.      *
  41.      * @return bool
  42.      */
  43.     public function canAccessResource(object $entity, array $requiredRoles): bool
  44.     {
  45.         $fn = function(UserAccess $access) use ($entity$requiredRoles) {
  46.             if (!in_array($access->getRole(), $requiredRoles)) {
  47.                 return false;
  48.             }
  49.             return ($access->getType() === UserAccess::TYPE_MAGASIN && $access->getMagasin() === $entity)
  50.                 || ($access->getType() === UserAccess::TYPE_GROUPEMENT && $access->getGroupement() === $entity)
  51.             ;
  52.         };
  53.         return $this->getAccesses()
  54.             ->filter($fn)
  55.             ->count() > 0;
  56.     }
  57.     public function getDefaultGroupement(): ?Groupement
  58.     {
  59.         foreach ($this->getAccesses() as $access) {
  60.             if ($access->getType() === UserAccess::TYPE_GROUPEMENT && $access->getRole() === UserAccess::ROLE_ADMIN) {
  61.                 return $access->getGroupement();
  62.             }
  63.         }
  64.         return null;
  65.     }
  66.     public function getDefaultProject(): ?Project
  67.     {
  68.         foreach ($this->getAccesses() as $access) {
  69.             if ($access->getType() === UserAccess::TYPE_MAGASIN && $access->getRole() === UserAccess::ROLE_ADMIN) {
  70.                 return $access->getMagasin()?->getProject();
  71.             }
  72.         }
  73.         return null;
  74.     }
  75.     public function getId(): ?int
  76.     {
  77.         return $this->id;
  78.     }
  79.     public function getEmail(): ?string
  80.     {
  81.         return $this->email;
  82.     }
  83.     public function setEmail(string $email): static
  84.     {
  85.         $this->email $email;
  86.         return $this;
  87.     }
  88.     /**
  89.      * A visual identifier that represents this user.
  90.      *
  91.      * @see UserInterface
  92.      */
  93.     public function getUserIdentifier(): string
  94.     {
  95.         return (string) $this->email;
  96.     }
  97.     /**
  98.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  99.      */
  100.     public function getUsername(): string
  101.     {
  102.         return (string) $this->email;
  103.     }
  104.     /**
  105.      * @see UserInterface
  106.      */
  107.     public function getRoles(): array
  108.     {
  109.         $roles $this->roles;
  110.         // guarantee every user at least has ROLE_USER
  111.         $roles[] = 'ROLE_USER';
  112.         return array_unique($roles);
  113.     }
  114.     public function setRoles(array $roles): static
  115.     {
  116.         $this->roles $roles;
  117.         return $this;
  118.     }
  119.     /**
  120.      * @see PasswordAuthenticatedUserInterface
  121.      */
  122.     public function getPassword(): string
  123.     {
  124.         return $this->password;
  125.     }
  126.     public function setPassword(string $password): static
  127.     {
  128.         $this->password $password;
  129.         return $this;
  130.     }
  131.     /**
  132.      * Returning a salt is only needed, if you are not using a modern
  133.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  134.      *
  135.      * @see UserInterface
  136.      */
  137.     public function getSalt(): ?string
  138.     {
  139.         return null;
  140.     }
  141.     /**
  142.      * @see UserInterface
  143.      */
  144.     public function eraseCredentials(): void
  145.     {
  146.         // If you store any temporary, sensitive data on the user, clear it here
  147.         // $this->plainPassword = null;
  148.     }
  149.     /**
  150.      * @return Collection<int, UserAccess>
  151.      */
  152.     public function getAccesses(): Collection
  153.     {
  154.         return $this->accesses;
  155.     }
  156.     public function addAccess(UserAccess $access): static
  157.     {
  158.         if (!$this->accesses->contains($access)) {
  159.             $this->accesses->add($access);
  160.             $access->setUser($this);
  161.         }
  162.         return $this;
  163.     }
  164.     public function removeAccess(UserAccess $access): static
  165.     {
  166.         if ($this->accesses->removeElement($access)) {
  167.             // set the owning side to null (unless already changed)
  168.             if ($access->getUser() === $this) {
  169.                 $access->setUser(null);
  170.             }
  171.         }
  172.         return $this;
  173.     }
  174. }