src/Entity/User.php line 16

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 Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. /**
  11.  * @ORM\Entity(repositoryClass=UserRepository::class)
  12.  */
  13. class User implements UserInterface, PasswordAuthenticatedUserInterface, TwoFactorInterface
  14. {
  15.     
  16.     use \App\Traits\CommonTrt;
  17.     /**
  18.      * @ORM\Id
  19.      * @ORM\GeneratedValue
  20.      * @ORM\Column(type="integer")
  21.      */
  22.     private $id;
  23.     /**
  24.      * @ORM\Column(type="string", length=180, unique=true)
  25.      */
  26.     private $email;
  27.     /**
  28.      * @ORM\Column(type="json")
  29.      */
  30.     private $roles = [];
  31.     /**
  32.      * @var string The hashed password
  33.      * @ORM\Column(type="string")
  34.      */
  35.     private $password;
  36.     /**
  37.      * @ORM\Column(type="string", length=255)
  38.      */
  39.     private $firstname;
  40.     /**
  41.      * @ORM\Column(type="string", length=255)
  42.      */
  43.     private $lastname;
  44.     /**
  45.      * @ORM\ManyToMany(targetEntity=Roles::class, inversedBy="users")
  46.      */
  47.     private $userRoles;
  48.     /**
  49.      * @ORM\Column(type="boolean")
  50.      */
  51.     private $isActive;
  52.     /**
  53.      * @ORM\Column(type="string", nullable=true)
  54.      */
  55.     private string $authCode;
  56.     /**
  57.      * @ORM\OneToMany(targetEntity=Historiques::class, mappedBy="user")
  58.      */
  59.     private $historiques;
  60.     // [...]
  61.     public function isEmailAuthEnabled(): bool
  62.     {
  63.         return true; // This can be a persisted field to switch email code authentication on/off
  64.     }
  65.     public function getEmailAuthRecipient(): string
  66.     {
  67.         return $this->email;
  68.     }
  69.     public function getEmailAuthCode(): string
  70.     {
  71.         if (null === $this->authCode) {
  72.             throw new \LogicException('The email authentication code was not set');
  73.         }
  74.         return $this->authCode;
  75.     }
  76.     public function setEmailAuthCode(string $authCode): void
  77.     {
  78.         $this->authCode = $authCode;
  79.     }
  80.     public function __construct() {
  81.         $this->userRoles = new ArrayCollection();
  82.         $this->historiques = new ArrayCollection();
  83.     }
  84.     public function getId(): ?int {
  85.         return $this->id;
  86.     }
  87.     public function getEmail(): ?string {
  88.         return $this->email;
  89.     }
  90.     public function setEmail(string $email): self {
  91.         $this->email = $email;
  92.         return $this;
  93.     }
  94.     /**
  95.      * A visual identifier that represents this user.
  96.      *
  97.      * @see UserInterface
  98.      */
  99.     public function getUserIdentifier(): string {
  100.         return (string) $this->email;
  101.     }
  102.     /**
  103.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  104.      */
  105.     public function getUsername(): string {
  106.         return (string) $this->email;
  107.     }
  108.     /**
  109.      * @see UserInterface
  110.      */
  111.     public function getRoles() {
  112.         $roles = $this->userRoles->map(function ($role) {
  113.                     return $role->getName();
  114.                 })->toArray();
  115.         $roles[] = 'ROLE_USER';
  116.         return $roles;
  117.     }
  118.     public function setRoles(array $roles): self {
  119.         $this->roles = $roles;
  120.         return $this;
  121.     }
  122.     /**
  123.      * @see PasswordAuthenticatedUserInterface
  124.      */
  125.     public function getPassword(): string {
  126.         return $this->password;
  127.     }
  128.     public function setPassword(string $password): self {
  129.         $this->password = $password;
  130.         return $this;
  131.     }
  132.     /**
  133.      * Returning a salt is only needed, if you are not using a modern
  134.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  135.      *
  136.      * @see UserInterface
  137.      */
  138.     public function getSalt(): ?string {
  139.         return null;
  140.     }
  141.     /**
  142.      * @see UserInterface
  143.      */
  144.     public function eraseCredentials() {
  145.         // If you store any temporary, sensitive data on the user, clear it here
  146.         // $this->plainPassword = null;
  147.     }
  148.     public function getFirstname(): ?string {
  149.         return $this->firstname;
  150.     }
  151.     public function setFirstname(string $firstname): self {
  152.         $this->firstname = $firstname;
  153.         return $this;
  154.     }
  155.     public function getLastname(): ?string {
  156.         return $this->lastname;
  157.     }
  158.     public function setLastname(string $lastname): self {
  159.         $this->lastname = $lastname;
  160.         return $this;
  161.     }
  162.     /**
  163.      * @return Collection<int, Roles>
  164.      */
  165.     public function getUserRoles(): Collection {
  166.         return $this->userRoles;
  167.     }
  168.     public function addUserRole(Roles $userRole): self {
  169.         if (!$this->userRoles->contains($userRole)) {
  170.             $this->userRoles[] = $userRole;
  171.         }
  172.         return $this;
  173.     }
  174.     public function removeUserRole(Roles $userRole): self {
  175.         $this->userRoles->removeElement($userRole);
  176.         return $this;
  177.     }
  178.     
  179.     public function getFullName() {
  180.         return "$this->firstname $this->lastname";
  181.     }
  182.     public function isIsActive(): ?bool
  183.     {
  184.         return $this->isActive;
  185.     }
  186.     public function setIsActive(bool $isActive): self
  187.     {
  188.         $this->isActive = $isActive;
  189.         return $this;
  190.     }
  191.     public function __toString()
  192.     {
  193.         return $this->getFullName();
  194.     }
  195.     /**
  196.      * @return Collection<int, Historiques>
  197.      */
  198.     public function getHistoriques(): Collection
  199.     {
  200.         return $this->historiques;
  201.     }
  202.     public function addHistorique(Historiques $historique): self
  203.     {
  204.         if (!$this->historiques->contains($historique)) {
  205.             $this->historiques[] = $historique;
  206.             $historique->setUser($this);
  207.         }
  208.         return $this;
  209.     }
  210.     public function removeHistorique(Historiques $historique): self
  211.     {
  212.         if ($this->historiques->removeElement($historique)) {
  213.             // set the owning side to null (unless already changed)
  214.             if ($historique->getUser() === $this) {
  215.                 $historique->setUser(null);
  216.             }
  217.         }
  218.         return $this;
  219.     }
  220. }