<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\AdminUserRepository")
* @ORM\HasLifecycleCallbacks()
* ApiResource()
*/
class AdminUser implements UserInterface, PasswordAuthenticatedUserInterface
{
public const ROLE_EASY_ADMIN = 'ROLE_ADMIN';
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=128, unique=true)
* @Assert\Length(max=128)
*/
private $username;
/**
* @ORM\Column(type="string", length=64, nullable=true)
*/
private $password;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @ORM\Column(type="boolean")
* @Assert\Type("bool")
*/
private $enabled;
/**
* User constructor.
*/
public function __construct($username)
{
$this->username = $username;
$this->enabled = true;
$this->roles = [];
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @param mixed $username
*/
public function setUsername($username): void
{
$this->username = $username;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* The public representation of the user (e.g. a username, an email address, etc.).
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_ADMIN
$roles[] = static::ROLE_EASY_ADMIN;
return array_unique($roles);
}
/**
* @return bool
*/
public function hasRole($role)
{
return in_array(strtoupper($role), $this->getRoles(), true);
}
/**
* @return $this
*/
public function addRole($role)
{
if ($role === static::ROLE_EASY_ADMIN) {
return $this;
}
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
/**
* @return $this
*/
public function removeRole($role)
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
public function eraseCredentials()
{
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function __toString(): string
{
return $this->getUsername();
}
}