src/Entity/User.php line 16

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