vendor/nucleos/user-bundle/src/Model/User.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the NucleosUserBundle package.
  5.  *
  6.  * (c) Christian Gripp <mail@core23.de>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Nucleos\UserBundle\Model;
  12. use DateTime;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface;
  16. /**
  17.  * @SuppressWarnings(PHPMD.TooManyPublicMethods)
  18.  *
  19.  * @phpstan-template GroupTemplate of GroupInterface
  20.  * @phpstan-implements GroupAwareUser<GroupTemplate>
  21.  *
  22.  * @SuppressWarnings(PHPMD.TooManyPublicMethods)
  23.  */
  24. abstract class User implements UserInterfaceGroupAwareUserLocaleAwareUser
  25. {
  26.     protected ?string $username null;
  27.     protected ?string $usernameCanonical null;
  28.     protected ?string $email null;
  29.     protected ?string $emailCanonical null;
  30.     protected bool $enabled false;
  31.     protected ?string $password null;
  32.     protected ?string $plainPassword null;
  33.     protected ?DateTime $lastLogin null;
  34.     protected ?string $confirmationToken null;
  35.     protected ?DateTime $passwordRequestedAt null;
  36.     /**
  37.      * @phpstan-var Collection<array-key, GroupTemplate>
  38.      */
  39.     protected Collection $groups;
  40.     /**
  41.      * @var string[]
  42.      */
  43.     protected array $roles = [];
  44.     protected ?string $locale null;
  45.     protected ?string $timezone null;
  46.     public function __construct()
  47.     {
  48.         $this->groups = new ArrayCollection();
  49.     }
  50.     public function __toString(): string
  51.     {
  52.         return $this->getUsername();
  53.     }
  54.     public function addRole(string $role): void
  55.     {
  56.         $role strtoupper($role);
  57.         if (!\in_array($role$this->rolestrue)) {
  58.             $this->roles[] = $role;
  59.         }
  60.     }
  61.     public function eraseCredentials(): void
  62.     {
  63.         $this->plainPassword null;
  64.     }
  65.     public function getUsername(): string
  66.     {
  67.         return $this->username ?? '';
  68.     }
  69.     public function getUserIdentifier(): string
  70.     {
  71.         return $this->getUsername();
  72.     }
  73.     public function getUsernameCanonical(): string
  74.     {
  75.         return $this->usernameCanonical ?? '';
  76.     }
  77.     public function getEmail(): string
  78.     {
  79.         return $this->email ?? '';
  80.     }
  81.     public function getEmailCanonical(): string
  82.     {
  83.         return $this->emailCanonical ?? '';
  84.     }
  85.     public function getPassword(): string
  86.     {
  87.         return $this->password ?? '';
  88.     }
  89.     public function getPlainPassword(): ?string
  90.     {
  91.         return $this->plainPassword;
  92.     }
  93.     public function getLastLogin(): ?DateTime
  94.     {
  95.         return $this->lastLogin;
  96.     }
  97.     public function getConfirmationToken(): ?string
  98.     {
  99.         return $this->confirmationToken;
  100.     }
  101.     public function getRoles(): array
  102.     {
  103.         $roles $this->roles;
  104.         foreach ($this->getGroups() as $group) {
  105.             $roles array_merge($roles$group->getRoles());
  106.         }
  107.         // we need to make sure to have at least one role
  108.         $roles[] = static::ROLE_DEFAULT;
  109.         return array_values(array_unique($roles));
  110.     }
  111.     public function hasRole(string $role): bool
  112.     {
  113.         return \in_array(strtoupper($role), $this->getRoles(), true);
  114.     }
  115.     public function isAccountNonExpired(): bool
  116.     {
  117.         return true;
  118.     }
  119.     public function isAccountNonLocked(): bool
  120.     {
  121.         return true;
  122.     }
  123.     public function isCredentialsNonExpired(): bool
  124.     {
  125.         return true;
  126.     }
  127.     public function isEnabled(): bool
  128.     {
  129.         return $this->enabled;
  130.     }
  131.     public function isSuperAdmin(): bool
  132.     {
  133.         return $this->hasRole(static::ROLE_SUPER_ADMIN);
  134.     }
  135.     public function removeRole(string $role): void
  136.     {
  137.         if (false !== $key array_search(strtoupper($role), $this->rolestrue)) {
  138.             unset($this->roles[$key]);
  139.             $this->roles array_values($this->roles);
  140.         }
  141.     }
  142.     public function setUsername(string $username): void
  143.     {
  144.         $this->username $username;
  145.     }
  146.     public function setUsernameCanonical(string $usernameCanonical): void
  147.     {
  148.         $this->usernameCanonical $usernameCanonical;
  149.     }
  150.     public function setEmail(string $email): void
  151.     {
  152.         $this->email $email;
  153.     }
  154.     public function setEmailCanonical(string $emailCanonical): void
  155.     {
  156.         $this->emailCanonical $emailCanonical;
  157.     }
  158.     public function setEnabled(bool $boolean): void
  159.     {
  160.         $this->enabled $boolean;
  161.     }
  162.     public function setPassword(string $password): void
  163.     {
  164.         $this->password $password;
  165.     }
  166.     public function setSuperAdmin(bool $boolean): void
  167.     {
  168.         if ($boolean) {
  169.             $this->addRole(static::ROLE_SUPER_ADMIN);
  170.         } else {
  171.             $this->removeRole(static::ROLE_SUPER_ADMIN);
  172.         }
  173.     }
  174.     public function setPlainPassword(?string $password): void
  175.     {
  176.         $this->plainPassword $password;
  177.     }
  178.     public function setLastLogin(DateTime $time null): void
  179.     {
  180.         $this->lastLogin $time;
  181.     }
  182.     public function setConfirmationToken(?string $confirmationToken): void
  183.     {
  184.         $this->confirmationToken $confirmationToken;
  185.     }
  186.     public function setPasswordRequestedAt(DateTime $date null): void
  187.     {
  188.         $this->passwordRequestedAt $date;
  189.     }
  190.     public function getPasswordRequestedAt(): ?DateTime
  191.     {
  192.         return $this->passwordRequestedAt;
  193.     }
  194.     public function isPasswordRequestNonExpired(int $ttl): bool
  195.     {
  196.         return $this->getPasswordRequestedAt() instanceof DateTime
  197.                && $this->getPasswordRequestedAt()->getTimestamp() + $ttl time();
  198.     }
  199.     public function setRoles(array $roles): void
  200.     {
  201.         $this->roles = [];
  202.         foreach ($roles as $role) {
  203.             $this->addRole($role);
  204.         }
  205.     }
  206.     public function getGroups(): Collection
  207.     {
  208.         if (!isset($this->groups)) {
  209.             return new ArrayCollection();
  210.         }
  211.         return $this->groups;
  212.     }
  213.     public function getGroupNames(): array
  214.     {
  215.         $names = [];
  216.         foreach ($this->getGroups() as $group) {
  217.             $names[] = $group->getName();
  218.         }
  219.         return $names;
  220.     }
  221.     public function hasGroup(string $name): bool
  222.     {
  223.         return \in_array($name$this->getGroupNames(), true);
  224.     }
  225.     public function addGroup(GroupInterface $group): void
  226.     {
  227.         if (!$this->getGroups()->contains($group)) {
  228.             $this->getGroups()->add($group);
  229.         }
  230.     }
  231.     public function removeGroup(GroupInterface $group): void
  232.     {
  233.         if ($this->getGroups()->contains($group)) {
  234.             $this->getGroups()->removeElement($group);
  235.         }
  236.     }
  237.     public function setLocale(?string $locale): void
  238.     {
  239.         $this->locale $locale;
  240.     }
  241.     public function getLocale(): ?string
  242.     {
  243.         return $this->locale;
  244.     }
  245.     public function setTimezone(?string $timezone): void
  246.     {
  247.         $this->timezone $timezone;
  248.     }
  249.     public function getTimezone(): ?string
  250.     {
  251.         return $this->timezone;
  252.     }
  253.     /**
  254.      * @deprecated since symfony 5.4
  255.      *
  256.      * @return string|null
  257.      */
  258.     public function getSalt()
  259.     {
  260.         return null;
  261.     }
  262.     /**
  263.      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  264.      */
  265.     public function isEqualTo(BaseUserInterface $user): bool
  266.     {
  267.         if (!$user instanceof self) {
  268.             return false;
  269.         }
  270.         if ($this->password !== $user->getPassword()) {
  271.             return false;
  272.         }
  273.         if ($this->username !== $user->getUsername()) {
  274.             return false;
  275.         }
  276.         return true;
  277.     }
  278. }