src/UserBundle/Entity/User.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of SolidInvoice project.
  5.  *
  6.  * (c) Pierre du Plessis <[email protected]>
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace SolidInvoice\UserBundle\Entity;
  12. use DateTime;
  13. use DateTimeInterface;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\DBAL\Types\Types;
  17. use Doctrine\ORM\Mapping as ORM;
  18. use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
  19. use Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator;
  20. use Ramsey\Uuid\UuidInterface;
  21. use SolidInvoice\CoreBundle\Entity\Company;
  22. use SolidInvoice\CoreBundle\Traits\Entity\TimeStampable;
  23. use SolidInvoice\UserBundle\Repository\UserRepository;
  24. use Stringable;
  25. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  26. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  27. use Symfony\Component\Security\Core\User\UserInterface;
  28. #[ORM\Table(nameUser::TABLE_NAME)]
  29. #[ORM\Entity(repositoryClassUserRepository::class)]
  30. #[UniqueEntity(fields: ['email'], message'This email is already in use. Do you want to log in instead?')]
  31. #[UniqueEntity(fields: ['username'], message'This username is already in use')]
  32. class User implements UserInterfacePasswordAuthenticatedUserInterfaceStringable
  33. {
  34.     final public const TABLE_NAME 'users';
  35.     use TimeStampable;
  36.     #[ORM\Column(typeUuidBinaryOrderedTimeType::NAME)]
  37.     #[ORM\Id]
  38.     #[ORM\GeneratedValue(strategy'CUSTOM')]
  39.     #[ORM\CustomIdGenerator(class: UuidOrderedTimeGenerator::class)]
  40.     private ?UuidInterface $id null;
  41.     #[ORM\Column(name'mobile'typeTypes::STRINGnullabletrue)]
  42.     private ?string $mobile null;
  43.     /**
  44.      * @var Collection<int, ApiToken>
  45.      */
  46.     #[ORM\OneToMany(mappedBy'user'targetEntityApiToken::class, cascade: ['persist''remove'], fetch'EXTRA_LAZY'orphanRemovaltrue)]
  47.     private Collection $apiTokens;
  48.     #[ORM\Column(name'username'typeTypes::STRINGlength180uniquetrue)]
  49.     private ?string $username null;
  50.     #[ORM\Column(name'email'typeTypes::STRINGlength180uniquetrue)]
  51.     private ?string $email null;
  52.     #[ORM\Column(name'enabled'typeTypes::BOOLEAN)]
  53.     private bool $enabled false;
  54.     #[ORM\Column(name'password'typeTypes::STRING)]
  55.     private ?string $password null;
  56.     private string $plainPassword '';
  57.     #[ORM\Column(name'last_login'typeTypes::DATETIME_MUTABLEnullabletrue)]
  58.     private ?DateTimeInterface $lastLogin null;
  59.     #[ORM\Column(name'confirmation_token'typeTypes::STRINGlength180uniquetruenullabletrue)]
  60.     private ?string $confirmationToken null;
  61.     #[ORM\Column(name'password_requested_at'typeTypes::DATETIME_MUTABLEnullabletrue)]
  62.     private ?DateTimeInterface $passwordRequestedAt null;
  63.     /**
  64.      * @var string[]
  65.      */
  66.     #[ORM\Column(name'roles'type'array')]
  67.     private array $roles = [];
  68.     /**
  69.      * @var Collection<int, Company>
  70.      */
  71.     #[ORM\ManyToMany(targetEntityCompany::class, inversedBy'users')]
  72.     private Collection $companies;
  73.     public function __construct()
  74.     {
  75.         $this->apiTokens = new ArrayCollection();
  76.         $this->companies = new ArrayCollection();
  77.     }
  78.     /**
  79.      * Don't return the salt, and rely on password_hash to generate a salt.
  80.      */
  81.     public function getSalt(): ?string
  82.     {
  83.         return null;
  84.     }
  85.     /**
  86.      * @return Collection<int, ApiToken>
  87.      */
  88.     public function getApiTokens(): Collection
  89.     {
  90.         return $this->apiTokens;
  91.     }
  92.     /**
  93.      * @param Collection<int, ApiToken> $apiTokens
  94.      */
  95.     public function setApiTokens(Collection $apiTokens): self
  96.     {
  97.         $this->apiTokens $apiTokens;
  98.         return $this;
  99.     }
  100.     public function getMobile(): ?string
  101.     {
  102.         return $this->mobile;
  103.     }
  104.     public function setMobile(string $mobile): self
  105.     {
  106.         $this->mobile $mobile;
  107.         return $this;
  108.     }
  109.     public function __toString(): string
  110.     {
  111.         return $this->username;
  112.     }
  113.     public function addRole(string $role): self
  114.     {
  115.         $role strtoupper($role);
  116.         if ('ROLE_USER' === $role) {
  117.             return $this;
  118.         }
  119.         if (! in_array($role$this->rolestrue)) {
  120.             $this->roles[] = $role;
  121.         }
  122.         return $this;
  123.     }
  124.     public function serialize(): string
  125.     {
  126.         return serialize([
  127.             $this->password,
  128.             $this->username,
  129.             $this->enabled,
  130.             $this->id,
  131.             $this->email,
  132.             $this->roles,
  133.             $this->mobile,
  134.             $this->created,
  135.             $this->updated,
  136.         ]);
  137.     }
  138.     public function unserialize(string $serialized): void
  139.     {
  140.         [
  141.             $this->password,
  142.             $this->username,
  143.             $this->enabled,
  144.             $this->id,
  145.             $this->email,
  146.             $this->roles,
  147.             $this->created,
  148.             $this->updated
  149.         ] = unserialize($serialized);
  150.     }
  151.     public function eraseCredentials(): void
  152.     {
  153.         $this->plainPassword '';
  154.     }
  155.     public function getId(): ?UuidInterface
  156.     {
  157.         return $this->id;
  158.     }
  159.     public function getUsername(): ?string
  160.     {
  161.         return $this->username;
  162.     }
  163.     public function getEmail(): ?string
  164.     {
  165.         return $this->email;
  166.     }
  167.     public function getPassword(): ?string
  168.     {
  169.         return $this->password;
  170.     }
  171.     public function getPlainPassword(): string
  172.     {
  173.         return $this->plainPassword;
  174.     }
  175.     public function getLastLogin(): ?DateTimeInterface
  176.     {
  177.         return $this->lastLogin;
  178.     }
  179.     public function getConfirmationToken(): ?string
  180.     {
  181.         return $this->confirmationToken;
  182.     }
  183.     public function getRoles(): array
  184.     {
  185.         $roles $this->roles;
  186.         // we need to make sure to have at least one role
  187.         $roles[] = 'ROLE_USER';
  188.         return array_unique($roles);
  189.     }
  190.     public function hasRole(string $role): bool
  191.     {
  192.         return in_array(strtoupper($role), $this->getRoles(), true);
  193.     }
  194.     public function isEnabled(): bool
  195.     {
  196.         return $this->enabled;
  197.     }
  198.     public function removeRole(string $role): self
  199.     {
  200.         if (false !== $key array_search(strtoupper($role), $this->rolestrue)) {
  201.             unset($this->roles[$key]);
  202.             $this->roles array_values($this->roles);
  203.         }
  204.         return $this;
  205.     }
  206.     public function setUsername(string $username): self
  207.     {
  208.         $this->username $username;
  209.         return $this;
  210.     }
  211.     public function setEmail(string $email): self
  212.     {
  213.         $this->email $email;
  214.         return $this;
  215.     }
  216.     public function setEnabled(bool $enabled): self
  217.     {
  218.         $this->enabled $enabled;
  219.         return $this;
  220.     }
  221.     public function setPassword(string $password): self
  222.     {
  223.         $this->password $password;
  224.         return $this;
  225.     }
  226.     public function setPlainPassword(?string $password): self
  227.     {
  228.         $this->plainPassword = (string) $password;
  229.         return $this;
  230.     }
  231.     public function setLastLogin(?DateTimeInterface $time null): self
  232.     {
  233.         $this->lastLogin $time;
  234.         return $this;
  235.     }
  236.     public function setConfirmationToken(?string $confirmationToken): self
  237.     {
  238.         $this->confirmationToken $confirmationToken;
  239.         return $this;
  240.     }
  241.     public function setPasswordRequestedAt(?DateTimeInterface $date null): self
  242.     {
  243.         $this->passwordRequestedAt $date;
  244.         return $this;
  245.     }
  246.     public function getPasswordRequestedAt(): ?DateTimeInterface
  247.     {
  248.         return $this->passwordRequestedAt;
  249.     }
  250.     public function isPasswordRequestNonExpired(int $ttl): bool
  251.     {
  252.         return $this->getPasswordRequestedAt() instanceof DateTime &&
  253.             $this->getPasswordRequestedAt()->getTimestamp() + $ttl time();
  254.     }
  255.     /**
  256.      * @param string[] $roles
  257.      */
  258.     public function setRoles(array $roles): self
  259.     {
  260.         $this->roles = [];
  261.         foreach ($roles as $role) {
  262.             $this->addRole($role);
  263.         }
  264.         return $this;
  265.     }
  266.     /**
  267.      * @return Collection<int, Company>
  268.      */
  269.     public function getCompanies(): Collection
  270.     {
  271.         return $this->companies;
  272.     }
  273.     public function addCompany(Company $company): self
  274.     {
  275.         if (! $this->companies->contains($company)) {
  276.             $this->companies[] = $company;
  277.         }
  278.         return $this;
  279.     }
  280.     public function removeCompany(Company $company): self
  281.     {
  282.         if ($this->companies->contains($company)) {
  283.             $this->companies->removeElement($company);
  284.         }
  285.         return $this;
  286.     }
  287. }