<?php
namespace App\Entity;
use App\Repository\UsersRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UsersRepository::class)]
class Users implements UserInterface, \Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column(length: 255)]
private ?string $email = null;
#[ORM\Column(length: 255)]
private ?string $password = null;
#[ORM\Column]
private ?bool $active = null;
#[ORM\ManyToMany(targetEntity: Roles::class, inversedBy: 'users')]
private Collection $roles;
#[ORM\ManyToMany(targetEntity: Roles::class, inversedBy: 'users')]
private Collection $roless;
public $passwordEncoder;
public function __construct(UserPasswordHasherInterface $passwordEncoder)
{
$this->roles = new ArrayCollection();
$this->roless = new ArrayCollection();
$this->passwordEncoder = $passwordEncoder;
}
/**
* @return Collection<int, Roles>
*/
public function getRoless(): Collection
{
return $this->roless;
}
public function addRoless(Roles $roless): self
{
if (!$this->roless->contains($roless)) {
$this->roless->add($roless);
}
return $this;
}
public function removeRoless(Roles $roless): self
{
$this->roless->removeElement($roless);
return $this;
}
public function getUserIdentifier(): string
{
return $this->email;
}
public function __toString()
{
return $this->getName(); // Reemplaza "getName()" con el método que devuelve el nombre en tu entidad Roles
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function verifyPassword(string $password): bool
{
return $this->passwordEncoder->isPasswordValid($this, $password);
}
public function setPassword(?string $password): self
{
if(($password=='' or $password==null) and ($this->password != '' and $this->password!=null)){
return $this;
}
if($this->passwordEncoder==null){
$this->password = $password;
}else{
$this->password = $this->passwordEncoder->hashPassword($this, $password);
}
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
/**
* @return array<int, Roles>
*/
public function getRoles(): array
{
$roles = [];
foreach ($this->roles as $role) {
$roles[$role->getId()] = $role->getName();
}
return $roles;
}
public function eraseCredentials()
{
return true;
// Aquí puedes eliminar información sensible almacenada en la entidad, como una contraseña en texto plano.
// Sin embargo, como estamos utilizando contraseñas encriptadas, no es necesario hacer nada en este método.
}
public function addRole(Roles $role): self
{
if (!$this->roles->contains($role)) {
$this->roles->add($role);
}
return $this;
}
public function removeRole(Roles $role): self
{
$this->roles->removeElement($role);
return $this;
}
}