src/Controller/SecurityController.php line 34

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.     /**
  25.      * @Route("/inicio", name="ruta_inicio")
  26.      */
  27.     public function inicioAction(AuthenticationUtils $authenticationUtils): Response
  28.     {
  29.         $em $this->getDoctrine()->getManager();
  30.         $configuration $em->getRepository(Configuration::class)->findOneBy([]);
  31.         return $this->render('login/login.html.twig', [
  32.             'last_username' => $authenticationUtils->getLastUsername(),
  33.             'error'         => $authenticationUtils->getLastAuthenticationError(),
  34.             'configuration' => $configuration,
  35.         ]);
  36.     }
  37.     
  38.     /**
  39.      * Portal principal con los 3 accesos (ManteVenues, ManteCatering, ManteAV)
  40.      *
  41.      * @Route("/login", name="login")
  42.      */
  43.     public function loginAction(AuthenticationUtils $authenticationUtilsRequest $request): Response
  44.     {
  45.         $em $this->getDoctrine()->getManager();
  46.         // Recupera la configuración general (solo debe haber 1)
  47.         $configuration $em->getRepository(Configuration::class)->findOneBy([]);
  48.         return $this->render('inicio/inicio.html.twig', [
  49.             'last_username' => $authenticationUtils->getLastUsername(),
  50.             'error'         => $authenticationUtils->getLastAuthenticationError(),
  51.             'configuration' => $configuration,
  52.         ]);
  53.     }
  54.     /**
  55.      * @Route("/login/{type}", name="app_login_branded")
  56.      */
  57.     public function loginBranded(string $typeRequest $requestAuthenticationUtils $authenticationUtils): Response
  58.     {
  59.         $em $this->getDoctrine()->getManager();
  60.         // 1. Buscamos la configuración de marca para este tipo (venues, catering o av)
  61.         $branding $em->getRepository(SettingsBranding::class)->findOneBy([
  62.             'accessType' => $type,
  63.             'isActive'   => true
  64.         ]);
  65.         // Si no existe esa configuración, volvemos al portal
  66.         if (!$branding) {
  67.             return $this->redirectToRoute('ruta_inicio');
  68.         }
  69.         // 2. Guardamos en sesión para que el Authenticator sepa a dónde volver si falla
  70.         $request->getSession()->set('current_access_type'$type);
  71.         return $this->render('login/login_unified.html.twig', [
  72.             'last_username' => $authenticationUtils->getLastUsername(),
  73.             'error'         => $authenticationUtils->getLastAuthenticationError(),
  74.             'branding'      => $branding,
  75.             'type'          => $type
  76.         ]);
  77.     }
  78.     /**
  79.      * Cambiar el tipo de acceso (venues / av / catering) y volver al dashboard
  80.      *
  81.      * @Route("/switch-access/{type}", name="switch_access")
  82.      */
  83.     public function switchAccess(string $typeRequest $request): Response
  84.     {
  85.         // Solo aceptamos estos tipos. Ajusta si quieres añadir más.
  86.         $allowed = ['venues''av''catering'];
  87.         if (!in_array($type$allowedtrue)) {
  88.             throw $this->createNotFoundException();
  89.         }
  90.         // Guardar el nuevo tipo en sesión
  91.         $request->getSession()->set('current_access_type'$type);
  92.         // Redirigir al dashboard principal (pon aquí tu ruta real)
  93.         return $this->redirectToRoute('homepage');
  94.         // ^^^ cambia 'home' por la ruta que uses para tu panel principal
  95.     }
  96.     /**
  97.      * @Route("/kickout", name="kickout")
  98.      */
  99.     public function KickoutAction(Request $request): Response
  100.     {
  101.         $this->get('security.token_storage')->setToken(null);
  102.         $session $request->getSession();
  103.         $session->invalidate();
  104.         return $this->render('kickout/kickout.html.twig');
  105.     }
  106.     /**
  107.      * @Route("/login_check", name="login_check")
  108.      */
  109.     public function loginCheckAction(): void
  110.     {
  111.         // lo gestiona el LoginAuthenticator (Guard)
  112.     }
  113.     /**
  114.      * @Route("/logout", name="logout")
  115.      */
  116.     public function logoutAction(): void
  117.     {
  118.         // gestionado por el firewall
  119.     }
  120.     public function getLicencias(): ?int
  121.     {
  122.         $repository    $this->em->getRepository(Configuration::class);
  123.         $configuration $repository->findOneById(1);
  124.         return $configuration->getLicenses();
  125.     }
  126. }