src/Entity/User.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Timestampable\Traits\TimestampableEntity;
  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. use Symfony\Component\Validator\Constraints as Assert;
  9. use App\Repository\UserRepository;
  10. /**
  11.  * @ORM\Entity(repositoryClass=UserRepository::class)
  12.  * @UniqueEntity(fields={"email"}, message="There is already an account with this email")
  13.  */
  14. class User implements UserInterfacePasswordAuthenticatedUserInterface
  15. {
  16.     /**
  17.      * Hook timestampable behavior
  18.      * updates createdAt, updatedAt fields
  19.      */
  20.     use TimestampableEntity;
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\Column(type="string", length=180, unique=true)
  29.      * @Assert\NotBlank
  30.      * @Assert\Email()
  31.      */
  32.     private $email;
  33.     /**
  34.      * @ORM\Column(type="json")
  35.      */
  36.     private $roles = [];
  37.     /**
  38.      * @ORM\Column(type="string", length=255)
  39.      * @Assert\NotBlank()
  40.      * @Assert\Length(min=3, max=255)
  41.      */
  42.     private $firstName;
  43.     /**
  44.      * @ORM\Column(type="string", length=255)
  45.      * @Assert\Regex(
  46.      *     pattern="/(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).{7,}/",
  47.      *     message="Password must be seven characters long and contain at least one digit, one uppercase letter and one lowercase letter"
  48.      * )
  49.      */
  50.     private $password;
  51.     /**
  52.      * @ORM\Column(type="boolean", options={"default":"1"})
  53.      */
  54.     private $enabled;
  55.     /**
  56.      * @ORM\Column(type="string", length=255)
  57.      */
  58.     private $lastName;
  59.     public function getId(): ?int
  60.     {
  61.         return $this->id;
  62.     }
  63.     public function getEmail(): ?string
  64.     {
  65.         return $this->email;
  66.     }
  67.     public function setEmail(string $email): self
  68.     {
  69.         $this->email $email;
  70.         return $this;
  71.     }
  72.     /**
  73.      * A visual identifier that represents this user.
  74.      *
  75.      * @see UserInterface
  76.      */
  77.     public function getUserIdentifier(): string
  78.     {
  79.         return (string) $this->email;
  80.     }
  81.     /**
  82.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  83.      */
  84.     public function getUsername(): string
  85.     {
  86.         return (string) $this->email;
  87.     }
  88.     /**
  89.      * @see UserInterface
  90.      */
  91.     public function getRoles(): array
  92.     {
  93.         $roles $this->roles;
  94.         // guarantee every user at least has ROLE_USER
  95.         $roles[] = 'ROLE_USER';
  96.         return array_unique($roles);
  97.     }
  98.     public function setRoles(array $roles): self
  99.     {
  100.         $this->roles $roles;
  101.         return $this;
  102.     }
  103.     /**
  104.      * @see PasswordAuthenticatedUserInterface
  105.      */
  106.     public function getPassword(): ?string
  107.     {
  108.         return $this->password;
  109.     }
  110.     /**
  111.      * This method can be removed in Symfony 6.0 - is not needed for apps that do not check user passwords.
  112.      *
  113.      * @see UserInterface
  114.      */
  115.     public function getSalt(): ?string
  116.     {
  117.         return null;
  118.     }
  119.     /**
  120.      * @see UserInterface
  121.      */
  122.     public function eraseCredentials()
  123.     {
  124.         // If you store any temporary, sensitive data on the user, clear it here
  125.         // $this->plainPassword = null;
  126.     }
  127.     public function getFirstName(): ?string
  128.     {
  129.         return $this->firstName;
  130.     }
  131.     public function setFirstName(string $firstName): self
  132.     {
  133.         $this->firstName $firstName;
  134.         return $this;
  135.     }
  136.     public function setPassword(string $password): self
  137.     {
  138.         if ($password !=='') {
  139.             $this->password $password;
  140.         }
  141.         return $this;
  142.     }
  143.     public function isEnabled(): ?bool
  144.     {
  145.         return $this->enabled;
  146.     }
  147.     public function setEnabled(?bool $enabled): self
  148.     {
  149.         $this->enabled $enabled;
  150.         return $this;
  151.     }
  152.     public function __toString()
  153.     {
  154.         return $this->firstName;
  155.     }
  156.     public function getLastName(): ?string
  157.     {
  158.         return $this->lastName;
  159.     }
  160.     public function setLastName(string $lastName): self
  161.     {
  162.         $this->lastName $lastName;
  163.         return $this;
  164.     }
  165. }