<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Serializable;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
* @UniqueEntity(fields={"username"}, message="Ya existe un usuario con ese nombre de usuario")
*/
class User implements UserInterface, Serializable, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
private $username;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\OneToOne(targetEntity=Cliente::class, mappedBy="user", cascade={"persist", "remove"})
*/
private $cliente;
/**
* @ORM\OneToMany(targetEntity=Contrato::class, mappedBy="createdBy")
*/
private $contratosCreados;
/**
* @ORM\OneToMany(targetEntity=Notificacion::class, mappedBy="user", orphanRemoval=true)
*/
private $notificaciones;
/**
* @ORM\OneToOne(targetEntity=Comercial::class, mappedBy="user", cascade={"persist", "remove"})
*/
private $comercial;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $email;
public function __construct()
{
$this->contratosCreados = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @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 null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function __toString(): ?string
{
return $this->username;
}
/**
* {@inheritdoc}
*/
public function serialize(): string
{
// add $this->salt too if you don't use Bcrypt or Argon2i
return serialize([$this->id, $this->username, $this->password, $this->roles]);
}
/**
* {@inheritdoc}
*/
public function unserialize($serialized): void
{
// add $this->salt too if you don't use Bcrypt or Argon2i
[$this->id, $this->username, $this->password, $this->roles] = unserialize($serialized, ['allowed_classes' => false]);
}
public function getCliente(): ?Cliente
{
return $this->cliente;
}
public function setCliente(?Cliente $cliente): self
{
$this->cliente = $cliente;
// set (or unset) the owning side of the relation if necessary
$newUser = null === $cliente ? null : $this;
if ($cliente->getUser() !== $newUser) {
$cliente->setUser($newUser);
}
return $this;
}
/**
* @return Collection|Contrato[]
*/
public function getContratosCreados(): Collection
{
return $this->contratosCreados;
}
public function addContratosCreado(Contrato $contratosCreado): self
{
if (!$this->contratosCreados->contains($contratosCreado)) {
$this->contratosCreados[] = $contratosCreado;
$contratosCreado->setCreatedBy($this);
}
return $this;
}
public function removeContratosCreado(Contrato $contratosCreado): self
{
if ($this->contratosCreados->contains($contratosCreado)) {
$this->contratosCreados->removeElement($contratosCreado);
// set the owning side to null (unless already changed)
if ($contratosCreado->getCreatedBy() === $this) {
$contratosCreado->setCreatedBy(null);
}
}
return $this;
}
/**
* @return Collection|Notificacion[]
*/
public function getNotificaciones(): Collection
{
return $this->notificaciones;
}
public function addNotificacion(Notificacion $notificacion): self
{
if (!$this->notificaciones->contains($notificacion)) {
$this->notificaciones[] = $notificacion;
$notificacion->setUser($this);
}
return $this;
}
public function removeNotificacion(Notificacion $notificacion): self
{
if ($this->notificaciones->contains($notificacion)) {
$this->notificaciones->removeElement($notificacion);
// set the owning side to null (unless already changed)
if ($notificacion->getUser() === $this) {
$notificacion->setUser(null);
}
}
return $this;
}
public function getComercial(): ?Comercial
{
return $this->comercial;
}
public function setComercial(?Comercial $comercial): self
{
$this->comercial = $comercial;
// set (or unset) the owning side of the relation if necessary
$newUser = null === $comercial ? null : $this;
if ($comercial->getUser() !== $newUser) {
$comercial->setUser($newUser);
}
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
}