src/Entity/Users.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UsersRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. #[ORM\Entity(repositoryClass: UsersRepository::class)]
  10. class Users implements UserInterface, \Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
  11. {
  12.     #[ORM\Id]
  13.     #[ORM\GeneratedValue]
  14.     #[ORM\Column]
  15.     private ?int $id = null;
  16.     #[ORM\Column(length: 255)]
  17.     private ?string $name = null;
  18.     #[ORM\Column(length: 255)]
  19.     private ?string $email = null;
  20.     #[ORM\Column(length: 255)]
  21.     private ?string $password = null;
  22.     #[ORM\Column]
  23.     private ?bool $active = null;
  24.     #[ORM\ManyToMany(targetEntity: Roles::class, inversedBy: 'users')]
  25.     private Collection $roles;
  26.     #[ORM\ManyToMany(targetEntity: Roles::class, inversedBy: 'users')]
  27.     private Collection $roless;
  28.     public $passwordEncoder;
  29.     public function __construct(UserPasswordHasherInterface $passwordEncoder)
  30.     {
  31.         $this->roles = new ArrayCollection();
  32.         $this->roless = new ArrayCollection();
  33.         $this->passwordEncoder = $passwordEncoder;
  34.     }
  35.     /**
  36.      * @return Collection<int, Roles>
  37.      */
  38.     public function getRoless(): Collection
  39.     {
  40.         return $this->roless;
  41.     }
  42.     public function addRoless(Roles $roless): self
  43.     {
  44.         if (!$this->roless->contains($roless)) {
  45.             $this->roless->add($roless);
  46.         }
  47.         return $this;
  48.     }
  49.     public function removeRoless(Roles $roless): self
  50.     {
  51.         $this->roless->removeElement($roless);
  52.         return $this;
  53.     }
  54.     public function getUserIdentifier(): string
  55.     {
  56.         return $this->email;
  57.     }
  58.     public function __toString()
  59.     {
  60.         return $this->getName(); // Reemplaza "getName()" con el método que devuelve el nombre en tu entidad Roles
  61.     }
  62.     public function getId(): ?int
  63.     {
  64.         return $this->id;
  65.     }
  66.     public function getName(): ?string
  67.     {
  68.         return $this->name;
  69.     }
  70.     public function setName(string $name): self
  71.     {
  72.         $this->name = $name;
  73.         return $this;
  74.     }
  75.     public function getEmail(): ?string
  76.     {
  77.         return $this->email;
  78.     }
  79.     public function setEmail(string $email): self
  80.     {
  81.         $this->email = $email;
  82.         return $this;
  83.     }
  84.     public function verifyPassword(string $password): bool
  85.     {
  86.         return $this->passwordEncoder->isPasswordValid($this, $password);
  87.     }
  88.     public function setPassword(?string $password): self
  89.     {
  90.         if(($password=='' or $password==null) and ($this->password != '' and $this->password!=null)){
  91.             return $this;
  92.         }
  93.         if($this->passwordEncoder==null){
  94.             $this->password = $password;
  95.         }else{
  96.             $this->password = $this->passwordEncoder->hashPassword($this, $password);
  97.         }
  98.         return $this;
  99.     }
  100.     public function getPassword(): ?string
  101.     {
  102.         return $this->password;
  103.     }
  104.     public function isActive(): ?bool
  105.     {
  106.         return $this->active;
  107.     }
  108.     public function setActive(bool $active): self
  109.     {
  110.         $this->active = $active;
  111.         return $this;
  112.     }
  113.     /**
  114.      * @return array<int, Roles>
  115.      */
  116.     public function getRoles(): array
  117.     {
  118.         $roles = [];
  119.         foreach ($this->roles as $role) {
  120.             $roles[$role->getId()] = $role->getName();
  121.         }
  122.         return $roles;
  123.     }
  124.     public function eraseCredentials()
  125.     {
  126.         return true;
  127.         // Aquí puedes eliminar información sensible almacenada en la entidad, como una contraseña en texto plano.
  128.         // Sin embargo, como estamos utilizando contraseñas encriptadas, no es necesario hacer nada en este método.
  129.     }
  130.     public function addRole(Roles $role): self
  131.     {
  132.         if (!$this->roles->contains($role)) {
  133.             $this->roles->add($role);
  134.         }
  135.         return $this;
  136.     }
  137.     public function removeRole(Roles $role): self
  138.     {
  139.         $this->roles->removeElement($role);
  140.         return $this;
  141.     }
  142. }