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