src/Entity/User.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. #[ORM\Entity(repositoryClass: UserRepository::class)]
  9. #[ORM\Table(name: '`user`')]
  10. #[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
  11. class User implements UserInterface, PasswordAuthenticatedUserInterface
  12. {
  13.     #[ORM\Id]
  14.     #[ORM\GeneratedValue]
  15.     #[ORM\Column]
  16.     private ?int $id = null;
  17.     #[ORM\Column(length: 180, unique: true)]
  18.     private ?string $username = null;
  19.     #[ORM\Column]
  20.     private array $roles = [];
  21.     /**
  22.      * @var string The hashed password
  23.      */
  24.     #[ORM\Column]
  25.     private ?string $password = null;
  26.     #[ORM\Column(length: 255, nullable: true)]
  27.     private ?string $auth_token = null;
  28.     public function getId(): ?int
  29.     {
  30.         return $this->id;
  31.     }
  32.     /**
  33.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  34.      */
  35.     public function getUsername(): string
  36.     {
  37.         return (string) $this->username;
  38.     }
  39.     public function setUsername(string $username): static
  40.     {
  41.         $this->username = $username;
  42.         return $this;
  43.     }
  44.     /**
  45.      * A visual identifier that represents this user.
  46.      *
  47.      * @see UserInterface
  48.      */
  49.     public function getUserIdentifier(): string
  50.     {
  51.         return (string) $this->username;
  52.     }
  53.     /**
  54.      * @see UserInterface
  55.      */
  56.     public function getRoles(): array
  57.     {
  58.         $roles = $this->roles;
  59.         // guarantee every user at least has ROLE_USER
  60.         $roles[] = 'ROLE_USER';
  61.         return array_unique($roles);
  62.     }
  63.     public function setRoles(array $roles): static
  64.     {
  65.         $this->roles = $roles;
  66.         return $this;
  67.     }
  68.     /**
  69.      * @see PasswordAuthenticatedUserInterface
  70.      */
  71.     public function getPassword(): string
  72.     {
  73.         return $this->password;
  74.     }
  75.     public function setPassword(string $password): static
  76.     {
  77.         $this->password = $password;
  78.         return $this;
  79.     }
  80.     /**
  81.      * Returning a salt is only needed, if you are not using a modern
  82.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  83.      *
  84.      * @see UserInterface
  85.      */
  86.     public function getSalt(): ?string
  87.     {
  88.         return null;
  89.     }
  90.     /**
  91.      * @see UserInterface
  92.      */
  93.     public function eraseCredentials(): void
  94.     {
  95.         // If you store any temporary, sensitive data on the user, clear it here
  96.         // $this->plainPassword = null;
  97.     }
  98.     public function getAuthToken(): ?string
  99.     {
  100.         return $this->auth_token;
  101.     }
  102.     public function setAuthToken(?string $auth_token): static
  103.     {
  104.         $this->auth_token = $auth_token;
  105.         return $this;
  106.     }
  107. }