src/Entity/User.php line 72

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  4. use Ramsey\Uuid\Uuid;
  5. use App\Trait\UuidTrait;
  6. use ApiPlatform\Metadata\Get;
  7. use ApiPlatform\Metadata\Put;
  8. use ApiPlatform\Metadata\Post;
  9. use Doctrine\DBAL\Types\Types;
  10. use ApiPlatform\Metadata\Patch;
  11. use ApiPlatform\Metadata\Delete;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use App\State\UserPasswordHasher;
  14. use App\Repository\UserRepository;
  15. use ApiPlatform\Metadata\ApiFilter;
  16. use ApiPlatform\Metadata\ApiProperty;
  17. use ApiPlatform\Metadata\ApiResource;
  18. use App\Controller\CurrentController;
  19. use ApiPlatform\Metadata\GetCollection;
  20. use Doctrine\Common\Collections\Collection;
  21. use App\Controller\ChangeMainEmpresaController;
  22. use Doctrine\Common\Collections\ArrayCollection;
  23. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  24. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  25. use Symfony\Component\Serializer\Annotation\Groups;
  26. use Symfony\Component\Validator\Constraints as Assert;
  27. use Symfony\Component\Security\Core\User\UserInterface;
  28. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  29. #[ApiResource(
  30.     operations: [
  31.         new GetCollection(uriTemplate'usuarios'),
  32.         new GetCollection(
  33.             uriTemplate'current',
  34.             controllerCurrentController::class,
  35.             outputUser::class,
  36.             paginationEnabledfalse,
  37.         ),
  38.         new Post(uriTemplate'usuarios'processorUserPasswordHasher::class, validationContext: ['groups' => ['Default''user:create']], denormalizationContext: ['groups' => ['user:create']]),
  39.         new Get(uriTemplate'usuarios/{uuid}'),
  40.         new Put(uriTemplate'usuarios/{uuid}'processorUserPasswordHasher::class, denormalizationContext: ['groups' => ['user:update']]),
  41.         new Put(uriTemplate'usuarios/{uuid}/cambioEmpresa'denormalizationContext: ['groups' => ['user:changeEmpresa']], controllerChangeMainEmpresaController::class, security"is_granted('ROLE_ADMIN_GROUP')"),
  42.         new Patch(uriTemplate'usuarios/{uuid}'processorUserPasswordHasher::class, denormalizationContext: ['groups' => ['user:update']]),
  43.         new Delete(uriTemplate'usuarios/{uuid}'),
  44.     ],
  45.     normalizationContext: ['groups' => ['user:read''uuid']],
  46. )]
  47. #[ORM\Entity(repositoryClassUserRepository::class)]
  48. #[UniqueEntity(
  49.     fields: ['username'],
  50.     message'Este nombre de usuario no está disponible. Por favor, elige otro.'
  51. )]
  52. #[ORM\Table(name'`user`')]
  53. #[ApiFilter(
  54.     SearchFilter::class,
  55.     properties: [
  56.         'username' => 'exact',
  57.         'nombre' => 'exact',
  58.         'apellido1' => 'exact',
  59.         'apellido2' => 'exact',
  60.         'nif' => 'exact',
  61.         'codigo' => 'exact',
  62.         'tipo' => 'exact',
  63.         'isActivo' => 'exact',
  64.         'emails.direccion' => 'exact'
  65.     ]
  66. )]
  67. #[ApiFilter(OrderFilter::class, properties: ['nombre''codigo''emails.direccion''telefonos.numero'])]
  68. class User implements UserInterfacePasswordAuthenticatedUserInterface
  69. {
  70.     use UuidTrait;
  71.     public const MODULO 'users';
  72.     public const TIPO_CLIENTE 'cliente';
  73.     public const TIPO_EVALUADOR 'evaluador';
  74.     public const TIPO_ADMIN 'administrador';
  75.     public const TIPO_ADMIN_GROUP 'administradorGrupo';
  76.     public const USER_CREATED 'user.created';
  77.     public const USER_UPDATED 'user.updated';
  78.     public const USER_DELETED 'user.deleted';
  79.     #[Assert\NotBlank(message'El nombre de usuario no puede estar vacío.',groups: ['user:create'])]
  80.     #[Groups(['user:read''user:create''user:update''sesion:read'])]
  81.     #[ORM\Column(length180uniquetrue)]
  82.     private ?string $username null;
  83.     #[Groups(['user:read''user:create''user:update'])]
  84.     #[ORM\Column]
  85.     private array $roles = [];
  86.     #[ORM\Column]
  87.     private ?string $password null;
  88.     #[Assert\NotBlank(groups: ['user:create'])]
  89.     #[Groups(['user:create''user:update'])]
  90.     private ?string $plainPassword null;
  91.     #[Groups(['user:read''user:create''user:update''sesion:read'])]
  92.     #[ORM\Column(length255nullabletrue)]
  93.     private ?string $codigo null;
  94.     #[Groups(['user:read''user:create''user:update''sesion:read'])]
  95.     #[ORM\Column(length255nullabletrue)]
  96.     private ?string $nombre null;
  97.     #[Groups(['user:read''user:create''user:update''sesion:read'])]
  98.     #[ORM\Column(length255nullabletrue)]
  99.     private ?string $apellido1 null;
  100.     #[Groups(['user:read''user:create''user:update''sesion:read'])]
  101.     #[ORM\Column(length255nullabletrue)]
  102.     private ?string $apellido2 null;
  103.     #[Groups(['user:read''user:create''user:update'])]
  104.     #[ORM\Column(length255nullabletrue)]
  105.     private ?string $cp null;
  106.     #[Groups(['user:read''user:create''user:update'])]
  107.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  108.     private ?string $observaciones null;
  109.     #[Groups(['user:read''user:create''user:update''sesion:read'])]
  110.     #[ORM\Column(length255nullabletrue)]
  111.     private ?string $nif null;
  112.     #[Groups(['user:read''user:update'])]
  113.     #[ORM\Column()]
  114.     private bool $isActivo true;
  115.     #[Groups(['user:read''user:create''user:update''sesion:read'])]
  116.     #[ORM\Column(nullabletrue)]
  117.     private ?string $descripcion null;
  118.     #[Groups(['user:read''user:create''user:update''sesion:read'])]
  119.     #[ORM\Column(length255)]
  120.     private ?string $tipo null;
  121.     #[Groups(['user:read''user:create''user:update'])]
  122.     #[ORM\OneToMany(mappedBy'user'targetEntityEmail::class, orphanRemovaltruecascade: ['persist''remove'])]
  123.     private Collection $emails;
  124.     #[Groups(['user:read''user:create''user:update'])]
  125.     #[ORM\OneToMany(mappedBy'user'targetEntityTelefono::class, orphanRemovaltruecascade: ['persist''remove'])]
  126.     private Collection $telefonos;
  127.     #[Groups(['user:read''user:create''user:update'])]
  128.     #[ORM\OneToMany(mappedBy'user'targetEntityDireccion::class, orphanRemovaltruecascade: ['persist''remove'])]
  129.     private Collection $direcciones;
  130.     #[Groups(['user:read''user:create''user:update'])]
  131.     #[ORM\Column(length255nullabletrue)]
  132.     private ?string $nombreRepresentante null;
  133.     #[Groups(['user:read''user:create''user:update'])]
  134.     #[ORM\Column(length255nullabletrue)]
  135.     private ?string $emailRepresentante null;
  136.     #[Groups(['user:read''user:create''user:update'])]
  137.     #[ORM\Column(length255nullabletrue)]
  138.     private ?string $telefonoRepresentante null;
  139.     #[Groups(['user:read''user:create''user:update'])]
  140.     #[ORM\Column(length255nullabletrue)]
  141.     private ?string $nifRepresentante null;
  142.     #[Groups(['user:read''user:create''user:update'])]
  143.     #[ORM\Column(length255nullabletrue)]
  144.     private ?string $cargoRepresentante null;
  145.     #[Groups(['user:read''user:create''user:update'])]
  146.     #[ORM\Column(nullabletrue)]
  147.     private array $seguimiento = [];
  148.     #[Groups(['user:read'])]
  149.     #[ORM\ManyToOne(inversedBy'users')]
  150.     private ?Empresa $empresa null;
  151.     #[Assert\NotBlank(groups: ['user:changeEmpresa'])]
  152.     #[Groups(['user:changeEmpresa'])]
  153.     private ?Empresa $cambioEmpresa null;
  154.     #[Groups(['user:read'])]
  155.     #[ORM\OneToMany(mappedBy'user'targetEntityNotificacion::class, orphanRemovaltrue)]
  156.     private Collection $notificaciones;
  157.     #[ORM\ManyToOne]
  158.     #[Groups(['user:read''user:create''user:update'])]
  159.     private ?GrupoEmpresa $grupoEmpresa null;
  160.     public function __construct()
  161.     {
  162.         $this->emails = new ArrayCollection();
  163.         $this->telefonos = new ArrayCollection();
  164.         $this->direcciones = new ArrayCollection();
  165.         $this->uuid Uuid::uuid4()->toString();
  166.         $this->notificaciones = new ArrayCollection();
  167.     }
  168.     #[ApiProperty(security"is_granted('ROLE_ADMIN_GROUP')")]
  169.     #[Groups(['user:read'])]
  170.     public function getEmpresasInGrupo()
  171.     {
  172.         if ($this->getEmpresa() === null || $this->getEmpresa()->getGrupoEmpresa() === null) {
  173.             return [];
  174.         }
  175.         return array_map(function ($empresa) {
  176.             return [
  177.                 'uuid' => $empresa->getUuid(),
  178.                 'nombre' => $empresa->getNombre()
  179.             ];
  180.         }, $this->getEmpresa()->getGrupoEmpresa()->getEmpresas()->toArray());
  181.     }
  182.     #[Groups(['user:read'])]
  183.     public function getNombreCompleto(): string
  184.     {
  185.         return trim($this->nombre ' ' $this->apellido1 ' ' $this->apellido2);
  186.     }
  187.     #[Groups(['user:read'])]
  188.     public function getNombreCompletoRepresentante(): string
  189.     {
  190.         return $this->nombreRepresentante ' ' $this->apellido1 ' ' $this->apellido2;
  191.     }
  192.     public function getUsername(): ?string
  193.     {
  194.         return $this->username;
  195.     }
  196.     public function setUsername(string $username): self
  197.     {
  198.         $this->username $username;
  199.         return $this;
  200.     }
  201.     /**
  202.      * A visual identifier that represents this user.
  203.      *
  204.      * @see UserInterface
  205.      */
  206.     public function getUserIdentifier(): string
  207.     {
  208.         return (string) $this->username;
  209.     }
  210.     /**
  211.      * @see UserInterface
  212.      */
  213.     public function getRoles(): array
  214.     {
  215.         $roles $this->roles;
  216.         // guarantee every user at least has ROLE_USER
  217.         $roles[] = 'ROLE_USER';
  218.         return array_unique($roles);
  219.     }
  220.     public function setRoles(array $roles): self
  221.     {
  222.         $this->roles $roles;
  223.         return $this;
  224.     }
  225.     /**
  226.      * @see PasswordAuthenticatedUserInterface
  227.      */
  228.     public function getPassword(): string
  229.     {
  230.         return $this->password;
  231.     }
  232.     public function setPassword(string $password): self
  233.     {
  234.         $this->password $password;
  235.         return $this;
  236.     }
  237.     /**
  238.      * @see UserInterface
  239.      */
  240.     public function eraseCredentials()
  241.     {
  242.         // If you store any temporary, sensitive data on the user, clear it here
  243.         $this->plainPassword null;
  244.         $this->cambioEmpresa null;
  245.     }
  246.     public function getCodigo(): ?string
  247.     {
  248.         return $this->codigo;
  249.     }
  250.     public function setCodigo(?string $codigo): self
  251.     {
  252.         $this->codigo $codigo;
  253.         return $this;
  254.     }
  255.     public function getNombre(): ?string
  256.     {
  257.         return $this->nombre;
  258.     }
  259.     public function setNombre(?string $nombre): self
  260.     {
  261.         $this->nombre $nombre;
  262.         return $this;
  263.     }
  264.     public function getApellido1(): ?string
  265.     {
  266.         return $this->apellido1;
  267.     }
  268.     public function setApellido1(?string $apellido1): self
  269.     {
  270.         $this->apellido1 $apellido1;
  271.         return $this;
  272.     }
  273.     public function getApellido2(): ?string
  274.     {
  275.         return $this->apellido2;
  276.     }
  277.     public function setApellido2(?string $apellido2): self
  278.     {
  279.         $this->apellido2 $apellido2;
  280.         return $this;
  281.     }
  282.     public function getCp(): ?string
  283.     {
  284.         return $this->cp;
  285.     }
  286.     public function setCp(?string $cp): self
  287.     {
  288.         $this->cp $cp;
  289.         return $this;
  290.     }
  291.     public function getObservaciones(): ?string
  292.     {
  293.         return $this->observaciones;
  294.     }
  295.     public function setObservaciones(?string $observaciones): self
  296.     {
  297.         $this->observaciones $observaciones;
  298.         return $this;
  299.     }
  300.     public function getNif(): ?string
  301.     {
  302.         return $this->nif;
  303.     }
  304.     public function setNif(?string $nif): self
  305.     {
  306.         $this->nif $nif;
  307.         return $this;
  308.     }
  309.     public function isIsActivo(): ?bool
  310.     {
  311.         return $this->isActivo;
  312.     }
  313.     public function setIsActivo(?bool $isActivo): self
  314.     {
  315.         $this->isActivo $isActivo;
  316.         return $this;
  317.     }
  318.     public function getDescripcion(): string
  319.     {
  320.         return $this->descripcion;
  321.     }
  322.     public function setDescripcion(?string $descripcion): self
  323.     {
  324.         $this->descripcion $descripcion;
  325.         return $this;
  326.     }
  327.     public function getPlainPassword(): ?string
  328.     {
  329.         return $this->plainPassword;
  330.     }
  331.     public function setPlainPassword(?string $plainPassword): self
  332.     {
  333.         $this->plainPassword $plainPassword;
  334.         return $this;
  335.     }
  336.     public function getTipo(): ?string
  337.     {
  338.         return $this->tipo;
  339.     }
  340.     public function setTipo(string $tipo): self
  341.     {
  342.         $this->tipo $tipo;
  343.         return $this;
  344.     }
  345.     /**
  346.      * @return Collection<int, Email>
  347.      */
  348.     public function getEmails(): Collection
  349.     {
  350.         return $this->emails;
  351.     }
  352.     public function addEmail(Email $email): self
  353.     {
  354.         if (!$this->emails->contains($email)) {
  355.             $this->emails->add($email);
  356.             $email->setUser($this);
  357.         }
  358.         return $this;
  359.     }
  360.     public function removeEmail(Email $email): self
  361.     {
  362.         if ($this->emails->removeElement($email)) {
  363.             // set the owning side to null (unless already changed)
  364.             if ($email->getUser() === $this) {
  365.                 $email->setUser(null);
  366.             }
  367.         }
  368.         return $this;
  369.     }
  370.     /**
  371.      * @return Collection<int, Telefono>
  372.      */
  373.     public function getTelefonos(): Collection
  374.     {
  375.         return $this->telefonos;
  376.     }
  377.     public function addTelefono(Telefono $telefono): self
  378.     {
  379.         if (!$this->telefonos->contains($telefono)) {
  380.             $this->telefonos->add($telefono);
  381.             $telefono->setUser($this);
  382.         }
  383.         return $this;
  384.     }
  385.     public function removeTelefono(Telefono $telefono): self
  386.     {
  387.         if ($this->telefonos->removeElement($telefono)) {
  388.             // set the owning side to null (unless already changed)
  389.             if ($telefono->getUser() === $this) {
  390.                 $telefono->setUser(null);
  391.             }
  392.         }
  393.         return $this;
  394.     }
  395.     /**
  396.      * @return Collection<int, Direccion>
  397.      */
  398.     public function getDirecciones(): Collection
  399.     {
  400.         return $this->direcciones;
  401.     }
  402.     public function addDireccione(Direccion $direccione): self
  403.     {
  404.         if (!$this->direcciones->contains($direccione)) {
  405.             $this->direcciones->add($direccione);
  406.             $direccione->setUser($this);
  407.         }
  408.         return $this;
  409.     }
  410.     public function removeDireccione(Direccion $direccione): self
  411.     {
  412.         if ($this->direcciones->removeElement($direccione)) {
  413.             // set the owning side to null (unless already changed)
  414.             if ($direccione->getUser() === $this) {
  415.                 $direccione->setUser(null);
  416.             }
  417.         }
  418.         return $this;
  419.     }
  420.     public function getNombreRepresentante(): ?string
  421.     {
  422.         return $this->nombreRepresentante;
  423.     }
  424.     public function setNombreRepresentante(?string $nombreRepresentante): self
  425.     {
  426.         $this->nombreRepresentante $nombreRepresentante;
  427.         return $this;
  428.     }
  429.     public function getEmailRepresentante(): ?string
  430.     {
  431.         return $this->emailRepresentante;
  432.     }
  433.     public function setEmailRepresentante(?string $emailRepresentante): self
  434.     {
  435.         $this->emailRepresentante $emailRepresentante;
  436.         return $this;
  437.     }
  438.     public function getTelefonoRepresentante(): ?string
  439.     {
  440.         return $this->telefonoRepresentante;
  441.     }
  442.     public function setTelefonoRepresentante(?string $telefonoRepresentante): self
  443.     {
  444.         $this->telefonoRepresentante $telefonoRepresentante;
  445.         return $this;
  446.     }
  447.     public function getNifRepresentante(): ?string
  448.     {
  449.         return $this->nifRepresentante;
  450.     }
  451.     public function setNifRepresentante(?string $nifRepresentante): self
  452.     {
  453.         $this->nifRepresentante $nifRepresentante;
  454.         return $this;
  455.     }
  456.     public function getCargoRepresentante(): ?string
  457.     {
  458.         return $this->cargoRepresentante;
  459.     }
  460.     public function setCargoRepresentante(?string $cargoRepresentante): self
  461.     {
  462.         $this->cargoRepresentante $cargoRepresentante;
  463.         return $this;
  464.     }
  465.     public function getSeguimiento(): array
  466.     {
  467.         return $this->seguimiento;
  468.     }
  469.     public function setSeguimiento(?array $seguimiento): self
  470.     {
  471.         $this->seguimiento $seguimiento;
  472.         return $this;
  473.     }
  474.     public function getEmpresa(): ?Empresa
  475.     {
  476.         return $this->empresa;
  477.     }
  478.     public function setEmpresa(?Empresa $empresa): self
  479.     {
  480.         $this->empresa $empresa;
  481.         return $this;
  482.     }
  483.     public function getCambioEmpresa(): ?Empresa
  484.     {
  485.         return $this->cambioEmpresa;
  486.     }
  487.     public function setCambioEmpresa(?Empresa $cambioEmpresa): self
  488.     {
  489.         $this->cambioEmpresa $cambioEmpresa;
  490.         return $this;
  491.     }
  492.     /**
  493.      * @return Collection<int, Notificacion>
  494.      */
  495.     public function getNotificaciones(): Collection
  496.     {
  497.         return $this->notificaciones;
  498.     }
  499.     public function addNotificacione(Notificacion $notificacione): self
  500.     {
  501.         if (!$this->notificaciones->contains($notificacione)) {
  502.             $this->notificaciones->add($notificacione);
  503.             $notificacione->setUser($this);
  504.         }
  505.         return $this;
  506.     }
  507.     public function removeNotificacione(Notificacion $notificacione): self
  508.     {
  509.         if ($this->notificaciones->removeElement($notificacione)) {
  510.             // set the owning side to null (unless already changed)
  511.             if ($notificacione->getUser() === $this) {
  512.                 $notificacione->setUser(null);
  513.             }
  514.         }
  515.         return $this;
  516.     }
  517.     public function getGrupoEmpresa(): ?GrupoEmpresa
  518.     {
  519.         return $this->grupoEmpresa;
  520.     }
  521.     public function setGrupoEmpresa(?GrupoEmpresa $grupoEmpresa): static
  522.     {
  523.         $this->grupoEmpresa $grupoEmpresa;
  524.         return $this;
  525.     }
  526. }