vendor/sylius/sylius/src/Sylius/Component/User/Model/User.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Component\User\Model;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Sylius\Component\Resource\Model\TimestampableTrait;
  15. use Sylius\Component\Resource\Model\ToggleableTrait;
  16. class User implements UserInterface
  17. {
  18.     use TimestampableTraitToggleableTrait;
  19.     /** @var mixed */
  20.     protected $id;
  21.     /**
  22.      * @var string|null
  23.      */
  24.     protected $username;
  25.     /**
  26.      * Normalized representation of a username.
  27.      * @var string|null
  28.      */
  29.     protected $usernameCanonical;
  30.     /**
  31.      * Random data that is used as an additional input to a function that hashes a password.
  32.      * @var string
  33.      */
  34.     protected $salt;
  35.     /**
  36.      * Encrypted password. Must be persisted.
  37.      * @var string|null
  38.      */
  39.     protected $password;
  40.     /**
  41.      * Password before encryption. Used for model validation. Must not be persisted.
  42.      * @var string|null
  43.      */
  44.     protected $plainPassword;
  45.     /**
  46.      * @var \DateTimeInterface|null
  47.      */
  48.     protected $lastLogin;
  49.     /**
  50.      * Random string sent to the user email address in order to verify it
  51.      * @var string|null
  52.      */
  53.     protected $emailVerificationToken;
  54.     /**
  55.      * Random string sent to the user email address in order to verify the password resetting request
  56.      * @var string|null
  57.      */
  58.     protected $passwordResetToken;
  59.     /**
  60.      * @var \DateTimeInterface|null
  61.      */
  62.     protected $passwordRequestedAt;
  63.     /**
  64.      * @var \DateTimeInterface|null
  65.      */
  66.     protected $verifiedAt;
  67.     /**
  68.      * @var bool
  69.      */
  70.     protected $locked false;
  71.     /**
  72.      * @var \DateTimeInterface|null
  73.      */
  74.     protected $expiresAt;
  75.     /**
  76.      * @var \DateTimeInterface|null
  77.      */
  78.     protected $credentialsExpireAt;
  79.     /**
  80.      * We need at least one role to be able to authenticate
  81.      * @var mixed[]
  82.      */
  83.     protected $roles = [UserInterface::DEFAULT_ROLE];
  84.     /**
  85.      * @var Collection|UserOAuthInterface[]
  86.      *
  87.      * @psalm-var Collection<array-key, UserOAuthInterface>
  88.      */
  89.     protected $oauthAccounts;
  90.     /**
  91.      * @var string|null
  92.      */
  93.     protected $email;
  94.     /**
  95.      * @var string|null
  96.      */
  97.     protected $emailCanonical;
  98.     /**
  99.      * @var string|null
  100.      */
  101.     protected $encoderName;
  102.     public function __construct()
  103.     {
  104.         $this->salt base_convert(bin2hex(random_bytes(20)), 1636);
  105.         /** @var ArrayCollection<array-key, UserOAuthInterface> $this->oauthAccounts */
  106.         $this->oauthAccounts = new ArrayCollection();
  107.         $this->createdAt = new \DateTime();
  108.         // Set here to overwrite default value from trait
  109.         $this->enabled false;
  110.     }
  111.     /** @psalm-suppress RedundantCastGivenDocblockType */
  112.     public function __toString(): string
  113.     {
  114.         return (string) $this->getUsername();
  115.     }
  116.     public function getId()
  117.     {
  118.         return $this->id;
  119.     }
  120.     public function getEmail(): ?string
  121.     {
  122.         return $this->email;
  123.     }
  124.     public function setEmail(?string $email): void
  125.     {
  126.         $this->email $email;
  127.     }
  128.     public function getEmailCanonical(): ?string
  129.     {
  130.         return $this->emailCanonical;
  131.     }
  132.     public function setEmailCanonical(?string $emailCanonical): void
  133.     {
  134.         $this->emailCanonical $emailCanonical;
  135.     }
  136.     public function getUsername(): ?string
  137.     {
  138.         return $this->username;
  139.     }
  140.     public function setUsername(?string $username): void
  141.     {
  142.         $this->username $username;
  143.     }
  144.     public function getUsernameCanonical(): ?string
  145.     {
  146.         return $this->usernameCanonical;
  147.     }
  148.     public function setUsernameCanonical(?string $usernameCanonical): void
  149.     {
  150.         $this->usernameCanonical $usernameCanonical;
  151.     }
  152.     public function getSalt(): string
  153.     {
  154.         return $this->salt;
  155.     }
  156.     public function getPlainPassword(): ?string
  157.     {
  158.         return $this->plainPassword;
  159.     }
  160.     public function setPlainPassword(?string $password): void
  161.     {
  162.         $this->plainPassword $password;
  163.     }
  164.     public function getPassword(): ?string
  165.     {
  166.         return $this->password;
  167.     }
  168.     public function setPassword(?string $password): void
  169.     {
  170.         $this->password $password;
  171.     }
  172.     public function getExpiresAt(): ?\DateTimeInterface
  173.     {
  174.         return $this->expiresAt;
  175.     }
  176.     public function setExpiresAt(?\DateTimeInterface $date): void
  177.     {
  178.         $this->expiresAt $date;
  179.     }
  180.     public function getCredentialsExpireAt(): ?\DateTimeInterface
  181.     {
  182.         return $this->credentialsExpireAt;
  183.     }
  184.     public function setCredentialsExpireAt(?\DateTimeInterface $date): void
  185.     {
  186.         $this->credentialsExpireAt $date;
  187.     }
  188.     public function getLastLogin(): ?\DateTimeInterface
  189.     {
  190.         return $this->lastLogin;
  191.     }
  192.     public function setLastLogin(?\DateTimeInterface $time): void
  193.     {
  194.         $this->lastLogin $time;
  195.     }
  196.     public function getEmailVerificationToken(): ?string
  197.     {
  198.         return $this->emailVerificationToken;
  199.     }
  200.     public function setEmailVerificationToken(?string $verificationToken): void
  201.     {
  202.         $this->emailVerificationToken $verificationToken;
  203.     }
  204.     public function getPasswordResetToken(): ?string
  205.     {
  206.         return $this->passwordResetToken;
  207.     }
  208.     public function setPasswordResetToken(?string $passwordResetToken): void
  209.     {
  210.         $this->passwordResetToken $passwordResetToken;
  211.     }
  212.     public function isCredentialsNonExpired(): bool
  213.     {
  214.         return !$this->hasExpired($this->credentialsExpireAt);
  215.     }
  216.     public function isAccountNonExpired(): bool
  217.     {
  218.         return !$this->hasExpired($this->expiresAt);
  219.     }
  220.     public function setLocked(bool $locked): void
  221.     {
  222.         $this->locked $locked;
  223.     }
  224.     public function isAccountNonLocked(): bool
  225.     {
  226.         return !$this->locked;
  227.     }
  228.     public function hasRole(string $role): bool
  229.     {
  230.         return in_array(strtoupper($role), $this->getRoles(), true);
  231.     }
  232.     public function addRole(string $role): void
  233.     {
  234.         $role strtoupper($role);
  235.         if (!in_array($role$this->rolestrue)) {
  236.             $this->roles[] = $role;
  237.         }
  238.     }
  239.     public function removeRole(string $role): void
  240.     {
  241.         if (false !== $key array_search(strtoupper($role), $this->rolestrue)) {
  242.             unset($this->roles[$key]);
  243.             $this->roles array_values($this->roles);
  244.         }
  245.     }
  246.     public function getRoles(): array
  247.     {
  248.         return $this->roles;
  249.     }
  250.     public function isPasswordRequestNonExpired(\DateInterval $ttl): bool
  251.     {
  252.         if (null === $this->passwordRequestedAt) {
  253.             return false;
  254.         }
  255.         $threshold = new \DateTime();
  256.         $threshold->sub($ttl);
  257.         return $threshold <= $this->passwordRequestedAt;
  258.     }
  259.     public function getPasswordRequestedAt(): ?\DateTimeInterface
  260.     {
  261.         return $this->passwordRequestedAt;
  262.     }
  263.     public function setPasswordRequestedAt(?\DateTimeInterface $date): void
  264.     {
  265.         $this->passwordRequestedAt $date;
  266.     }
  267.     public function isVerified(): bool
  268.     {
  269.         return null !== $this->verifiedAt;
  270.     }
  271.     public function getVerifiedAt(): ?\DateTimeInterface
  272.     {
  273.         return $this->verifiedAt;
  274.     }
  275.     public function setVerifiedAt(?\DateTimeInterface $verifiedAt): void
  276.     {
  277.         $this->verifiedAt $verifiedAt;
  278.     }
  279.     public function eraseCredentials(): void
  280.     {
  281.         $this->plainPassword null;
  282.     }
  283.     public function getOAuthAccounts(): Collection
  284.     {
  285.         return $this->oauthAccounts;
  286.     }
  287.     public function getOAuthAccount(string $provider): ?UserOAuthInterface
  288.     {
  289.         if ($this->oauthAccounts->isEmpty()) {
  290.             return null;
  291.         }
  292.         $filtered $this->oauthAccounts->filter(function (UserOAuthInterface $oauth) use ($provider): bool {
  293.             return $provider === $oauth->getProvider();
  294.         });
  295.         if ($filtered->isEmpty()) {
  296.             return null;
  297.         }
  298.         return $filtered->current();
  299.     }
  300.     public function addOAuthAccount(UserOAuthInterface $oauth): void
  301.     {
  302.         if (!$this->oauthAccounts->contains($oauth)) {
  303.             $this->oauthAccounts->add($oauth);
  304.             $oauth->setUser($this);
  305.         }
  306.     }
  307.     public function getEncoderName(): ?string
  308.     {
  309.         return $this->encoderName;
  310.     }
  311.     public function setEncoderName(?string $encoderName): void
  312.     {
  313.         $this->encoderName $encoderName;
  314.     }
  315.     /**
  316.      * The serialized data have to contain the fields used by the equals method and the username.
  317.      */
  318.     public function serialize(): string
  319.     {
  320.         return serialize([
  321.             $this->password,
  322.             $this->salt,
  323.             $this->usernameCanonical,
  324.             $this->username,
  325.             $this->locked,
  326.             $this->enabled,
  327.             $this->id,
  328.             $this->encoderName,
  329.         ]);
  330.     }
  331.     /**
  332.      * @param string $serialized
  333.      */
  334.     public function unserialize($serialized): void
  335.     {
  336.         $data unserialize($serialized);
  337.         // add a few extra elements in the array to ensure that we have enough keys when unserializing
  338.         // older data which does not include all properties.
  339.         $data array_merge($dataarray_fill(02null));
  340.         [
  341.             $this->password,
  342.             $this->salt,
  343.             $this->usernameCanonical,
  344.             $this->username,
  345.             $this->locked,
  346.             $this->enabled,
  347.             $this->id,
  348.             $this->encoderName,
  349.         ] = $data;
  350.     }
  351.     protected function hasExpired(?\DateTimeInterface $date): bool
  352.     {
  353.         return null !== $date && new \DateTime() >= $date;
  354.     }
  355. }