<?php
namespace App\Modules\User\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="user", indexes={@ORM\Index(name="type", columns={"type"}), @ORM\Index(name="is_active", columns={"is_active"})})
* @ORM\Entity(repositoryClass="App\Modules\User\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
const TYPE_ADMIN = 1;
const TYPE_USER = 2;
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $email;
/**
* @var int
*
* @ORM\Column(name="type", type="smallint", nullable=false)
*/
private $type;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = false;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid('', true));
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized, array('allowed_classes' => false));
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getRoles()
{
switch($this->type)
{
case self::TYPE_ADMIN:
return ['ROLE_ADMIN'];
case self::TYPE_USER:
return ['ROLE_USER'];
default:
throw new \Exception('Unknown user.');
}
}
public function eraseCredentials()
{
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
}