src/Entity/User.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use App\Traits\CommonTrt;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. /**
  11.  * @ORM\Entity(repositoryClass=UserRepository::class)
  12.  * @ORM\Table(name="`user`")
  13.  */
  14. class User implements UserInterface, PasswordAuthenticatedUserInterface
  15. {
  16.     use CommonTrt;
  17.     /**
  18.      * @ORM\Id
  19.      * @ORM\GeneratedValue
  20.      * @ORM\Column(type="integer")
  21.      */
  22.     private $id;
  23.     /**
  24.      * @ORM\Column(type="string", length=180, unique=true)
  25.      */
  26.     private $email;
  27.     /**
  28.      * @ORM\Column(type="json")
  29.      */
  30.     private $roles = [];
  31.     /**
  32.      * @var string The hashed password
  33.      * @ORM\Column(type="string")
  34.      */
  35.     private $password;
  36.     /**
  37.      * @ORM\Column(type="string", length=255)
  38.      */
  39.     private $firstName;
  40.     /**
  41.      * @ORM\Column(type="string", length=255)
  42.      */
  43.     private $lastName;
  44.     /**
  45.      * @ORM\ManyToMany(targetEntity=Roles::class, inversedBy="users")
  46.      */
  47.     private $userRoles;
  48.     /**
  49.      * @ORM\Column(type="boolean")
  50.      */
  51.     private $isActive;
  52.     public function __construct()
  53.     {
  54.         $this->userRoles = new ArrayCollection();
  55.     }
  56.     public function getId(): ?int
  57.     {
  58.         return $this->id;
  59.     }
  60.     public function getEmail(): ?string
  61.     {
  62.         return $this->email;
  63.     }
  64.     public function setEmail(string $email): self
  65.     {
  66.         $this->email = $email;
  67.         return $this;
  68.     }
  69.     /**
  70.      * A visual identifier that represents this user.
  71.      *
  72.      * @see UserInterface
  73.      */
  74.     public function getUserIdentifier(): string
  75.     {
  76.         return (string) $this->email;
  77.     }
  78.     /**
  79.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  80.      */
  81.     public function getUsername(): string
  82.     {
  83.         return (string) $this->email;
  84.     }
  85.     /**
  86.      * @see UserInterface
  87.      */
  88.     public function getRoles()
  89.     {
  90.         $roles = $this->userRoles->map(function ($role) {
  91.             return $role->getName();
  92.         })->toArray();
  93.         $roles[] = 'ROLE_USER';
  94.         return $roles;
  95.     }
  96.     public function setRoles(array $roles): self
  97.     {
  98.         $this->roles = $roles;
  99.         return $this;
  100.     }
  101.     /**
  102.      * @see PasswordAuthenticatedUserInterface
  103.      */
  104.     public function getPassword(): string
  105.     {
  106.         return $this->password;
  107.     }
  108.     public function setPassword(string $password): self
  109.     {
  110.         $this->password = $password;
  111.         return $this;
  112.     }
  113.     /**
  114.      * Returning a salt is only needed, if you are not using a modern
  115.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  116.      *
  117.      * @see UserInterface
  118.      */
  119.     public function getSalt(): ?string
  120.     {
  121.         return null;
  122.     }
  123.     /**
  124.      * @see UserInterface
  125.      */
  126.     public function eraseCredentials()
  127.     {
  128.         // If you store any temporary, sensitive data on the user, clear it here
  129.         // $this->plainPassword = null;
  130.     }
  131.     public function getFirstName(): ?string
  132.     {
  133.         return $this->firstName;
  134.     }
  135.     public function setFirstName(string $firstName): self
  136.     {
  137.         $this->firstName = $firstName;
  138.         return $this;
  139.     }
  140.     public function getLastName(): ?string
  141.     {
  142.         return $this->lastName;
  143.     }
  144.     public function setLastName(string $lastName): self
  145.     {
  146.         $this->lastName = $lastName;
  147.         return $this;
  148.     }
  149.     /**
  150.      * @return Collection<int, Roles>
  151.      */
  152.     public function getUserRoles(): Collection
  153.     {
  154.         return $this->userRoles;
  155.     }
  156.     public function addUserRole(Roles $userRole): self
  157.     {
  158.         if (!$this->userRoles->contains($userRole)) {
  159.             $this->userRoles[] = $userRole;
  160.         }
  161.         return $this;
  162.     }
  163.     public function removeUserRole(Roles $userRole): self
  164.     {
  165.         $this->userRoles->removeElement($userRole);
  166.         return $this;
  167.     }
  168.     public function isIsActive(): ?bool
  169.     {
  170.         return $this->isActive;
  171.     }
  172.     public function setIsActive(bool $isActive): self
  173.     {
  174.         $this->isActive = $isActive;
  175.         return $this;
  176.     }
  177.     public function getFullName()
  178.     {
  179.         return $this->firstName.' '.$this->lastName;
  180.     }
  181. }