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.     public function __construct()
  46.     {
  47.     }
  48.     public function getId(): ?int
  49.     {
  50.         return $this->id;
  51.     }
  52.     /**
  53.      * A visual identifier that represents this user.
  54.      *
  55.      * @see UserInterface
  56.      */
  57.     public function getUsername(): string
  58.     {
  59.         return (string) $this->username;
  60.     }
  61.     public function getUserIdentifier(): string
  62.     {
  63.         return (string) $this->username;
  64.     }
  65.     public function setUsername($username): self
  66.     {
  67.         $this->username $username;
  68.         return $this;
  69.     }
  70.     /**
  71.      * @see UserInterface
  72.      */
  73.     public function getRoles(): array
  74.     {
  75.         $roles $this->roles;
  76.         // guarantee every user at least has ROLE_USER
  77.         $roles[] = 'ROLE_USER';
  78.         return array_unique($roles);
  79.     }
  80.     public function setRoles(array $roles): self
  81.     {
  82.         $this->roles $roles;
  83.         return $this;
  84.     }
  85.     /**
  86.      * @see UserInterface
  87.      */
  88.     public function getPassword(): ?string
  89.     {
  90.         return $this->password;
  91.     }
  92.     /**
  93.      * @see UserInterface
  94.      */
  95.     public function eraseCredentials()
  96.     {
  97.         // If you store any temporary, sensitive data on the user, clear it here
  98.         // $this->plainPassword = null;
  99.     }
  100.     public function getNomComplet(): ?string
  101.     {
  102.         return $this->nomComplet;
  103.     }
  104.     public function setNomComplet($nomComplet): self
  105.     {
  106.         $this->nomComplet $nomComplet;
  107.         return $this;
  108.     }
  109.     public function getEmail(): ?string
  110.     {
  111.         return $this->email;
  112.     }
  113.     public function setEmail($email): self
  114.     {
  115.         $this->email $email;
  116.         return $this;
  117.     }
  118.     public function isValid(): ?bool
  119.     {
  120.         return $this->valid;
  121.     }
  122.     public function setValid(bool $valid): self
  123.     {
  124.         $this->valid $valid;
  125.         return $this;
  126.     }
  127.     public function isDeleted(): ?bool
  128.     {
  129.         return $this->deleted;
  130.     }
  131.     public function setDeleted(bool $deleted): self
  132.     {
  133.         $this->deleted $deleted;
  134.         return $this;
  135.     }
  136.     public function setPassword($password): self
  137.     {
  138.         $this->password $password;
  139.         return $this;
  140.     }
  141.     public function getAvatarUrl(): string
  142.     {
  143.         return "https://ui-avatars.com/api/?background=0D8ABC&color=fff&name=".$this->username;
  144.     }
  145.     public function getColorCode(): string
  146.     {
  147.         $code dechex(crc32($this->getUsername()));
  148.         $code substr($code06);
  149.         return '#'.$code;
  150.     }
  151.     #[Assert\Callback]
  152.     public function validate(ExecutionContextInterface $context$payload)
  153.     {
  154.         /*if (strlen($this->password)< 3){
  155.             $context->buildViolation('Mot de passe trop court')
  156.                 ->atPath('justpassword')
  157.                 ->addViolation();
  158.         }*/
  159.     }
  160.     public function __toString(): string
  161.     {
  162.         return "$this->nomComplet ($this->id)";
  163.     }
  164.     public function isAdmin(): ?bool
  165.     {
  166.         return $this->admin;
  167.     }
  168.     public function setAdmin(bool $admin): self
  169.     {
  170.         $this->admin $admin;
  171.         return $this;
  172.     }
  173.     public function isEqualTo(UserInterface $user): bool
  174.     {
  175.         if ($user instanceof User) {
  176.             return $this->isValid() && !$this->isDeleted() && $this->getPassword() == $user->getPassword() && $this->getUsername() == $user->getUsername()
  177.                 && $this->getEmail() == $user->getEmail();
  178.         }
  179.         return false;
  180.     }
  181. }