Advertisement
Guest User

Astrups/SpectacleBundle/Entity/User.php

a guest
Apr 19th, 2013
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Astrups\SpectacleBundle\Entity;
  4.  
  5. use \Symfony\Component\Security\Core\User\UserInterface;
  6. use \Doctrine\ORM\Mapping as ORM;
  7.  
  8. /**
  9.  * @ORM\Table(name="users")
  10.  * @ORM\Entity
  11.  */
  12. class User implements UserInterface, \Serializable
  13. {
  14.     /**
  15.      * @ORM\Column(type="integer")
  16.      * @ORM\Id
  17.      * @ORM\GeneratedValue(strategy="AUTO")
  18.      */
  19.     private $id;
  20.  
  21.     /**
  22.      * @ORM\Column(type="string", length=50, unique=true)
  23.      */
  24.     private $username;
  25.  
  26.     /**
  27.      * @ORM\Column(name="password_hash", type="string", length=40)
  28.      */
  29.     private $passwordHash;
  30.  
  31.     /**
  32.      * @ORM\Column(type="string", length=10)
  33.      */
  34.     private $salt;
  35.  
  36.     /**
  37.      * @ORM\Column(name="account_type", type="string", length=10)
  38.      */
  39.     private $accountType;
  40.  
  41.     /**
  42.      * @ORM\Column(name="full_name", type="string", length=255, nullable=true)
  43.      */
  44.     private $fullName;
  45.  
  46.     /**
  47.      * @ORM\Column(name="email", type="string", length=255, nullable=true)
  48.      */
  49.     private $email;
  50.  
  51.     public function __construct() {
  52.         $this->salt = substr(0, 10, md5(uniqid(null, true)));
  53.     }
  54.  
  55.     public function getUsername()
  56.     {
  57.         return $this->username;
  58.     }
  59.  
  60.     public function getFullName()
  61.     {
  62.         return $this->fullName;
  63.     }
  64.  
  65.     public function getPassword()
  66.     {
  67.         return $this->passwordHash;
  68.     }
  69.  
  70.     public function setPassword($password, $algo='sha1', $salt=null)
  71.     {
  72.         if(is_null($salt)) {
  73.             $salt = substr(0, 10, md5(uniqid(null, true)));
  74.         }
  75.  
  76.         $this->password = sha1($password . $salt);
  77.     }
  78.  
  79.     public function getSalt()
  80.     {
  81.         return $this->salt;
  82.     }
  83.  
  84.     public function getRoles()
  85.     {
  86.         $roles = array();
  87.  
  88.         if($this->accountType === 'user') {
  89.             $roles[] = 'ROLE_USER';
  90.         }
  91.  
  92.         if($this->accountType === 'admin') {
  93.             $roles[] = 'ROLE_ADMIN';
  94.         }
  95.  
  96.         return $roles;
  97.     }
  98.  
  99.     public function eraseCredentials()
  100.     {
  101.     }
  102.  
  103.     public function serialize()
  104.     {
  105.         return serialize(array(
  106.             $this->id, $this->username, $this->passwordHash, $this->salt
  107.         ));
  108.     }
  109.  
  110.     public function unserialize($serialized)
  111.     {
  112.         list($this->id, $this->username, $this->passwordHash, $this->salt)
  113.             = unserialize($serialized);
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement