src/Modules/User/Entity/User.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Modules\User\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Security\Core\User\UserInterface;
  5. /**
  6.  * @ORM\Table(name="user", indexes={@ORM\Index(name="type", columns={"type"}), @ORM\Index(name="is_active", columns={"is_active"})})
  7.  * @ORM\Entity(repositoryClass="App\Modules\User\Repository\UserRepository")
  8.  */
  9. class User implements UserInterface\Serializable
  10. {
  11.     const TYPE_ADMIN 1;
  12.     const TYPE_USER 2;
  13.     /**
  14.      * @ORM\Column(type="integer")
  15.      * @ORM\Id
  16.      * @ORM\GeneratedValue(strategy="AUTO")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\Column(type="string", length=255, unique=true)
  21.      */
  22.     private $username;
  23.     /**
  24.      * @ORM\Column(type="string", length=64)
  25.      */
  26.     private $password;
  27.     /**
  28.      * @ORM\Column(type="string", length=255, unique=true)
  29.      */
  30.     private $email;
  31.     /**
  32.      * @var int
  33.      *
  34.      * @ORM\Column(name="type", type="smallint", nullable=false)
  35.      */
  36.     private $type;
  37.  
  38.     /**
  39.      * @ORM\Column(name="is_active", type="boolean")
  40.      */
  41.     private $isActive;
  42.     public function __construct()
  43.     {
  44.         $this->isActive false;
  45.         // may not be needed, see section on salt below
  46.         // $this->salt = md5(uniqid('', true));
  47.     }
  48.     /** @see \Serializable::serialize() */
  49.     public function serialize()
  50.     {
  51.         return serialize(array(
  52.             $this->id,
  53.             $this->username,
  54.             $this->password,
  55.             // see section on salt below
  56.             // $this->salt,
  57.         ));
  58.     }
  59.     /** @see \Serializable::unserialize() */
  60.     public function unserialize($serialized)
  61.     {
  62.         list (
  63.             $this->id,
  64.             $this->username,
  65.             $this->password,
  66.             // see section on salt below
  67.             // $this->salt
  68.         ) = unserialize($serialized, array('allowed_classes' => false));
  69.     }
  70.     public function getId(): ?int
  71.     {
  72.         return $this->id;
  73.     }
  74.     public function getEmail(): ?string
  75.     {
  76.         return $this->email;
  77.     }
  78.     public function setEmail(string $email): self
  79.     {
  80.         $this->email $email;
  81.         return $this;
  82.     }
  83.     public function getIsActive(): ?bool
  84.     {
  85.         return $this->isActive;
  86.     }
  87.     public function setIsActive(bool $isActive): self
  88.     {
  89.         $this->isActive $isActive;
  90.         return $this;
  91.     }
  92.     public function setUsername(string $username): self
  93.     {
  94.         $this->username $username;
  95.         return $this;
  96.     }
  97.     public function getUsername()
  98.     {
  99.         return $this->username;
  100.     }
  101.     public function getSalt()
  102.     {
  103.         // you *may* need a real salt depending on your encoder
  104.         // see section on salt below
  105.         return null;
  106.     }
  107.     public function getPassword()
  108.     {
  109.         return $this->password;
  110.     }
  111.     public function setPassword(string $password): self
  112.     {
  113.         $this->password $password;
  114.         return $this;
  115.     }
  116.     public function getRoles()
  117.     {
  118.         switch($this->type)
  119.         {
  120.             case self::TYPE_ADMIN:
  121.                 return ['ROLE_ADMIN'];
  122.             case self::TYPE_USER:
  123.                 return ['ROLE_USER'];
  124.             default:
  125.                 throw new \Exception('Unknown user.');
  126.         }
  127.     }
  128.     public function eraseCredentials()
  129.     {
  130.     }
  131.     public function getType(): ?int
  132.     {
  133.         return $this->type;
  134.     }
  135.     public function setType(int $type): self
  136.     {
  137.         $this->type $type;
  138.         return $this;
  139.     }
  140. }