vendor/friendsofsymfony/user-bundle/Model/User.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSUserBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Model;
  11. use Symfony\Component\Security\Core\User\EquatableInterface;
  12. use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface;
  13. /**
  14.  * Storage agnostic user object.
  15.  *
  16.  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  17.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18.  */
  19. abstract class User implements UserInterfaceEquatableInterface, \Serializable
  20. {
  21.     /**
  22.      * @var mixed
  23.      */
  24.     protected $id;
  25.     /**
  26.      * @var string
  27.      */
  28.     protected $username;
  29.     /**
  30.      * @var string
  31.      */
  32.     protected $usernameCanonical;
  33.     /**
  34.      * @var string
  35.      */
  36.     protected $email;
  37.     /**
  38.      * @var string
  39.      */
  40.     protected $emailCanonical;
  41.     /**
  42.      * @var bool
  43.      */
  44.     protected $enabled;
  45.     /**
  46.      * The salt to use for hashing.
  47.      *
  48.      * @var string
  49.      */
  50.     protected $salt;
  51.     /**
  52.      * Encrypted password. Must be persisted.
  53.      *
  54.      * @var string
  55.      */
  56.     protected $password;
  57.     /**
  58.      * Plain password. Used for model validation. Must not be persisted.
  59.      *
  60.      * @var string|null
  61.      */
  62.     protected $plainPassword;
  63.     /**
  64.      * @var \DateTime|null
  65.      */
  66.     protected $lastLogin;
  67.     /**
  68.      * Random string sent to the user email address in order to verify it.
  69.      *
  70.      * @var string|null
  71.      */
  72.     protected $confirmationToken;
  73.     /**
  74.      * @var \DateTime|null
  75.      */
  76.     protected $passwordRequestedAt;
  77.     /**
  78.      * @var array
  79.      */
  80.     protected $roles;
  81.     /**
  82.      *
  83.      * @var string|null
  84.      */
  85.     protected $avatar;
  86.     /**
  87.      * User constructor.
  88.      */
  89.     public function __construct()
  90.     {
  91.         $this->enabled false;
  92.         $this->roles = [];
  93.     }
  94.     /**
  95.      * @return string
  96.      */
  97.     public function __toString()
  98.     {
  99.         return (string) $this->getUsername();
  100.     }
  101.     public function __serialize(): array
  102.     {
  103.         return [
  104.             $this->password,
  105.             $this->salt,
  106.             $this->usernameCanonical,
  107.             $this->username,
  108.             $this->enabled,
  109.             $this->id,
  110.             $this->email,
  111.             $this->emailCanonical,
  112.         ];
  113.     }
  114.     public function __unserialize(array $data): void
  115.     {
  116.         if (13 === count($data)) {
  117.             // Unserializing a User object from 1.3.x
  118.             unset($data[4], $data[5], $data[6], $data[9], $data[10]);
  119.             $data array_values($data);
  120.         } elseif (11 === count($data)) {
  121.             // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
  122.             unset($data[4], $data[7], $data[8]);
  123.             $data array_values($data);
  124.         }
  125.         list(
  126.             $this->password,
  127.             $this->salt,
  128.             $this->usernameCanonical,
  129.             $this->username,
  130.             $this->enabled,
  131.             $this->id,
  132.             $this->email,
  133.             $this->emailCanonical
  134.             ) = $data;
  135.     }
  136.     /**
  137.      * @internal
  138.      */
  139.     final public function serialize()
  140.     {
  141.         return serialize($this->__serialize());
  142.     }
  143.     /**
  144.      * @internal
  145.      */
  146.     final public function unserialize($serialized)
  147.     {
  148.         $this->__unserialize(unserialize($serialized));
  149.     }
  150.     /**
  151.      * {@inheritdoc}
  152.      */
  153.     public function addRole($role)
  154.     {
  155.         $role strtoupper($role);
  156.         if ($role === static::ROLE_DEFAULT) {
  157.             return $this;
  158.         }
  159.         if (!in_array($role$this->rolestrue)) {
  160.             $this->roles[] = $role;
  161.         }
  162.         return $this;
  163.     }
  164.     /**
  165.      * {@inheritdoc}
  166.      */
  167.     public function eraseCredentials()
  168.     {
  169.         $this->plainPassword null;
  170.     }
  171.     /**
  172.      * {@inheritdoc}
  173.      */
  174.     public function getId()
  175.     {
  176.         return $this->id;
  177.     }
  178.     public function getUserIdentifier(): string
  179.     {
  180.         return $this->username;
  181.     }
  182.     /**
  183.      * {@inheritdoc}
  184.      *
  185.      * @return string
  186.      */
  187.     public function getUsername()
  188.     {
  189.         return $this->username;
  190.     }
  191.     /**
  192.      * {@inheritdoc}
  193.      */
  194.     public function getUsernameCanonical()
  195.     {
  196.         return $this->usernameCanonical;
  197.     }
  198.     /**
  199.      * {@inheritdoc}
  200.      */
  201.     public function getSalt(): ?string
  202.     {
  203.         return $this->salt;
  204.     }
  205.     /**
  206.      * {@inheritdoc}
  207.      */
  208.     public function getEmail()
  209.     {
  210.         return $this->email;
  211.     }
  212.     /**
  213.      * {@inheritdoc}
  214.      */
  215.     public function getEmailCanonical()
  216.     {
  217.         return $this->emailCanonical;
  218.     }
  219.     /**
  220.      * {@inheritdoc}
  221.      */
  222.     public function getPassword(): ?string
  223.     {
  224.         return $this->password;
  225.     }
  226.     /**
  227.      * {@inheritdoc}
  228.      */
  229.     public function getPlainPassword()
  230.     {
  231.         return $this->plainPassword;
  232.     }
  233.     /**
  234.      * Gets the last login time.
  235.      *
  236.      * @return \DateTime|null
  237.      */
  238.     public function getLastLogin()
  239.     {
  240.         return $this->lastLogin;
  241.     }
  242.     /**
  243.      * {@inheritdoc}
  244.      */
  245.     public function getConfirmationToken()
  246.     {
  247.         return $this->confirmationToken;
  248.     }
  249.     /**
  250.      * {@inheritdoc}
  251.      */
  252.     public function getRoles(): array
  253.     {
  254.         $roles $this->roles;
  255.         // we need to make sure to have at least one role
  256.         $roles[] = static::ROLE_DEFAULT;
  257.         return array_values(array_unique($roles));
  258.     }
  259.     /**
  260.      * {@inheritdoc}
  261.      */
  262.     public function hasRole($role)
  263.     {
  264.         return in_array(strtoupper($role), $this->getRoles(), true);
  265.     }
  266.     public function isEnabled()
  267.     {
  268.         return $this->enabled;
  269.     }
  270.     /**
  271.      * {@inheritdoc}
  272.      */
  273.     public function isSuperAdmin()
  274.     {
  275.         return $this->hasRole(static::ROLE_SUPER_ADMIN);
  276.     }
  277.     /**
  278.      * {@inheritdoc}
  279.      */
  280.     public function removeRole($role)
  281.     {
  282.         if (false !== $key array_search(strtoupper($role), $this->rolestrue)) {
  283.             unset($this->roles[$key]);
  284.             $this->roles array_values($this->roles);
  285.         }
  286.         return $this;
  287.     }
  288.     /**
  289.      * {@inheritdoc}
  290.      */
  291.     public function setUsername($username)
  292.     {
  293.         $this->username $username;
  294.         return $this;
  295.     }
  296.     /**
  297.      * {@inheritdoc}
  298.      */
  299.     public function setUsernameCanonical($usernameCanonical)
  300.     {
  301.         $this->usernameCanonical $usernameCanonical;
  302.         return $this;
  303.     }
  304.     /**
  305.      * {@inheritdoc}
  306.      */
  307.     public function setSalt($salt)
  308.     {
  309.         $this->salt $salt;
  310.         return $this;
  311.     }
  312.     /**
  313.      * {@inheritdoc}
  314.      */
  315.     public function setEmail($email)
  316.     {
  317.         $this->email $email;
  318.         return $this;
  319.     }
  320.     /**
  321.      * {@inheritdoc}
  322.      */
  323.     public function setEmailCanonical($emailCanonical)
  324.     {
  325.         $this->emailCanonical $emailCanonical;
  326.         return $this;
  327.     }
  328.     /**
  329.      * {@inheritdoc}
  330.      */
  331.     public function setEnabled($boolean)
  332.     {
  333.         $this->enabled = (bool) $boolean;
  334.         return $this;
  335.     }
  336.     /**
  337.      * {@inheritdoc}
  338.      */
  339.     public function setPassword($password)
  340.     {
  341.         $this->password $password;
  342.         return $this;
  343.     }
  344.     /**
  345.      * {@inheritdoc}
  346.      */
  347.     public function setSuperAdmin($boolean)
  348.     {
  349.         if (true === $boolean) {
  350.             $this->addRole(static::ROLE_SUPER_ADMIN);
  351.         } else {
  352.             $this->removeRole(static::ROLE_SUPER_ADMIN);
  353.         }
  354.         return $this;
  355.     }
  356.     /**
  357.      * {@inheritdoc}
  358.      */
  359.     public function setPlainPassword($password)
  360.     {
  361.         $this->plainPassword $password;
  362.         return $this;
  363.     }
  364.     /**
  365.      * {@inheritdoc}
  366.      */
  367.     public function setLastLogin(\DateTime $time null)
  368.     {
  369.         $this->lastLogin $time;
  370.         return $this;
  371.     }
  372.     /**
  373.      * {@inheritdoc}
  374.      */
  375.     public function setConfirmationToken($confirmationToken)
  376.     {
  377.         $this->confirmationToken $confirmationToken;
  378.         return $this;
  379.     }
  380.     /**
  381.      * {@inheritdoc}
  382.      */
  383.     public function setPasswordRequestedAt(\DateTime $date null)
  384.     {
  385.         $this->passwordRequestedAt $date;
  386.         return $this;
  387.     }
  388.     /**
  389.      * Gets the timestamp that the user requested a password reset.
  390.      *
  391.      * @return \DateTime|null
  392.      */
  393.     public function getPasswordRequestedAt()
  394.     {
  395.         return $this->passwordRequestedAt;
  396.     }
  397.     /**
  398.      * {@inheritdoc}
  399.      */
  400.     public function isPasswordRequestNonExpired($ttl)
  401.     {
  402.         return $this->getPasswordRequestedAt() instanceof \DateTime &&
  403.                $this->getPasswordRequestedAt()->getTimestamp() + $ttl time();
  404.     }
  405.     /**
  406.      * {@inheritdoc}
  407.      */
  408.     public function setRoles(array $roles)
  409.     {
  410.         $this->roles = [];
  411.         foreach ($roles as $role) {
  412.             $this->addRole($role);
  413.         }
  414.         return $this;
  415.     }
  416.     /**
  417.      * {@inheritdoc}
  418.      */
  419.     public function isEqualTo(BaseUserInterface $user): bool
  420.     {
  421.         if (!$user instanceof self) {
  422.             return false;
  423.         }
  424.         if ($this->password !== $user->getPassword()) {
  425.             return false;
  426.         }
  427.         if ($this->salt !== $user->getSalt()) {
  428.             return false;
  429.         }
  430.         if ($this->username !== $user->getUsername()) {
  431.             return false;
  432.         }
  433.         return true;
  434.     }
  435. }