src/Entity/AdminUser.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. /**
  9.  * @ORM\Entity(repositoryClass="App\Repository\AdminUserRepository")
  10.  * @ORM\HasLifecycleCallbacks()
  11.  * ApiResource()
  12.  */
  13. class AdminUser implements UserInterfacePasswordAuthenticatedUserInterface
  14. {
  15.     public const ROLE_EASY_ADMIN 'ROLE_ADMIN';
  16.     /**
  17.      * @ORM\Id()
  18.      * @ORM\GeneratedValue()
  19.      * @ORM\Column(type="integer")
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\Column(type="string", length=128, unique=true)
  24.      * @Assert\Length(max=128)
  25.      */
  26.     private $username;
  27.     /**
  28.      * @ORM\Column(type="string", length=64, nullable=true)
  29.      */
  30.     private $password;
  31.     /**
  32.      * @ORM\Column(type="json")
  33.      */
  34.     private $roles = [];
  35.     /**
  36.      * @ORM\Column(type="boolean")
  37.      * @Assert\Type("bool")
  38.      */
  39.     private $enabled;
  40.     /**
  41.      * User constructor.
  42.      */
  43.     public function __construct($username)
  44.     {
  45.         $this->username $username;
  46.         $this->enabled true;
  47.         $this->roles = [];
  48.     }
  49.     public function getId(): ?int
  50.     {
  51.         return $this->id;
  52.     }
  53.     /**
  54.      * @return string
  55.      */
  56.     public function getUsername()
  57.     {
  58.         return $this->username;
  59.     }
  60.     /**
  61.      * @param mixed $username
  62.      */
  63.     public function setUsername($username): void
  64.     {
  65.         $this->username $username;
  66.     }
  67.     /**
  68.      * @see PasswordAuthenticatedUserInterface
  69.      */
  70.     public function getPassword(): string
  71.     {
  72.         return $this->password;
  73.     }
  74.     public function setPassword(string $password): self
  75.     {
  76.         $this->password $password;
  77.         return $this;
  78.     }
  79.     /**
  80.      * Returning a salt is only needed, if you are not using a modern
  81.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  82.      *
  83.      * @see UserInterface
  84.      */
  85.     public function getSalt(): ?string
  86.     {
  87.         return null;
  88.     }
  89.     /**
  90.      * The public representation of the user (e.g. a username, an email address, etc.).
  91.      *
  92.      * @see UserInterface
  93.      */
  94.     public function getUserIdentifier(): string
  95.     {
  96.         return (string) $this->username;
  97.     }
  98.     /**
  99.      * @see UserInterface
  100.      */
  101.     public function getRoles(): array
  102.     {
  103.         $roles $this->roles;
  104.         // guarantee every user at least has ROLE_ADMIN
  105.         $roles[] = static::ROLE_EASY_ADMIN;
  106.         return array_unique($roles);
  107.     }
  108.     /**
  109.      * @return bool
  110.      */
  111.     public function hasRole($role)
  112.     {
  113.         return in_array(strtoupper($role), $this->getRoles(), true);
  114.     }
  115.     /**
  116.      * @return $this
  117.      */
  118.     public function addRole($role)
  119.     {
  120.         if ($role === static::ROLE_EASY_ADMIN) {
  121.             return $this;
  122.         }
  123.         if (!in_array($role$this->rolestrue)) {
  124.             $this->roles[] = $role;
  125.         }
  126.         return $this;
  127.     }
  128.     /**
  129.      * @return $this
  130.      */
  131.     public function removeRole($role)
  132.     {
  133.         if (false !== $key array_search(strtoupper($role), $this->rolestrue)) {
  134.             unset($this->roles[$key]);
  135.             $this->roles array_values($this->roles);
  136.         }
  137.         return $this;
  138.     }
  139.     public function eraseCredentials()
  140.     {
  141.     }
  142.     public function getEnabled(): ?bool
  143.     {
  144.         return $this->enabled;
  145.     }
  146.     public function setEnabled(bool $enabled): self
  147.     {
  148.         $this->enabled $enabled;
  149.         return $this;
  150.     }
  151.     public function __toString(): string
  152.     {
  153.         return $this->getUsername();
  154.     }
  155. }