airux

Untitled

Jul 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.47 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * This file is part of the FOSUserBundle package.
  5.  *
  6.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11.  
  12. namespace FOS\UserBundle\Model;
  13.  
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16.  
  17. /**
  18.  * Storage agnostic user object.
  19.  *
  20.  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  21.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22.  */
  23. abstract class User implements UserInterface, GroupableInterface
  24. {
  25.     /**
  26.      * @var mixed
  27.      */
  28.     protected $id;
  29.  
  30.     /**
  31.      * @var string
  32.      */
  33.     protected $username;
  34.  
  35.     /**
  36.      * @var string
  37.      */
  38.     protected $usernameCanonical;
  39.  
  40.     /**
  41.      * @var string
  42.      */
  43.     protected $email;
  44.  
  45.     /**
  46.      * @var string
  47.      */
  48.     protected $emailCanonical;
  49.  
  50.     /**
  51.      * @var bool
  52.      */
  53.     protected $enabled;
  54.  
  55.     /**
  56.      * The salt to use for hashing.
  57.      *
  58.      * @var string
  59.      */
  60.     protected $salt;
  61.  
  62.     /**
  63.      * Encrypted password. Must be persisted.
  64.      *
  65.      * @var string
  66.      */
  67.     protected $password;
  68.  
  69.     /**
  70.      * Plain password. Used for model validation. Must not be persisted.
  71.      *
  72.      * @var string
  73.      */
  74.     protected $plainPassword;
  75.  
  76.     /**
  77.      * @var \DateTime|null
  78.      */
  79.     protected $lastLogin;
  80.  
  81.     /**
  82.      * Random string sent to the user email address in order to verify it.
  83.      *
  84.      * @var string|null
  85.      */
  86.     protected $confirmationToken;
  87.  
  88.     /**
  89.      * @var \DateTime|null
  90.      */
  91.     protected $passwordRequestedAt;
  92.  
  93.     /**
  94.      * @var GroupInterface[]|Collection
  95.      */
  96.     protected $groups;
  97.  
  98.     /**
  99.      * @var array
  100.      */
  101.     protected $roles;
  102.  
  103.     /**
  104.      * User constructor.
  105.      */
  106.     public function __construct()
  107.     {
  108.         $this->enabled = false;
  109.         $this->roles = array();
  110.     }
  111.  
  112.     /**
  113.      * @return string
  114.      */
  115.     public function __toString()
  116.     {
  117.         return (string) $this->getUsername();
  118.     }
  119.  
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function addRole($role)
  124.     {
  125.         $role = strtoupper($role);
  126.         if ($role === static::ROLE_DEFAULT) {
  127.             return $this;
  128.         }
  129.  
  130.         if (!in_array($role, $this->roles, true)) {
  131.             $this->roles[] = $role;
  132.         }
  133.  
  134.         return $this;
  135.     }
  136.  
  137.     /**
  138.      * {@inheritdoc}
  139.      */
  140.     public function serialize()
  141.     {
  142.         return serialize(array(
  143.             $this->password,
  144.             $this->salt,
  145.             $this->usernameCanonical,
  146.             $this->username,
  147.             $this->enabled,
  148.             $this->id,
  149.             $this->email,
  150.             $this->emailCanonical,
  151.         ));
  152.     }
  153.  
  154.     /**
  155.      * {@inheritdoc}
  156.      */
  157.     public function unserialize($serialized)
  158.     {
  159.         $data = unserialize($serialized);
  160.  
  161.         if (13 === count($data)) {
  162.             // Unserializing a User object from 1.3.x
  163.             unset($data[4], $data[5], $data[6], $data[9], $data[10]);
  164.             $data = array_values($data);
  165.         } elseif (11 === count($data)) {
  166.             // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1
  167.             unset($data[4], $data[7], $data[8]);
  168.             $data = array_values($data);
  169.         }
  170.  
  171.         list(
  172.             $this->password,
  173.             $this->salt,
  174.             $this->usernameCanonical,
  175.             $this->username,
  176.             $this->enabled,
  177.             $this->id,
  178.             $this->email,
  179.             $this->emailCanonical
  180.         ) = $data;
  181.     }
  182.  
  183.     /**
  184.      * {@inheritdoc}
  185.      */
  186.     public function eraseCredentials()
  187.     {
  188.         $this->plainPassword = null;
  189.     }
  190.  
  191.     /**
  192.      * {@inheritdoc}
  193.      */
  194.     public function getId()
  195.     {
  196.         return $this->id;
  197.     }
  198.  
  199.     /**
  200.      * {@inheritdoc}
  201.      */
  202.     public function getUsername()
  203.     {
  204.         return $this->username;
  205.     }
  206.  
  207.     /**
  208.      * {@inheritdoc}
  209.      */
  210.     public function getUsernameCanonical()
  211.     {
  212.         return $this->usernameCanonical;
  213.     }
  214.  
  215.     /**
  216.      * {@inheritdoc}
  217.      */
  218.     public function getSalt()
  219.     {
  220.         return $this->salt;
  221.     }
  222.  
  223.     /**
  224.      * {@inheritdoc}
  225.      */
  226.     public function getEmail()
  227.     {
  228.         return $this->email;
  229.     }
  230.  
  231.     /**
  232.      * {@inheritdoc}
  233.      */
  234.     public function getEmailCanonical()
  235.     {
  236.         return $this->emailCanonical;
  237.     }
  238.  
  239.     /**
  240.      * {@inheritdoc}
  241.      */
  242.     public function getPassword()
  243.     {
  244.         return $this->password;
  245.     }
  246.  
  247.     /**
  248.      * {@inheritdoc}
  249.      */
  250.     public function getPlainPassword()
  251.     {
  252.         return $this->plainPassword;
  253.     }
  254.  
  255.     /**
  256.      * Gets the last login time.
  257.      *
  258.      * @return \DateTime|null
  259.      */
  260.     public function getLastLogin()
  261.     {
  262.         return $this->lastLogin;
  263.     }
  264.  
  265.     /**
  266.      * {@inheritdoc}
  267.      */
  268.     public function getConfirmationToken()
  269.     {
  270.         return $this->confirmationToken;
  271.     }
  272.  
  273.     /**
  274.      * {@inheritdoc}
  275.      */
  276.     public function getRoles()
  277.     {
  278.         $roles = $this->roles;
  279.  
  280.         foreach ($this->getGroups() as $group) {
  281.             $roles = array_merge($roles, $group->getRoles());
  282.         }
  283.  
  284.         // we need to make sure to have at least one role
  285.         $roles[] = static::ROLE_DEFAULT;
  286.  
  287.         return array_unique($roles);
  288.     }
  289.  
  290.     /**
  291.      * {@inheritdoc}
  292.      */
  293.     public function hasRole($role)
  294.     {
  295.         return in_array(strtoupper($role), $this->getRoles(), true);
  296.     }
  297.  
  298.     /**
  299.      * {@inheritdoc}
  300.      */
  301.     public function isAccountNonExpired()
  302.     {
  303.         return true;
  304.     }
  305.  
  306.     /**
  307.      * {@inheritdoc}
  308.      */
  309.     public function isAccountNonLocked()
  310.     {
  311.         return true;
  312.     }
  313.  
  314.     /**
  315.      * {@inheritdoc}
  316.      */
  317.     public function isCredentialsNonExpired()
  318.     {
  319.         return true;
  320.     }
  321.  
  322.     public function isEnabled()
  323.     {
  324.         return $this->enabled;
  325.     }
  326.  
  327.     /**
  328.      * {@inheritdoc}
  329.      */
  330.     public function isSuperAdmin()
  331.     {
  332.         return $this->hasRole(static::ROLE_SUPER_ADMIN);
  333.     }
  334.  
  335.     /**
  336.      * {@inheritdoc}
  337.      */
  338.     public function removeRole($role)
  339.     {
  340.         if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
  341.             unset($this->roles[$key]);
  342.             $this->roles = array_values($this->roles);
  343.         }
  344.  
  345.         return $this;
  346.     }
  347.  
  348.     /**
  349.      * {@inheritdoc}
  350.      */
  351.     public function setUsername($username)
  352.     {
  353.         $this->username = $username;
  354.  
  355.         return $this;
  356.     }
  357.  
  358.     /**
  359.      * {@inheritdoc}
  360.      */
  361.     public function setUsernameCanonical($usernameCanonical)
  362.     {
  363.         $this->usernameCanonical = $usernameCanonical;
  364.  
  365.         return $this;
  366.     }
  367.  
  368.     /**
  369.      * {@inheritdoc}
  370.      */
  371.     public function setSalt($salt)
  372.     {
  373.         $this->salt = $salt;
  374.  
  375.         return $this;
  376.     }
  377.  
  378.     /**
  379.      * {@inheritdoc}
  380.      */
  381.     public function setEmail($email)
  382.     {
  383.         $this->email = $email;
  384.  
  385.         return $this;
  386.     }
  387.  
  388.     /**
  389.      * {@inheritdoc}
  390.      */
  391.     public function setEmailCanonical($emailCanonical)
  392.     {
  393.         $this->emailCanonical = $emailCanonical;
  394.  
  395.         return $this;
  396.     }
  397.  
  398.     /**
  399.      * {@inheritdoc}
  400.      */
  401.     public function setEnabled($boolean)
  402.     {
  403.         $this->enabled = (bool) $boolean;
  404.  
  405.         return $this;
  406.     }
  407.  
  408.     /**
  409.      * {@inheritdoc}
  410.      */
  411.     public function setPassword($password)
  412.     {
  413.         $this->password = $password;
  414.  
  415.         return $this;
  416.     }
  417.  
  418.     /**
  419.      * {@inheritdoc}
  420.      */
  421.     public function setSuperAdmin($boolean)
  422.     {
  423.         if (true === $boolean) {
  424.             $this->addRole(static::ROLE_SUPER_ADMIN);
  425.         } else {
  426.             $this->removeRole(static::ROLE_SUPER_ADMIN);
  427.         }
  428.  
  429.         return $this;
  430.     }
  431.  
  432.     /**
  433.      * {@inheritdoc}
  434.      */
  435.     public function setPlainPassword($password)
  436.     {
  437.         $this->plainPassword = $password;
  438.  
  439.         return $this;
  440.     }
  441.  
  442.     /**
  443.      * {@inheritdoc}
  444.      */
  445.     public function setLastLogin(\DateTime $time = null)
  446.     {
  447.         $this->lastLogin = $time;
  448.  
  449.         return $this;
  450.     }
  451.  
  452.     /**
  453.      * {@inheritdoc}
  454.      */
  455.     public function setConfirmationToken($confirmationToken)
  456.     {
  457.         $this->confirmationToken = $confirmationToken;
  458.  
  459.         return $this;
  460.     }
  461.  
  462.     /**
  463.      * {@inheritdoc}
  464.      */
  465.     public function setPasswordRequestedAt(\DateTime $date = null)
  466.     {
  467.         $this->passwordRequestedAt = $date;
  468.  
  469.         return $this;
  470.     }
  471.  
  472.     /**
  473.      * Gets the timestamp that the user requested a password reset.
  474.      *
  475.      * @return null|\DateTime
  476.      */
  477.     public function getPasswordRequestedAt()
  478.     {
  479.         return $this->passwordRequestedAt;
  480.     }
  481.  
  482.     /**
  483.      * {@inheritdoc}
  484.      */
  485.     public function isPasswordRequestNonExpired($ttl)
  486.     {
  487.         return $this->getPasswordRequestedAt() instanceof \DateTime &&
  488.                $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time();
  489.     }
  490.  
  491.     /**
  492.      * {@inheritdoc}
  493.      */
  494.     public function setRoles(array $roles)
  495.     {
  496.         $this->roles = array();
  497.  
  498.         foreach ($roles as $role) {
  499.             $this->addRole($role);
  500.         }
  501.  
  502.         return $this;
  503.     }
  504.  
  505.     /**
  506.      * {@inheritdoc}
  507.      */
  508.     public function getGroups()
  509.     {
  510.         return $this->groups ?: $this->groups = new ArrayCollection();
  511.     }
  512.  
  513.     /**
  514.      * {@inheritdoc}
  515.      */
  516.     public function getGroupNames()
  517.     {
  518.         $names = array();
  519.         foreach ($this->getGroups() as $group) {
  520.             $names[] = $group->getName();
  521.         }
  522.  
  523.         return $names;
  524.     }
  525.  
  526.     /**
  527.      * {@inheritdoc}
  528.      */
  529.     public function hasGroup($name)
  530.     {
  531.         return in_array($name, $this->getGroupNames());
  532.     }
  533.  
  534.     /**
  535.      * {@inheritdoc}
  536.      */
  537.     public function addGroup(GroupInterface $group)
  538.     {
  539.         if (!$this->getGroups()->contains($group)) {
  540.             $this->getGroups()->add($group);
  541.         }
  542.  
  543.         return $this;
  544.     }
  545.  
  546.     /**
  547.      * {@inheritdoc}
  548.      */
  549.     public function removeGroup(GroupInterface $group)
  550.     {
  551.         if ($this->getGroups()->contains($group)) {
  552.             $this->getGroups()->removeElement($group);
  553.         }
  554.  
  555.         return $this;
  556.     }
  557. }
Add Comment
Please, Sign In to add comment