src/Entity/User.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Uid\Uuid;
  10. /**
  11.  * @ORM\Entity(repositoryClass=UserRepository::class)
  12.  * @ORM\Table(name="t_admin_user")
  13.  */
  14. class User implements UserInterfacePasswordAuthenticatedUserInterface
  15. {
  16.     /**
  17.      * @ORM\Id()
  18.      * @ORM\GeneratedValue(strategy="AUTO")
  19.      * @ORM\Column(type="integer")
  20.      */
  21.     private ?int $id null;
  22.     /**
  23.      * @var string|null
  24.      * @ORM\Column(nullable=true)
  25.      */
  26.     private ?string $token null;
  27.     /**
  28.      * @ORM\Column(type="string", length=50, unique=true)
  29.      */
  30.     private ?string $username;
  31.     /**
  32.      * @var string|null The hashed password
  33.      * @ORM\Column(type="string")
  34.      */
  35.     private ?string $password;
  36.     /**
  37.      * @ORM\OneToMany(targetEntity=Role::class, mappedBy="users")
  38.      */
  39.     private Collection $roles;
  40.     public function __construct()
  41.     {
  42.         $this->token Uuid::v4();
  43.         $this->roles = new ArrayCollection();
  44.     }
  45.     /**
  46.      * @return int|null
  47.      */
  48.     public function getId(): ?int
  49.     {
  50.         return $this->id;
  51.     }
  52.     /**
  53.      * @return string|null
  54.      */
  55.     public function getToken(): ?string
  56.     {
  57.         return $this->token;
  58.     }
  59.     /**
  60.      * @param string|null $token
  61.      */
  62.     public function setToken(?string $token): void
  63.     {
  64.         $this->token $token;
  65.     }
  66.     /**
  67.      * A visual identifier that represents this user.
  68.      *
  69.      * @see UserInterface
  70.      */
  71.     public function getUsername(): string
  72.     {
  73.         return (string)$this->username;
  74.     }
  75.     /**
  76.      * @param string $username
  77.      * @return $this
  78.      */
  79.     public function setUsername(string $username): self
  80.     {
  81.         $this->username $username;
  82.         return $this;
  83.     }
  84.     /**
  85.      * @see UserInterface
  86.      */
  87.     public function getRoles(): array
  88.     {
  89.         $roles array_map(static function (Role $role){
  90.             return $role->getName();
  91.         },$this->roles->toArray());
  92.         return array_unique($roles);
  93.     }
  94.     /**
  95.      * @param array $roles
  96.      * @return $this
  97.      */
  98.     public function setRoles(array $roles): self
  99.     {
  100.         $this->roles $roles;
  101.         return $this;
  102.     }
  103.     /**
  104.      * @see UserInterface
  105.      */
  106.     public function getPassword(): string
  107.     {
  108.         return (string)$this->password;
  109.     }
  110.     /**
  111.      * @param string $password
  112.      * @return $this
  113.      */
  114.     public function setPassword(string $password): self
  115.     {
  116.         $this->password $password;
  117.         return $this;
  118.     }
  119.     /**
  120.      * @see UserInterface
  121.      */
  122.     public function getSalt()
  123.     {
  124.         // not needed when using the "bcrypt" algorithm in security.yaml
  125.     }
  126.     /**
  127.      * @see UserInterface
  128.      */
  129.     public function eraseCredentials()
  130.     {
  131.         // If you store any temporary, sensitive data on the user, clear it here
  132.         // $this->plainPassword = null;
  133.     }
  134.     public function getUserIdentifier(): string
  135.     {
  136.         return (string)$this->username;
  137.     }
  138.     public function addRole(Role $role): self
  139.     {
  140.         if (!$this->roles->contains($role)) {
  141.             $this->roles[] = $role;
  142.             $role->setUsers($this);
  143.         }
  144.         return $this;
  145.     }
  146.     public function removeRole(Role $role): self
  147.     {
  148.         if ($this->roles->removeElement($role)) {
  149.             $role->removeUser($this);
  150.         }
  151.         return $this;
  152.     }
  153. }