Guest User

Untitled

a guest
Oct 27th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.33 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8.  
  9. /**
  10.  * User
  11.  *
  12.  * @ORM\Table(name="user")
  13.  * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
  14.  */
  15. class User implements UserInterface
  16. {
  17.     /**
  18.      * @var int
  19.      *
  20.      * @ORM\Column(name="id", type="integer")
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue(strategy="AUTO")
  23.      */
  24.     private $id;
  25.  
  26.     /**
  27.      * @var String
  28.      * @Orm\Column(name="username", type="string", length=64)
  29.      */
  30.     private $username;
  31.  
  32.     /**
  33.      * @var
  34.      * @Orm\Column(type="string", length=64)
  35.      */
  36.     private $password;
  37.  
  38.     /**
  39.      * @var
  40.      * @Orm\Column(type="string", length=32)
  41.      */
  42.     private $salt;
  43.  
  44.     /**
  45.      * @var
  46.      * @Orm\Column(type="string", length=64)
  47.      */
  48.     private $email;
  49.  
  50.     /**
  51.      * @ORM\Column(type="json_array")
  52.      */
  53.     private $roles = [];
  54.  
  55.  
  56.     /**
  57.      * Get id
  58.      *
  59.      * @return int
  60.      */
  61.     public function getId()
  62.     {
  63.         return $this->id;
  64.     }
  65.  
  66.     /**
  67.      * @return String
  68.      */
  69.     public function getUsername()
  70.     {
  71.         return $this->username;
  72.     }
  73.  
  74.     /**
  75.      * @param String $username
  76.      */
  77.     public function setUsername($username)
  78.     {
  79.         $this->username = $username;
  80.     }
  81.  
  82.     /**
  83.      * @return mixed
  84.      */
  85.     public function getRoles()
  86.     {
  87.         $roles = $this->roles;
  88.  
  89.         if (!in_array('ROLE_USER', $roles)) {
  90.             $roles[] = 'ROLE_USER';
  91.         }
  92.  
  93.         return $roles;
  94.     }
  95.  
  96.     /**
  97.      * @param mixed $roles
  98.      */
  99.     public function setRoles(array $roles)
  100.     {
  101.         $this->roles = $roles;
  102.     }
  103.  
  104.     /**
  105.      * @return mixed
  106.      */
  107.     public function getPassword()
  108.     {
  109.         return $this->password;
  110.     }
  111.  
  112.     /**
  113.      * @param mixed $password
  114.      */
  115.     public function setPassword($password)
  116.     {
  117.         $encoder = new MessageDigestPasswordEncoder('sha1');
  118.         $this->password = $encoder->encodePassword($password, $this->getSalt());
  119.     }
  120.  
  121.     /**
  122.      * @return mixed
  123.      */
  124.     public function getEmail()
  125.     {
  126.         return $this->email;
  127.     }
  128.  
  129.     /**
  130.      * @param mixed $email
  131.      */
  132.     public function setEmail($email)
  133.     {
  134.         $this->email = $email;
  135.     }
  136.  
  137.     /**
  138.      * Returns the salt that was originally used to encode the password.
  139.      *
  140.      * This can return null if the password was not encoded using a salt.
  141.      *
  142.      * @return string|null The salt
  143.      */
  144.     public function getSalt()
  145.     {
  146.  
  147.         if (null === $this->salt) {
  148.             $this->salt = md5(sprintf(
  149.                 '%s_%d_%f',
  150.                 uniqid(),
  151.                 rand(0, 99999),
  152.                 microtime(true)
  153.             ));
  154.         }
  155.  
  156.         return $this->salt;
  157.     }
  158.  
  159.     /**
  160.      * Removes sensitive data from the user.
  161.      *
  162.      * This is important if, at any given point, sensitive information like
  163.      * the plain-text password is stored on this object.
  164.      */
  165.     public function eraseCredentials()
  166.     {
  167.         return null;
  168.     }
  169.  
  170.     /**
  171.      * @param mixed $salt
  172.      */
  173.     public function setSalt($salt)
  174.     {
  175.         $this->salt = $salt;
  176.     }
  177. }
Add Comment
Please, Sign In to add comment