src/Entity/User.php line 67

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