<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
#[UniqueEntity(fields: ["username", "email"], message: "El nombre de usuario y/o email ya existe")]
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\Table(name: '`user`')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: "integer")]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $username = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $usernameCanonical = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $emailCanonical = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $salt = null;
#[ORM\Column(type:"array")]
private array $roles = [];
#[ORM\Column]
private ?string $password = null;
#[ORM\OneToOne(inversedBy: "user", targetEntity: "Employee", cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(name: "employee_id", referencedColumnName: "id", onDelete: "CASCADE")]
protected $employee;
#[ORM\OneToOne(targetEntity:"Supplier", mappedBy:"user", cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(name: "supplier_id", referencedColumnName: "id", onDelete: "CASCADE", nullable: true)]
protected $supplier;
#[ORM\Column(type: "boolean")]
protected $enabled;
#[ORM\Column(type: "boolean")]
protected $secured = false;
#[ORM\Column(type: "boolean", options: ["default" => false])]
protected $passwordUpdatedForNewPolicy = false;
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getEmailCanonical(): ?string
{
return $this->emailCanonical;
}
public function setEmailCanonical(string $emailCanonical): self
{
$this->emailCanonical = $emailCanonical;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
public function getUsername(): string
{
return (string) $this->username;
}
public function setUsername($username): self
{
$this->username = $username;
return $this;
}
public function getUsernameCanonical(): string
{
return (string) $this->usernameCanonical;
}
public function setUsernameCanonical($usernameCanonical): self
{
$this->usernameCanonical = $usernameCanonical;
return $this;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return $this->salt;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getEmployee()
{
return $this->employee;
}
public function setEmployee($employee)
{
$this->employee = $employee;
$this->employee->setUser($this);
}
public function isEmployee()
{
return !is_null($this->employee);
}
public function getIsEmployee()
{
return $this->isEmployee();
}
public function getSupplier()
{
return $this->supplier;
}
public function setSupplier($supplier)
{
$this->supplier = $supplier;
$this->supplier->setUser($this);
}
public function isSupplier()
{
return !is_null($this->supplier);
}
public function getIsSupplier()
{
return $this->isSupplier();
}
public function isSecured(): bool
{
return $this->secured;
}
public function setSecured(bool $secured)
{
$this->secured = $secured;
}
public function isPasswordUpdatedForNewPolicy(): bool
{
return $this->passwordUpdatedForNewPolicy;
}
public function setPasswordUpdatedForNewPolicy(bool $passwordUpdatedForNewPolicy): self
{
$this->passwordUpdatedForNewPolicy = $passwordUpdatedForNewPolicy;
return $this;
}
public function isEnabled()
{
return $this->enabled;
}
public function setEnabled($boolean)
{
$this->enabled = (bool) $boolean;
return $this;
}
public function addRole($role)
{
$role = strtoupper($role);
if ($role === 'ROLE_USER') {
return $this;
}
if (!in_array($role, $this->roles, true)) {
$this->roles[] = $role;
}
return $this;
}
public function removeRole($role)
{
if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
unset($this->roles[$key]);
$this->roles = array_values($this->roles);
}
return $this;
}
public function __toString(): string
{
if ($this->employee) {
return $this->employee->getFullName();
}
if ($this->supplier) {
return $this->supplier->getName();
}
return $this->username ?? '';
}
}