vendor/symfony/security-core/User/User.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Core\User;
  11. /**
  12.  * User is the user implementation used by the in-memory user provider.
  13.  *
  14.  * This should not be used for anything else.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  */
  18. final class User implements UserInterfaceEquatableInterfaceAdvancedUserInterface
  19. {
  20.     private $username;
  21.     private $password;
  22.     private $enabled;
  23.     private $accountNonExpired;
  24.     private $credentialsNonExpired;
  25.     private $accountNonLocked;
  26.     private $roles;
  27.     private $extraFields;
  28.     public function __construct(?string $username, ?string $password, array $roles = [], bool $enabled truebool $userNonExpired truebool $credentialsNonExpired truebool $userNonLocked true, array $extraFields = [])
  29.     {
  30.         if ('' === $username || null === $username) {
  31.             throw new \InvalidArgumentException('The username cannot be empty.');
  32.         }
  33.         $this->username $username;
  34.         $this->password $password;
  35.         $this->enabled $enabled;
  36.         $this->accountNonExpired $userNonExpired;
  37.         $this->credentialsNonExpired $credentialsNonExpired;
  38.         $this->accountNonLocked $userNonLocked;
  39.         $this->roles $roles;
  40.         $this->extraFields $extraFields;
  41.     }
  42.     public function __toString(): string
  43.     {
  44.         return $this->getUsername();
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     public function getRoles(): array
  50.     {
  51.         return $this->roles;
  52.     }
  53.     /**
  54.      * {@inheritdoc}
  55.      */
  56.     public function getPassword(): ?string
  57.     {
  58.         return $this->password;
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function getSalt(): ?string
  64.     {
  65.         return null;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function getUsername(): string
  71.     {
  72.         return $this->username;
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function isAccountNonExpired(): bool
  78.     {
  79.         return $this->accountNonExpired;
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function isAccountNonLocked(): bool
  85.     {
  86.         return $this->accountNonLocked;
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      */
  91.     public function isCredentialsNonExpired(): bool
  92.     {
  93.         return $this->credentialsNonExpired;
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      */
  98.     public function isEnabled(): bool
  99.     {
  100.         return $this->enabled;
  101.     }
  102.     /**
  103.      * {@inheritdoc}
  104.      */
  105.     public function eraseCredentials()
  106.     {
  107.     }
  108.     public function getExtraFields(): array
  109.     {
  110.         return $this->extraFields;
  111.     }
  112.     /**
  113.      * {@inheritdoc}
  114.      */
  115.     public function isEqualTo(UserInterface $user): bool
  116.     {
  117.         if (!$user instanceof self) {
  118.             return false;
  119.         }
  120.         if ($this->getPassword() !== $user->getPassword()) {
  121.             return false;
  122.         }
  123.         if ($this->getSalt() !== $user->getSalt()) {
  124.             return false;
  125.         }
  126.         $currentRoles array_map('strval', (array) $this->getRoles());
  127.         $newRoles array_map('strval', (array) $user->getRoles());
  128.         $rolesChanged = \count($currentRoles) !== \count($newRoles) || \count($currentRoles) !== \count(array_intersect($currentRoles$newRoles));
  129.         if ($rolesChanged) {
  130.             return false;
  131.         }
  132.         if ($this->getUsername() !== $user->getUsername()) {
  133.             return false;
  134.         }
  135.         if ($this->isAccountNonExpired() !== $user->isAccountNonExpired()) {
  136.             return false;
  137.         }
  138.         if ($this->isAccountNonLocked() !== $user->isAccountNonLocked()) {
  139.             return false;
  140.         }
  141.         if ($this->isCredentialsNonExpired() !== $user->isCredentialsNonExpired()) {
  142.             return false;
  143.         }
  144.         if ($this->isEnabled() !== $user->isEnabled()) {
  145.             return false;
  146.         }
  147.         return true;
  148.     }
  149.     public function setPassword(string $password)
  150.     {
  151.         $this->password $password;
  152.     }
  153. }