src/Entity/User.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Serializable;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. /**
  12.  * @ORM\Entity(repositoryClass=UserRepository::class)
  13.  * @ORM\Table(name="`user`")
  14.  * @UniqueEntity(fields={"username"}, message="Ya existe un usuario con ese nombre de usuario")
  15.  */
  16. class User implements UserInterfaceSerializablePasswordAuthenticatedUserInterface
  17. {
  18.     /**
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue
  21.      * @ORM\Column(type="integer")
  22.      */
  23.     private $id;
  24.     /**
  25.      * @ORM\Column(type="string", length=180, unique=true)
  26.      */
  27.     private $username;
  28.     /**
  29.      * @ORM\Column(type="json")
  30.      */
  31.     private $roles = [];
  32.     /**
  33.      * @var string The hashed password
  34.      * @ORM\Column(type="string")
  35.      */
  36.     private $password;
  37.     /**
  38.      * @ORM\OneToOne(targetEntity=Cliente::class, mappedBy="user", cascade={"persist", "remove"})
  39.      */
  40.     private $cliente;
  41.     /**
  42.      * @ORM\OneToMany(targetEntity=Contrato::class, mappedBy="createdBy")
  43.      */
  44.     private $contratosCreados;
  45.     /**
  46.      * @ORM\OneToMany(targetEntity=Notificacion::class, mappedBy="user", orphanRemoval=true)
  47.      */
  48.     private $notificaciones;
  49.     /**
  50.      * @ORM\OneToOne(targetEntity=Comercial::class, mappedBy="user", cascade={"persist", "remove"})
  51.      */
  52.     private $comercial;
  53.     /**
  54.      * @ORM\Column(type="string", length=255, nullable=true)
  55.      */
  56.     private $email;
  57.     public function __construct()
  58.     {
  59.         $this->contratosCreados = new ArrayCollection();
  60.     }
  61.     public function getId(): ?int
  62.     {
  63.         return $this->id;
  64.     }
  65.     /**
  66.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  67.      */
  68.     public function getUsername(): string
  69.     {
  70.         return (string) $this->username;
  71.     }
  72.     public function setUsername(string $username): self
  73.     {
  74.         $this->username $username;
  75.         return $this;
  76.     }
  77.     /**
  78.      * A visual identifier that represents this user.
  79.      *
  80.      * @see UserInterface
  81.      */
  82.     public function getUserIdentifier(): string
  83.     {
  84.         return (string) $this->username;
  85.     }
  86.     /**
  87.      * @see UserInterface
  88.      */
  89.     public function getRoles(): array
  90.     {
  91.         $roles $this->roles;
  92.         // guarantee every user at least has ROLE_USER
  93.         $roles[] = 'ROLE_USER';
  94.         return array_unique($roles);
  95.     }
  96.     public function setRoles(array $roles): self
  97.     {
  98.         $this->roles $roles;
  99.         return $this;
  100.     }
  101.     /**
  102.      * @see PasswordAuthenticatedUserInterface
  103.      */
  104.     public function getPassword(): string
  105.     {
  106.         return $this->password;
  107.     }
  108.     public function setPassword(string $password): self
  109.     {
  110.         $this->password $password;
  111.         return $this;
  112.     }
  113.     /**
  114.      * Returning a salt is only needed, if you are not using a modern
  115.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  116.      *
  117.      * @see UserInterface
  118.      */
  119.     public function getSalt(): ?string
  120.     {
  121.         return null;
  122.     }
  123.     /**
  124.      * @see UserInterface
  125.      */
  126.     public function eraseCredentials()
  127.     {
  128.         // If you store any temporary, sensitive data on the user, clear it here
  129.         // $this->plainPassword = null;
  130.     }
  131.     public function __toString(): ?string
  132.     {
  133.         return $this->username;
  134.     }
  135.     /**
  136.      * {@inheritdoc}
  137.      */
  138.     public function serialize(): string
  139.     {
  140.         // add $this->salt too if you don't use Bcrypt or Argon2i
  141.         return serialize([$this->id$this->username$this->password$this->roles]);
  142.     }
  143.     /**
  144.      * {@inheritdoc}
  145.      */
  146.     public function unserialize($serialized): void
  147.     {
  148.         // add $this->salt too if you don't use Bcrypt or Argon2i
  149.         [$this->id$this->username$this->password$this->roles] = unserialize($serialized, ['allowed_classes' => false]);
  150.     }
  151.     public function getCliente(): ?Cliente
  152.     {
  153.         return $this->cliente;
  154.     }
  155.     public function setCliente(?Cliente $cliente): self
  156.     {
  157.         $this->cliente $cliente;
  158.         // set (or unset) the owning side of the relation if necessary
  159.         $newUser null === $cliente null $this;
  160.         if ($cliente->getUser() !== $newUser) {
  161.             $cliente->setUser($newUser);
  162.         }
  163.         return $this;
  164.     }
  165.     /**
  166.      * @return Collection|Contrato[]
  167.      */
  168.     public function getContratosCreados(): Collection
  169.     {
  170.         return $this->contratosCreados;
  171.     }
  172.     public function addContratosCreado(Contrato $contratosCreado): self
  173.     {
  174.         if (!$this->contratosCreados->contains($contratosCreado)) {
  175.             $this->contratosCreados[] = $contratosCreado;
  176.             $contratosCreado->setCreatedBy($this);
  177.         }
  178.         return $this;
  179.     }
  180.     public function removeContratosCreado(Contrato $contratosCreado): self
  181.     {
  182.         if ($this->contratosCreados->contains($contratosCreado)) {
  183.             $this->contratosCreados->removeElement($contratosCreado);
  184.             // set the owning side to null (unless already changed)
  185.             if ($contratosCreado->getCreatedBy() === $this) {
  186.                 $contratosCreado->setCreatedBy(null);
  187.             }
  188.         }
  189.         return $this;
  190.     }
  191.     /**
  192.      * @return Collection|Notificacion[]
  193.      */
  194.     public function getNotificaciones(): Collection
  195.     {
  196.         return $this->notificaciones;
  197.     }
  198.     public function addNotificacion(Notificacion $notificacion): self
  199.     {
  200.         if (!$this->notificaciones->contains($notificacion)) {
  201.             $this->notificaciones[] = $notificacion;
  202.             $notificacion->setUser($this);
  203.         }
  204.         return $this;
  205.     }
  206.     public function removeNotificacion(Notificacion $notificacion): self
  207.     {
  208.         if ($this->notificaciones->contains($notificacion)) {
  209.             $this->notificaciones->removeElement($notificacion);
  210.             // set the owning side to null (unless already changed)
  211.             if ($notificacion->getUser() === $this) {
  212.                 $notificacion->setUser(null);
  213.             }
  214.         }
  215.         return $this;
  216.     }
  217.     public function getComercial(): ?Comercial
  218.     {
  219.         return $this->comercial;
  220.     }
  221.     public function setComercial(?Comercial $comercial): self
  222.     {
  223.         $this->comercial $comercial;
  224.         // set (or unset) the owning side of the relation if necessary
  225.         $newUser null === $comercial null $this;
  226.         if ($comercial->getUser() !== $newUser) {
  227.             $comercial->setUser($newUser);
  228.         }
  229.         return $this;
  230.     }
  231.     public function getEmail(): ?string
  232.     {
  233.         return $this->email;
  234.     }
  235.     public function setEmail(?string $email): self
  236.     {
  237.         $this->email $email;
  238.         return $this;
  239.     }
  240. }