<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Traits\CreatedUpdatedTrait;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\ExclusionPolicy;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="v_admin")
* @ORM\Entity(repositoryClass="App\Repository\AdminRepository")
* @ExclusionPolicy("all")
*/
class Admin implements UserInterface, EquatableInterface
{
public function isEqualTo(UserInterface $user)
{
return ($user->getUsername() == $this->email && $user->getPassword() == $this->password);
}
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Expose
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Expose
*/
private $name;
/**
* @ORM\Column(type="string", length=255, unique=true)
* @Expose
*/
private $email;
/**
* @ORM\Column(type="string", length=80, nullable=true)
* @Expose
*/
private $phone;
/**
* @var string[]
* @ORM\Column(type="json", nullable=true)
*/
private $roles;
/**
* @var string Salt
* @ORM\Column(type="string")
*/
private $salt;
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* Plain password. Used for model validation. Must not be persisted.
* @var string
*/
private $plainPassword;
/**
* @var boolean
* @ORM\Column(type="boolean")
* @Expose
*/
private $confirmed = true;
/**
* @var boolean
* @ORM\Column(type="boolean")
* @Expose
*/
private $enabled = true;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Group")
*/
protected $groups;
use CreatedUpdatedTrait;
public function __construct()
{
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
}
public function __toString()
{
return (string) $this->name;
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name): void
{
$this->name = $name;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* @return mixed
*/
public function getPhone()
{
return $this->phone;
}
/**
* @param mixed $phone
*/
public function setPhone($phone): void
{
$this->phone = $phone;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
foreach ($this->getGroups() as $group)
{
if(count($group->getRoles()) > 0) {
foreach ($group->getRoles() as $role) {
$roles[] = $role;
}
}
}
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function addRole($role)
{
$role = strtoupper($role);
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @return string
*/
public function getPlainPassword(): ?string
{
return $this->plainPassword;
}
/**
* @param string $plainPassword
*/
public function setPlainPassword(string $plainPassword): void
{
$this->plainPassword = $plainPassword;
}
public function getSalt(): ?string
{
return $this->salt;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* @return bool
*/
public function isConfirmed(): bool
{
return $this->confirmed;
}
/**
* @param bool $confirmed
*/
public function setConfirmed(bool $confirmed): void
{
$this->confirmed = $confirmed;
}
/**
* @return bool
*/
public function isEnabled(): bool
{
return $this->enabled;
}
/**
* @param bool $enabled
*/
public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}
/**
* @return mixed
*/
public function getGroups()
{
return $this->groups;
}
/**
* @param mixed $groups
*/
public function setGroups($groups): void
{
$this->groups = $groups;
}
}