src/Entity/User.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. /**
  9.  * @ORM\Entity(repositoryClass=UserRepository::class)
  10.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  11.  */
  12. class User implements UserInterfacePasswordAuthenticatedUserInterface
  13. {
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\GeneratedValue
  17.      * @ORM\Column(type="integer")
  18.      */
  19.     private ?int $id null;
  20.     /**
  21.      * @ORM\Column(type="string", length=180, unique=true)
  22.      */
  23.     private ?string $email null;
  24.     /**
  25.      * @ORM\Column(type="string", length=100)
  26.      */
  27.     private string $name;
  28.     /**
  29.      * @ORM\Column(type="string", length=30)
  30.      */
  31.     private string $phone;
  32.     /**
  33.      * @ORM\Column(type="json")
  34.      */
  35.     private array $roles = [];
  36.     /**
  37.      * @var string|null The hashed password
  38.      * @ORM\Column(type="string", nullable=true)
  39.      */
  40.     private ?string $password '';
  41.     /**
  42.      * @ORM\Column(type="boolean")
  43.      */
  44.     private bool $isVerified false;
  45.     /**
  46.      * True for an account this app created by itself while a visitor was
  47.      * checking out as a guest, rather than one somebody filled the sign-up form
  48.      * in for. Such an account has a random password nobody knows, so it stays
  49.      * unusable for signing in until the passenger follows the "choose a
  50.      * password" link mailed to them. Cleared the moment they do.
  51.      *
  52.      * @ORM\Column(type="boolean")
  53.      */
  54.     private bool $isGuest false;
  55.     /**
  56.      * One-time token behind that link. Null once used or never issued.
  57.      *
  58.      * @ORM\Column(type="string", length=64, nullable=true, unique=true)
  59.      */
  60.     private ?string $passwordSetToken null;
  61.     /**
  62.      * @ORM\Column(type="datetime_immutable", nullable=true)
  63.      */
  64.     private ?\DateTimeImmutable $passwordSetTokenExpiresAt null;
  65.     private ?string $plainPassword null;
  66.     public function getId(): ?int
  67.     {
  68.         return $this->id;
  69.     }
  70.     public function getEmail(): ?string
  71.     {
  72.         return $this->email;
  73.     }
  74.     public function setEmail(string $email): self
  75.     {
  76.         $this->email $email;
  77.         return $this;
  78.     }
  79.     /**
  80.      * A visual identifier that represents this user.
  81.      * @see UserInterface
  82.      */
  83.     public function getUserIdentifier(): string
  84.     {
  85.         return (string) $this->email;
  86.     }
  87.     /**
  88.      * Required by UserInterface for Symfony 5 compatibility.
  89.      */
  90.     public function getUsername(): string
  91.     {
  92.         return $this->getUserIdentifier();
  93.     }
  94.     public function hasRole(string $role): bool
  95.     {
  96.         return in_array($role$this->getRoles());
  97.     }
  98.     /**
  99.      * @see UserInterface
  100.      */
  101.     public function getRoles(): array
  102.     {
  103.         $roles $this->roles;
  104.         $roles[] = 'ROLE_USER';
  105.         return array_unique($roles);
  106.     }
  107.     public function setRoles(array $roles): self
  108.     {
  109.         $this->roles $roles;
  110.         return $this;
  111.     }
  112.     /**
  113.      * @see PasswordAuthenticatedUserInterface
  114.      */
  115.     public function getPassword(): ?string
  116.     {
  117.         return $this->password;
  118.     }
  119.     public function setPassword(?string $password): self
  120.     {
  121.         $this->password $password;
  122.         return $this;
  123.     }
  124.     /**
  125.      * @see UserInterface
  126.      */
  127.     public function eraseCredentials(): void
  128.     {
  129.         $this->plainPassword null;
  130.     }
  131.     /**
  132.      * Required by UserInterface for Symfony 5 compatibility.
  133.      */
  134.     public function getSalt(): ?string
  135.     {
  136.         return null;
  137.     }
  138.     public function isVerified(): bool
  139.     {
  140.         return $this->isVerified;
  141.     }
  142.     public function setIsVerified(bool $isVerified): self
  143.     {
  144.         $this->isVerified $isVerified;
  145.         return $this;
  146.     }
  147.     public function isIsVerified(): ?bool
  148.     {
  149.         return $this->isVerified;
  150.     }
  151.     public function isGuest(): bool
  152.     {
  153.         return $this->isGuest;
  154.     }
  155.     public function setIsGuest(bool $isGuest): self
  156.     {
  157.         $this->isGuest $isGuest;
  158.         return $this;
  159.     }
  160.     public function getPasswordSetToken(): ?string
  161.     {
  162.         return $this->passwordSetToken;
  163.     }
  164.     public function getPasswordSetTokenExpiresAt(): ?\DateTimeImmutable
  165.     {
  166.         return $this->passwordSetTokenExpiresAt;
  167.     }
  168.     /**
  169.      * Issue (or re-issue) the one-time link that lets this account choose its
  170.      * first password, and hand the raw token back for the email. Any token
  171.      * issued earlier stops working, so the newest mail is always the live one.
  172.      */
  173.     public function startPasswordSetup(int $validForDays 14): string
  174.     {
  175.         $this->passwordSetToken          bin2hex(random_bytes(32));
  176.         $this->passwordSetTokenExpiresAt = new \DateTimeImmutable(sprintf('+%d days'$validForDays));
  177.         return $this->passwordSetToken;
  178.     }
  179.     public function isPasswordSetupPending(): bool
  180.     {
  181.         return $this->passwordSetToken !== null
  182.             && $this->passwordSetTokenExpiresAt !== null
  183.             && $this->passwordSetTokenExpiresAt > new \DateTimeImmutable();
  184.     }
  185.     /**
  186.      * Burn the token. Called once the password is chosen, so the link in the
  187.      * mailbox cannot be replayed by anyone who reads that inbox later.
  188.      */
  189.     public function finishPasswordSetup(): void
  190.     {
  191.         $this->passwordSetToken          null;
  192.         $this->passwordSetTokenExpiresAt null;
  193.         $this->isGuest                   false;
  194.     }
  195.     public function getPlainPassword(): ?string
  196.     {
  197.         return $this->plainPassword;
  198.     }
  199.     public function setPlainPassword(?string $plainPassword): void
  200.     {
  201.         $this->plainPassword $plainPassword;
  202.     }
  203.     public function getName(): string
  204.     {
  205.         return $this->name;
  206.     }
  207.     public function setName(string $name): void
  208.     {
  209.         $this->name $name;
  210.     }
  211.     public function getPhone(): string
  212.     {
  213.         return $this->phone;
  214.     }
  215.     public function setPhone(string $phone): void
  216.     {
  217.         $this->phone $phone;
  218.     }
  219.     public function __toString(): string
  220.     {
  221.         return $this->email ?? '';
  222.     }
  223. }