src/Entity/User.php line 22

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use App\Traits\Timestamps;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Security\Core\User\EquatableInterface;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  14. #[ORM\Entity(repositoryClassUserRepository::class)]
  15. #[UniqueEntity(fields: ['username'])]
  16. #[UniqueEntity(fields: ['email'])]
  17. #[ORM\HasLifecycleCallbacks]
  18. class User implements UserInterfaceEquatableInterfacePasswordAuthenticatedUserInterface
  19. {
  20.     use Timestamps;
  21.     #[ORM\Id]
  22.     #[ORM\GeneratedValue]
  23.     #[ORM\Column]
  24.     private ?int $id null;
  25.     #[ORM\Column(length180uniquetrue)]
  26.     #[Assert\NotBlank(message'Ne doit pas être vide')]
  27.     private ?string $username;
  28.     #[ORM\Column(type'json')]
  29.     private array $roles = [];
  30.     #[ORM\Column(length50)]
  31.     #[Assert\NotBlank(message'Ne doit pas être vide')]
  32.     private ?string $nomCompletnull;
  33.     #[ORM\Columnlength100uniquetrue)]
  34.     #[Assert\NotBlank(message'Ne doit pas être vide')]
  35.     #[Assert\Email(message'Email invalide')]
  36.     private ?string $emailnull;
  37.     #[ORM\Column]
  38.     private ?bool $valid null;
  39.     #[ORM\Column]
  40.     private ?bool $deleted null;
  41.     #[ORM\Columnlength255)]
  42.     private ?string $password null;
  43.     #[ORM\Column(type'boolean')]
  44.     private ?bool $admin null;
  45.     #[ORM\ManyToMany(targetEntityVault::class, mappedBy'authorizedUsers')]
  46.     private Collection $vaults;
  47.     public function __construct()
  48.     {
  49.         $this->vaults = new ArrayCollection();
  50.     }
  51.     public function getId(): ?int
  52.     {
  53.         return $this->id;
  54.     }
  55.     /**
  56.      * A visual identifier that represents this user.
  57.      *
  58.      * @see UserInterface
  59.      */
  60.     public function getUsername(): string
  61.     {
  62.         return (string) $this->username;
  63.     }
  64.     public function getUserIdentifier(): string
  65.     {
  66.         return (string) $this->username;
  67.     }
  68.     public function setUsername($username): self
  69.     {
  70.         $this->username $username;
  71.         return $this;
  72.     }
  73.     /**
  74.      * @see UserInterface
  75.      */
  76.     public function getRoles(): array
  77.     {
  78.         $roles $this->roles;
  79.         // guarantee every user at least has ROLE_USER
  80.         $roles[] = 'ROLE_USER';
  81.         return array_unique($roles);
  82.     }
  83.     public function setRoles(array $roles): self
  84.     {
  85.         $this->roles $roles;
  86.         return $this;
  87.     }
  88.     /**
  89.      * @see UserInterface
  90.      */
  91.     public function getPassword(): ?string
  92.     {
  93.         return $this->password;
  94.     }
  95.     /**
  96.      * @see UserInterface
  97.      */
  98.     public function eraseCredentials()
  99.     {
  100.         // If you store any temporary, sensitive data on the user, clear it here
  101.         // $this->plainPassword = null;
  102.     }
  103.     public function getNomComplet(): ?string
  104.     {
  105.         return $this->nomComplet;
  106.     }
  107.     public function setNomComplet($nomComplet): self
  108.     {
  109.         $this->nomComplet $nomComplet;
  110.         return $this;
  111.     }
  112.     public function getEmail(): ?string
  113.     {
  114.         return $this->email;
  115.     }
  116.     public function setEmail($email): self
  117.     {
  118.         $this->email $email;
  119.         return $this;
  120.     }
  121.     public function isValid(): ?bool
  122.     {
  123.         return $this->valid;
  124.     }
  125.     public function setValid(bool $valid): self
  126.     {
  127.         $this->valid $valid;
  128.         return $this;
  129.     }
  130.     public function isDeleted(): ?bool
  131.     {
  132.         return $this->deleted;
  133.     }
  134.     public function setDeleted(bool $deleted): self
  135.     {
  136.         $this->deleted $deleted;
  137.         return $this;
  138.     }
  139.     public function setPassword($password): self
  140.     {
  141.         $this->password $password;
  142.         return $this;
  143.     }
  144.     public function getAvatarUrl(): string
  145.     {
  146.         return "https://ui-avatars.com/api/?background=0D8ABC&color=fff&name=".$this->username;
  147.     }
  148.     public function getColorCode(): string
  149.     {
  150.         $code dechex(crc32($this->getUsername()));
  151.         $code substr($code06);
  152.         return '#'.$code;
  153.     }
  154.     #[Assert\Callback]
  155.     public function validate(ExecutionContextInterface $context$payload)
  156.     {
  157.         /*if (strlen($this->password)< 3){
  158.             $context->buildViolation('Mot de passe trop court')
  159.                 ->atPath('justpassword')
  160.                 ->addViolation();
  161.         }*/
  162.     }
  163.     public function __toString(): string
  164.     {
  165.         return "$this->nomComplet ($this->id)";
  166.     }
  167.     public function isAdmin(): ?bool
  168.     {
  169.         return $this->admin;
  170.     }
  171.     public function setAdmin(bool $admin): self
  172.     {
  173.         $this->admin $admin;
  174.         return $this;
  175.     }
  176.     public function isEqualTo(UserInterface $user): bool
  177.     {
  178.         if ($user instanceof User) {
  179.             return $this->isValid() && !$this->isDeleted() && $this->getPassword() == $user->getPassword() && $this->getUsername() == $user->getUsername()
  180.                 && $this->getEmail() == $user->getEmail();
  181.         }
  182.         return false;
  183.     }
  184.     /**
  185.      * @return Collection<int, Vault>
  186.      */
  187.     public function getVaults(): Collection
  188.     {
  189.         return $this->vaults;
  190.     }
  191.     public function addVault(Vault $vault): self
  192.     {
  193.         if (!$this->vaults->contains($vault)) {
  194.             $this->vaults->add($vault);
  195.             $vault->addAuthorizedUser($this);
  196.         }
  197.         return $this;
  198.     }
  199.     public function removeVault(Vault $vault): self
  200.     {
  201.         if ($this->vaults->removeElement($vault)) {
  202.             $vault->removeAuthorizedUser($this);
  203.         }
  204.         return $this;
  205.     }
  206.     public function hasRole(string $role): bool
  207.     {
  208.         return in_array($role$this->getRoles());
  209.     }
  210. }