src/Controller/SecurityController.php line 48

Open in your IDE?
  1. <?php
  2. /**
  3. * Created by Mediterranean Develup Solutions.
  4. * User: carlos.rojas@develup.solutions
  5. * Date: 12/06/2017
  6. * Time: 17:44
  7. */
  8. namespace App\Controller;
  9. use App\Entity\Configuration;
  10. use App\Entity\SettingsBranding;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  16. use Symfony\Component\HttpFoundation\Response;
  17. class SecurityController extends AbstractController
  18. {
  19. protected EntityManagerInterface $em;
  20. public function __construct(EntityManagerInterface $em)
  21. {
  22. $this->em = $em;
  23. }
  24. #[Route(path: '/inicio', name: 'ruta_inicio')]
  25. public function inicioAction(AuthenticationUtils $authenticationUtils): Response
  26. {
  27. $em = $this->getDoctrine()->getManager();
  28. $configuration = $em->getRepository(Configuration::class)->findOneBy([]);
  29. return $this->render('login/login.html.twig', [
  30. 'last_username' => $authenticationUtils->getLastUsername(),
  31. 'error' => $authenticationUtils->getLastAuthenticationError(),
  32. 'configuration' => $configuration,
  33. ]);
  34. }
  35. /**
  36. * Portal principal con los 3 accesos (ManteVenues, ManteCatering, ManteAV)
  37. */
  38. #[Route(path: '/login', name: 'login')]
  39. public function loginAction(AuthenticationUtils $authenticationUtils): Response
  40. {
  41. $em = $this->getDoctrine()->getManager();
  42. // Recupera la configuración general (solo debe haber 1)
  43. $configuration = $em->getRepository(Configuration::class)->findOneBy([]);
  44. return $this->render('inicio/inicio.html.twig', [
  45. 'last_username' => $authenticationUtils->getLastUsername(),
  46. 'error' => $authenticationUtils->getLastAuthenticationError(),
  47. 'configuration' => $configuration,
  48. ]);
  49. }
  50. #[Route(path: '/login/{type}', name: 'app_login_branded')]
  51. public function loginBranded(string $type, Request $request, AuthenticationUtils $authenticationUtils): Response
  52. {
  53. $em = $this->getDoctrine()->getManager();
  54. // 1. Buscamos la configuración de marca para este tipo (venues, catering o av)
  55. $branding = $em->getRepository(SettingsBranding::class)->findOneBy([
  56. 'accessType' => $type,
  57. 'isActive' => true
  58. ]);
  59. // Si no existe esa configuración, volvemos al portal
  60. if (!$branding) {
  61. return $this->redirectToRoute('ruta_inicio');
  62. }
  63. // 2. Guardamos en sesión para que el Authenticator sepa a dónde volver si falla
  64. $request->getSession()->set('current_access_type', $type);
  65. return $this->render('login/login_unified.html.twig', [
  66. 'last_username' => $authenticationUtils->getLastUsername(),
  67. 'error' => $authenticationUtils->getLastAuthenticationError(),
  68. 'branding' => $branding,
  69. 'type' => $type
  70. ]);
  71. }
  72. /**
  73. * Cambiar el tipo de acceso (venues / av / catering) y volver al dashboard
  74. */
  75. #[Route(path: '/switch-access/{type}', name: 'switch_access')]
  76. public function switchAccess(string $type, Request $request): Response
  77. {
  78. // Solo aceptamos estos tipos. Ajusta si quieres añadir más.
  79. $allowed = ['venues', 'av', 'catering'];
  80. if (!in_array($type, $allowed, true)) {
  81. throw $this->createNotFoundException();
  82. }
  83. // Guardar el nuevo tipo en sesión
  84. $request->getSession()->set('current_access_type', $type);
  85. // Redirigir al dashboard principal (pon aquí tu ruta real)
  86. return $this->redirectToRoute('homepage');
  87. // ^^^ cambia 'home' por la ruta que uses para tu panel principal
  88. }
  89. #[Route(path: '/kickout', name: 'kickout')]
  90. public function KickoutAction(Request $request): Response
  91. {
  92. $this->get('security.token_storage')->setToken(null);
  93. $session = $request->getSession();
  94. $session->invalidate();
  95. return $this->render('kickout/kickout.html.twig');
  96. }
  97. #[Route(path: '/login_check', name: 'login_check')]
  98. public function loginCheckAction(): void
  99. {
  100. // lo gestiona el LoginAuthenticator (Guard)
  101. }
  102. #[Route(path: '/logout', name: 'logout')]
  103. public function logoutAction(): void
  104. {
  105. // gestionado por el firewall
  106. }
  107. public function getLicencias(): ?int
  108. {
  109. $repository = $this->em->getRepository(Configuration::class);
  110. $configuration = $repository->findOneById(1);
  111. return $configuration->getLicenses();
  112. }
  113. }