src/Entity/User.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace App\Entity;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. /**
  16.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  17.  * @ORM\Table(name="symfony_demo_user")
  18.  *
  19.  * Defines the properties of the User entity to represent the application users.
  20.  * See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
  21.  *
  22.  * Tip: if you have an existing database, you can generate these entity class automatically.
  23.  * See https://symfony.com/doc/current/doctrine/reverse_engineering.html
  24.  *
  25.  * @author Ryan Weaver <weaverryan@gmail.com>
  26.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  27.  */
  28. class User implements UserInterfacePasswordAuthenticatedUserInterface
  29. {
  30.     /**
  31.      * @ORM\Id
  32.      * @ORM\GeneratedValue
  33.      * @ORM\Column(type="integer")
  34.      */
  35.     private ?int $id null;
  36.     /**
  37.      * @ORM\Column(type="string")
  38.      */
  39.     #[Assert\NotBlank]
  40.     private ?string $fullName null;
  41.     /**
  42.      * @ORM\Column(type="string", unique=true)
  43.      */
  44.     #[
  45.         Assert\NotBlank,
  46.         Assert\Length(min2max50)
  47.     ]
  48.     private ?string $username null;
  49.     /**
  50.      * @ORM\Column(type="string", unique=true)
  51.      */
  52.     #[Assert\Email]
  53.     private ?string $email null;
  54.     /**
  55.      * @ORM\Column(type="string")
  56.      */
  57.     private ?string $password null;
  58.     /**
  59.      * @ORM\Column(type="json")
  60.      */
  61.     private array $roles = [];
  62.     public function getId(): ?int
  63.     {
  64.         return $this->id;
  65.     }
  66.     public function setFullName(string $fullName): void
  67.     {
  68.         $this->fullName $fullName;
  69.     }
  70.     public function getFullName(): ?string
  71.     {
  72.         return $this->fullName;
  73.     }
  74.     public function getUserIdentifier(): string
  75.     {
  76.         return $this->username;
  77.     }
  78.     public function getUsername(): string
  79.     {
  80.         return $this->getUserIdentifier();
  81.     }
  82.     public function setUsername(string $username): void
  83.     {
  84.         $this->username $username;
  85.     }
  86.     public function getEmail(): ?string
  87.     {
  88.         return $this->email;
  89.     }
  90.     public function setEmail(string $email): void
  91.     {
  92.         $this->email $email;
  93.     }
  94.     public function getPassword(): ?string
  95.     {
  96.         return $this->password;
  97.     }
  98.     public function setPassword(string $password): void
  99.     {
  100.         $this->password $password;
  101.     }
  102.     /**
  103.      * Returns the roles or permissions granted to the user for security.
  104.      */
  105.     public function getRoles(): array
  106.     {
  107.         $roles $this->roles;
  108.         // guarantees that a user always has at least one role for security
  109.         if (empty($roles)) {
  110.             $roles[] = 'ROLE_USER';
  111.         }
  112.         return array_unique($roles);
  113.     }
  114.     public function setRoles(array $roles): void
  115.     {
  116.         $this->roles $roles;
  117.     }
  118.     /**
  119.      * Returns the salt that was originally used to encode the password.
  120.      *
  121.      * {@inheritdoc}
  122.      */
  123.     public function getSalt(): ?string
  124.     {
  125.         // We're using bcrypt in security.yaml to encode the password, so
  126.         // the salt value is built-in and you don't have to generate one
  127.         // See https://en.wikipedia.org/wiki/Bcrypt
  128.         return null;
  129.     }
  130.     /**
  131.      * Removes sensitive data from the user.
  132.      *
  133.      * {@inheritdoc}
  134.      */
  135.     public function eraseCredentials(): void
  136.     {
  137.         // if you had a plainPassword property, you'd nullify it here
  138.         // $this->plainPassword = null;
  139.     }
  140.     public function __serialize(): array
  141.     {
  142.         // add $this->salt too if you don't use Bcrypt or Argon2i
  143.         return [$this->id$this->username$this->password];
  144.     }
  145.     public function __unserialize(array $data): void
  146.     {
  147.         // add $this->salt too if you don't use Bcrypt or Argon2i
  148.         [$this->id$this->username$this->password] = $data;
  149.     }
  150. }