src/Entity/UsuarioHermes.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\RolEnum;
  4. use App\Validator as ConforcatAssert;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Gedmo\Mapping\Annotation as Gedmo;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. /**
  14. * @ORM\Entity
  15. * @ORM\Table(name="usuario_hermes")
  16. * @method string getUserIdentifier()
  17. */
  18. class UsuarioHermes extends BaseEntity implements UserInterface
  19. {
  20. /**
  21. * @ORM\Id
  22. * @ORM\Column(type="integer")
  23. * @ORM\GeneratedValue(strategy="AUTO")
  24. */
  25. private $id;
  26. /**
  27. * @ORM\Column(type="string", length=255, nullable=true)
  28. */
  29. private $name;
  30. /**
  31. * @ORM\Column(type="string", nullable=true)
  32. */
  33. private $surname;
  34. /**
  35. * @ORM\Column(type="string", nullable=true)
  36. */
  37. private $username;
  38. /**
  39. * @ORM\Column(type="string", nullable=true)
  40. */
  41. private $password;
  42. /**
  43. * @ORM\Column(type="string", nullable=true, name="metodo_authenticacion")
  44. */
  45. private $metodoAuthenticacion;
  46. /**
  47. * @ORM\Column(type="string", nullable=true)
  48. */
  49. private $telefono;
  50. /**
  51. * @ORM\Column(type="string", nullable=true, name="user_token")
  52. */
  53. private $userToken;
  54. /**
  55. * @ORM\Column(type="datetime", nullable=true, name="deleted_at")
  56. */
  57. private $deletedAt;
  58. /**
  59. * @ORM\Column(type="datetime", nullable=false, name="updated_at", options={"default":"2024-01-01 00:00:00"})
  60. * @Gedmo\Timestampable(on="update")
  61. */
  62. private $updatedAt;
  63. /**
  64. * @ORM\Column(type="datetime", nullable=false, name="created_at", options={"default":"2024-01-01 00:00:00"})
  65. * @Gedmo\Timestampable(on="create")
  66. */
  67. private $createdAt;
  68. /**
  69. * @ORM\Column(type="array", nullable=true)
  70. */
  71. private $roles;
  72. /**
  73. * @ORM\Column(type="string", nullable=true)
  74. */
  75. private $salt;
  76. /**
  77. * @ORM\Column(type="string", unique=true, nullable=true)
  78. * @Assert\Email()
  79. */
  80. private $email;
  81. /**
  82. * @ConforcatAssert\ConstraintIdentidad()
  83. * @ORM\Column(type="string", unique=true, nullable=true)
  84. */
  85. private $dni;
  86. /**
  87. * @ORM\Column(type="boolean", nullable=true, name="aceptaLodp")
  88. */
  89. private $aceptaLodp;
  90. /**
  91. * @ORM\Column(type="boolean", nullable=true, options={"default":1})
  92. */
  93. private $active=1;
  94. /**
  95. * @ORM\Column(type="string", nullable=true)
  96. */
  97. private $tokenValid;
  98. /**
  99. * @ORM\Column(type="datetime", nullable=true, name="password_changed_at")
  100. * @Gedmo\Timestampable(on="change", field="password")
  101. */
  102. private $passwordChangedAt;
  103. /**
  104. * @ORM\OneToMany(
  105. * targetEntity=\App\Entity\PropietarioContenidoHasUsuarioHermes::class,
  106. * mappedBy="usuarioHermes",
  107. * cascade={"all"}
  108. * )
  109. */
  110. private $propietarioContenidoHasUsuarioHermes;
  111. /**
  112. * @ORM\OneToMany(targetEntity=\App\Entity\MensajeLeidoForo::class, mappedBy="usuario")
  113. */
  114. private $mensajeLeidoForos;
  115. private $plainPassword;
  116. /**
  117. * @ORM\ManyToMany(targetEntity=\App\Entity\SolicitudAccionFormativa::class, inversedBy="usuarioHermesInteres")
  118. * @ORM\JoinTable(
  119. * name="muestra_interes_solicitud",
  120. * joinColumns={@ORM\JoinColumn(name="usuario_hermes_id", referencedColumnName="id", nullable=false)},
  121. * inverseJoinColumns={@ORM\JoinColumn(name="solicitud_accion_formativa_id", referencedColumnName="id", nullable=false)}
  122. * )
  123. */
  124. private $solicitudesInteres;
  125. /**
  126. * @ORM\ManyToMany(targetEntity=\App\Entity\SolicitudAccionFormativa::class, inversedBy="usuarioHermesFavorito")
  127. * @ORM\JoinTable(
  128. * name="registra_favorito_solicitud",
  129. * joinColumns={@ORM\JoinColumn(name="usuario_hermes_id", referencedColumnName="id", nullable=false)},
  130. * inverseJoinColumns={@ORM\JoinColumn(name="solicitud_accion_formativa_id", referencedColumnName="id", nullable=false)}
  131. * )
  132. */
  133. private $solicitudesFavorito;
  134. /**
  135. * @ORM\ManyToMany(targetEntity=\App\Entity\CentroFormacion::class, mappedBy="usuarioHermesFavorito")
  136. */
  137. private $centrosFormacionFavorito;
  138. /**
  139. * @ORM\ManyToMany(targetEntity=\App\Entity\Grupo::class, inversedBy="usuarioHermesInteres")
  140. * @ORM\JoinTable(
  141. * name="muestra_interes_grupo",
  142. * joinColumns={@ORM\JoinColumn(name="usuario_hermes_id", referencedColumnName="id", nullable=false)},
  143. * inverseJoinColumns={@ORM\JoinColumn(name="grupo_id", referencedColumnName="id", nullable=false)}
  144. * )
  145. */
  146. private $gruposInteres;
  147. /**
  148. * @ORM\ManyToMany(targetEntity=\App\Entity\Grupo::class, inversedBy="usuarioHermesFavorito")
  149. * @ORM\JoinTable(
  150. * name="registra_favorito_grupo",
  151. * joinColumns={@ORM\JoinColumn(name="usuario_hermes_id", referencedColumnName="id", nullable=false)},
  152. * inverseJoinColumns={@ORM\JoinColumn(name="grupo_id", referencedColumnName="id", nullable=false)}
  153. * )
  154. */
  155. private $gruposFavorito;
  156. /**
  157. * @ORM\ManyToMany(targetEntity=\App\Entity\EntidadColaboradora::class, inversedBy="usuarioHermesFavorito")
  158. * @ORM\JoinTable(
  159. * name="usuario_hermes_has_favorito_entidad_colaboradora",
  160. * joinColumns={@ORM\JoinColumn(name="usuario_hermes_id", referencedColumnName="id", nullable=false)},
  161. * inverseJoinColumns={@ORM\JoinColumn(name="entidad_colaboradora_id", referencedColumnName="id", nullable=false)}
  162. * )
  163. */
  164. private $entidadColaboradorasFavorito;
  165. /**
  166. * @ORM\ManyToMany(targetEntity=\App\Entity\Formador::class, inversedBy="usuarioHermesFavorito")
  167. * @ORM\JoinTable(
  168. * name="usuario_hermes_has_favorito_formador",
  169. * joinColumns={@ORM\JoinColumn(name="usuario_hermes_id", referencedColumnName="id", nullable=false)},
  170. * inverseJoinColumns={@ORM\JoinColumn(name="formador_id", referencedColumnName="id", nullable=false)}
  171. * )
  172. */
  173. private $formadoresFavorito;
  174. public function __construct()
  175. {
  176. $this->propietarioContenidoHasUsuarioHermes = new ArrayCollection();
  177. $this->solicitudesFavorito = new ArrayCollection();
  178. $this->solicitudesInteres = new ArrayCollection();
  179. $this->gruposFavorito = new ArrayCollection();
  180. $this->gruposInteres = new ArrayCollection();
  181. $this->entidadColaboradorasFavorito = new ArrayCollection();
  182. $this->formadoresFavorito = new ArrayCollection();
  183. $this->centrosFormacionFavorito = new ArrayCollection();
  184. $this->mensajeLeidoForos = new ArrayCollection();
  185. }
  186. public function getId(): ?int
  187. {
  188. return $this->id;
  189. }
  190. public function setId($id)
  191. {
  192. $this->id = $id;
  193. return $this;
  194. }
  195. public function getName(): ?string
  196. {
  197. return $this->name;
  198. }
  199. public function setName(?string $name): static
  200. {
  201. $this->name = $name;
  202. return $this;
  203. }
  204. public function getSurname(): ?string
  205. {
  206. return $this->surname;
  207. }
  208. public function setSurname(?string $surname): static
  209. {
  210. $this->surname = $surname;
  211. return $this;
  212. }
  213. public function getUsername(): ?string
  214. {
  215. return $this->username;
  216. }
  217. public function setUsername(?string $username): static
  218. {
  219. $this->username = $username;
  220. return $this;
  221. }
  222. public function getPassword(): ?string
  223. {
  224. return $this->password;
  225. }
  226. public function setPassword(?string $password): static
  227. {
  228. $this->password = $password;
  229. $this->setPasswordChangedAt(new DateTime());
  230. return $this;
  231. }
  232. public function getMetodoAuthenticacion(): ?string
  233. {
  234. return $this->metodoAuthenticacion;
  235. }
  236. public function setMetodoAuthenticacion(?string $metodoAuthenticacion): static
  237. {
  238. $this->metodoAuthenticacion = $metodoAuthenticacion;
  239. return $this;
  240. }
  241. public function getTelefono(): ?string
  242. {
  243. return $this->telefono;
  244. }
  245. public function setTelefono(?string $telefono): static
  246. {
  247. $this->telefono = $telefono;
  248. return $this;
  249. }
  250. public function getUserToken(): ?string
  251. {
  252. return $this->userToken;
  253. }
  254. public function setUserToken(?string $userToken): static
  255. {
  256. $this->userToken = $userToken;
  257. return $this;
  258. }
  259. public function getDeletedAt(): ?\DateTimeInterface
  260. {
  261. return $this->deletedAt;
  262. }
  263. public function setDeletedAt(?\DateTimeInterface $deletedAt): static
  264. {
  265. $this->deletedAt = $deletedAt;
  266. return $this;
  267. }
  268. public function getUpdatedAt(): ?\DateTimeInterface
  269. {
  270. return $this->updatedAt;
  271. }
  272. public function setUpdatedAt(\DateTimeInterface $updatedAt): static
  273. {
  274. $this->updatedAt = $updatedAt;
  275. return $this;
  276. }
  277. public function getCreatedAt(): ?\DateTimeInterface
  278. {
  279. return $this->createdAt;
  280. }
  281. public function setCreatedAt(\DateTimeInterface $createdAt): static
  282. {
  283. $this->createdAt = $createdAt;
  284. return $this;
  285. }
  286. public function getRoles(): ?array
  287. {
  288. return $this->roles;
  289. }
  290. public function setRoles(?array $roles): static
  291. {
  292. $this->roles = $roles;
  293. return $this;
  294. }
  295. public function getSalt(): ?string
  296. {
  297. return $this->salt;
  298. }
  299. public function setSalt(?string $salt): static
  300. {
  301. $this->salt = $salt;
  302. return $this;
  303. }
  304. public function getEmail(): ?string
  305. {
  306. return $this->email;
  307. }
  308. public function setEmail(?string $email): static
  309. {
  310. $this->email = $email;
  311. return $this;
  312. }
  313. public function getDni(): ?string
  314. {
  315. return $this->dni;
  316. }
  317. public function setDni(?string $dni): static
  318. {
  319. $this->dni = $dni;
  320. return $this;
  321. }
  322. public function isAceptaLodp(): ?bool
  323. {
  324. return $this->aceptaLodp;
  325. }
  326. public function setAceptaLodp(?bool $aceptaLodp): static
  327. {
  328. $this->aceptaLodp = $aceptaLodp;
  329. return $this;
  330. }
  331. public function isActive(): ?bool
  332. {
  333. return $this->active;
  334. }
  335. public function setActive(?bool $active): static
  336. {
  337. $this->active = $active;
  338. return $this;
  339. }
  340. public function getNombreCompleto(): ?string
  341. {
  342. return trim($this->getName() . ' ' . $this->getSurname())??'Sin Identificar';
  343. }
  344. /**
  345. * @return Collection<int, PropietarioContenidoHasUsuarioHermes>
  346. */
  347. public function getPropietarioContenidoHasUsuarioHermes(): Collection
  348. {
  349. return $this->propietarioContenidoHasUsuarioHermes;
  350. }
  351. public function addPropietarioContenidoHasUsuarioHerme(PropietarioContenidoHasUsuarioHermes $propietarioContenidoHasUsuarioHerme): static
  352. {
  353. if (!$this->propietarioContenidoHasUsuarioHermes->contains($propietarioContenidoHasUsuarioHerme)) {
  354. $this->propietarioContenidoHasUsuarioHermes->add($propietarioContenidoHasUsuarioHerme);
  355. $propietarioContenidoHasUsuarioHerme->setUsuarioHermes($this);
  356. }
  357. return $this;
  358. }
  359. public function removePropietarioContenidoHasUsuarioHerme(PropietarioContenidoHasUsuarioHermes $propietarioContenidoHasUsuarioHerme): static
  360. {
  361. if ($this->propietarioContenidoHasUsuarioHermes->removeElement($propietarioContenidoHasUsuarioHerme)) {
  362. // set the owning side to null (unless already changed)
  363. if ($propietarioContenidoHasUsuarioHerme->getUsuarioHermes() === $this) {
  364. $propietarioContenidoHasUsuarioHerme->setUsuarioHermes(null);
  365. }
  366. }
  367. return $this;
  368. }
  369. public function getPropietarioContenidos(): ArrayCollection
  370. {
  371. return $this->getPropietarioContenidoHasUsuarioHermes()?->map(fn($p) => $p->getPropietarioContenido());
  372. }
  373. public function eraseCredentials()
  374. {
  375. // TODO: Implement eraseCredentials() method.
  376. }
  377. /**
  378. * @return Collection<int, SolicitudAccionFormativa>
  379. */
  380. public function getSolicitudesFavorito(): Collection
  381. {
  382. return $this->solicitudesFavorito;
  383. }
  384. public function addSolicitudesFavorito(SolicitudAccionFormativa $solicitudesFavorito): static
  385. {
  386. if (!$this->solicitudesFavorito->contains($solicitudesFavorito)) {
  387. $this->solicitudesFavorito->add($solicitudesFavorito);
  388. }
  389. return $this;
  390. }
  391. public function removeSolicitudesFavorito(SolicitudAccionFormativa $solicitudesFavorito): static
  392. {
  393. $this->solicitudesFavorito->removeElement($solicitudesFavorito);
  394. return $this;
  395. }
  396. /**
  397. * @return Collection<int, SolicitudAccionFormativa>
  398. */
  399. public function getSolicitudesInteres(): Collection
  400. {
  401. return $this->solicitudesInteres;
  402. }
  403. public function addSolicitudesIntere(SolicitudAccionFormativa $solicitudesIntere): static
  404. {
  405. if (!$this->solicitudesInteres->contains($solicitudesIntere)) {
  406. $this->solicitudesInteres->add($solicitudesIntere);
  407. }
  408. return $this;
  409. }
  410. public function removeSolicitudesIntere(SolicitudAccionFormativa $solicitudesIntere): static
  411. {
  412. $this->solicitudesInteres->removeElement($solicitudesIntere);
  413. return $this;
  414. }
  415. /**
  416. * @return Collection<int, Grupo>
  417. */
  418. public function getGruposFavorito(): Collection
  419. {
  420. return $this->gruposFavorito;
  421. }
  422. public function addGruposFavorito(Grupo $grupo): static
  423. {
  424. if (!$this->gruposFavorito->contains($grupo)) {
  425. $this->gruposFavorito->add($grupo);
  426. }
  427. return $this;
  428. }
  429. public function removeGruposFavorito(Grupo $grupo): static
  430. {
  431. $this->gruposFavorito->removeElement($grupo);
  432. return $this;
  433. }
  434. /**
  435. * Get all favorites (both solicitudes and grupos)
  436. *
  437. * @return Collection
  438. */
  439. public function getFavoritosCursos(): Collection
  440. {
  441. $result = new ArrayCollection();
  442. // Add solicitudes
  443. foreach ($this->getSolicitudesFavorito() as $solicitud) {
  444. $result->add($solicitud);
  445. }
  446. // Add grupos
  447. foreach ($this->getGruposFavorito() as $grupo) {
  448. $result->add($grupo);
  449. }
  450. return $result;
  451. }
  452. /**
  453. * @return Collection<int, EntidadColaboradora>
  454. */
  455. public function getEntidadColaboradorasFavorito(): Collection
  456. {
  457. return $this->entidadColaboradorasFavorito;
  458. }
  459. public function addEntidadColaboradorasFavorito(EntidadColaboradora $entidadColaboradora): static
  460. {
  461. if (!$this->entidadColaboradorasFavorito->contains($entidadColaboradora)) {
  462. $this->entidadColaboradorasFavorito->add($entidadColaboradora);
  463. }
  464. return $this;
  465. }
  466. public function removeEntidadColaboradorasFavorito(EntidadColaboradora $entidadColaboradora): static
  467. {
  468. $this->entidadColaboradorasFavorito->removeElement($entidadColaboradora);
  469. return $this;
  470. }
  471. /**
  472. * @return Collection<int, Formador>
  473. */
  474. public function getFormadoresFavorito(): Collection
  475. {
  476. return $this->formadoresFavorito;
  477. }
  478. public function addFormadoresFavorito(Formador $formador): static
  479. {
  480. if (!$this->formadoresFavorito->contains($formador)) {
  481. $this->formadoresFavorito->add($formador);
  482. }
  483. return $this;
  484. }
  485. public function removeFormadoresFavorito(Formador $formador): static
  486. {
  487. $this->formadoresFavorito->removeElement($formador);
  488. return $this;
  489. }
  490. /**
  491. * @return Collection<int, Grupo>
  492. */
  493. public function getGruposInteres(): Collection
  494. {
  495. return $this->gruposInteres;
  496. }
  497. public function addGruposIntere(Grupo $grupo): static
  498. {
  499. if (!$this->gruposInteres->contains($grupo)) {
  500. $this->gruposInteres->add($grupo);
  501. }
  502. return $this;
  503. }
  504. public function removeGruposIntere(Grupo $grupo): static
  505. {
  506. $this->gruposInteres->removeElement($grupo);
  507. return $this;
  508. }
  509. /**
  510. * Get all interests (both solicitudes and grupos)
  511. *
  512. * @return Collection
  513. */
  514. public function getIntereses(): Collection
  515. {
  516. $result = new ArrayCollection();
  517. // Add solicitudes
  518. foreach ($this->getSolicitudesInteres() as $solicitud) {
  519. $result->add($solicitud);
  520. }
  521. // Add grupos
  522. foreach ($this->getGruposInteres() as $grupo) {
  523. $result->add($grupo);
  524. }
  525. return $result;
  526. }
  527. public function getTokenValid(): ?string
  528. {
  529. return $this->tokenValid;
  530. }
  531. public function setTokenValid(?string $tokenValid): static
  532. {
  533. $this->tokenValid = $tokenValid;
  534. return $this;
  535. }
  536. public function getPlainPassword()
  537. {
  538. return $this->plainPassword;
  539. }
  540. public function setPlainPassword($plainPassword)
  541. {
  542. $this->plainPassword = $plainPassword;
  543. return $this;
  544. }
  545. public function getPropietarioContenidoByClass ($class){
  546. return $this->getPropietarioContenidos()->filter(fn($p) => $p instanceof $class);
  547. }
  548. /**
  549. * Obtiene el PropietarioContenido del usuario para el rol especificado
  550. *
  551. * @param string $rol
  552. * @return PropietarioContenido|null
  553. */
  554. public function getPropietarioContenidoByRol(string $rol): ?PropietarioContenido
  555. {
  556. $class = RolEnum::getClass($rol);
  557. $propietarioContenidos = $this->getPropietarioContenidoByClass($class);
  558. return $propietarioContenidos->isEmpty() ? null : $propietarioContenidos->first();
  559. }
  560. /**
  561. * @return Collection<int, CentroFormacion>
  562. */
  563. public function getCentrosFormacionFavorito(): Collection
  564. {
  565. return $this->centrosFormacionFavorito;
  566. }
  567. public function addCentrosFormacionFavorito(CentroFormacion $centrosFormacionFavorito): static
  568. {
  569. if (!$this->centrosFormacionFavorito->contains($centrosFormacionFavorito)) {
  570. $this->centrosFormacionFavorito->add($centrosFormacionFavorito);
  571. $centrosFormacionFavorito->addUsuarioHermesFavorito($this);
  572. }
  573. return $this;
  574. }
  575. public function removeCentrosFormacionFavorito(CentroFormacion $centrosFormacionFavorito): static
  576. {
  577. if ($this->centrosFormacionFavorito->removeElement($centrosFormacionFavorito)) {
  578. $centrosFormacionFavorito->removeUsuarioHermesFavorito($this);
  579. }
  580. return $this;
  581. }
  582. public function isCentroFormacionFavorito(?CentroFormacion $centroFormacion): bool
  583. {
  584. if(!$centroFormacion) return false;
  585. return $this->getCentrosFormacionFavorito()->contains($centroFormacion);
  586. }
  587. public function getEmpresa(): ?Empresa
  588. {
  589. $propietarioContenidos = $this->getPropietarioContenidos();
  590. if (!$propietarioContenidos) return null;
  591. foreach ($propietarioContenidos as $propietarioContenido) {
  592. if ($propietarioContenido instanceof EmpresaPropietarioContenido) {
  593. return $propietarioContenido->getEmpresa();
  594. }
  595. }
  596. return null;
  597. }
  598. /**
  599. * Obtiene los roles del usuario basados en sus PropietarioContenido
  600. *
  601. * @return array Array de roles (strings)
  602. */
  603. public function getRolesByPropietarioContenido(): array
  604. {
  605. $roles = [];
  606. $propietarioContenidos = $this->getPropietarioContenidos();
  607. if (!$propietarioContenidos) {
  608. return $roles;
  609. }
  610. foreach ($propietarioContenidos as $propietarioContenido) {
  611. $rol = RolEnum::getRolFromClass(get_class($propietarioContenido));
  612. if ($rol && !in_array($rol, $roles)) {
  613. $roles[] = $rol;
  614. }
  615. }
  616. return $roles;
  617. }
  618. public function getEntidadAsociadaByRol(?string $rol): ?Entidad
  619. {
  620. return !$rol ? null : $this->getPropietarioContenidoByRol($rol)?->getEntidadAsociada();
  621. }
  622. public function getPasswordChangedAt(): ?\DateTimeInterface
  623. {
  624. return $this->passwordChangedAt;
  625. }
  626. public function setPasswordChangedAt(?\DateTimeInterface $passwordChangedAt): static
  627. {
  628. $this->passwordChangedAt = $passwordChangedAt;
  629. return $this;
  630. }
  631. /**
  632. * @return Collection<int, MensajeLeidoForo>
  633. */
  634. public function getMensajeLeidoForos(): Collection
  635. {
  636. return $this->mensajeLeidoForos;
  637. }
  638. public function addMensajeLeidoForo(MensajeLeidoForo $mensajeLeidoForo): static
  639. {
  640. if (!$this->mensajeLeidoForos->contains($mensajeLeidoForo)) {
  641. $this->mensajeLeidoForos->add($mensajeLeidoForo);
  642. $mensajeLeidoForo->setUsuario($this);
  643. }
  644. return $this;
  645. }
  646. public function removeMensajeLeidoForo(MensajeLeidoForo $mensajeLeidoForo): static
  647. {
  648. if ($this->mensajeLeidoForos->removeElement($mensajeLeidoForo)) {
  649. // set the owning side to null (unless already changed)
  650. if ($mensajeLeidoForo->getUsuario() === $this) {
  651. $mensajeLeidoForo->setUsuario(null);
  652. }
  653. }
  654. return $this;
  655. }
  656. }