src/Services/SecurityCore.php line 102

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  4. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Twig\Environment;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. class SecurityCore
  12. {
  13.     private $token;
  14.     private $entityManager;
  15.     private $request;
  16.     private $template;
  17.     private $router;
  18.     private $redirect;
  19.     public function __construct(EntityManagerInterface $entityManagerTokenStorageInterface $tokenStorageRequestStack $requestStackEnvironment $templatingRouterInterface $router)
  20.     {
  21.         $this->token $tokenStorage;
  22.         $this->em $entityManager;
  23.         $this->request $requestStack;
  24.         $this->template $templating;
  25.         $this->router $router;
  26.     }
  27.     public function validationAccess()
  28.     {
  29.         // Evitar subrequests internos (ej: render(path(...)) desde templates Twig).
  30.         // En sub-requests el redirect + exit mataría el proceso completo causando about:blank.
  31.         if ($this->request->getMainRequest() !== $this->request->getCurrentRequest()) {
  32.             return;
  33.         }
  34.         // var_dump($this->request->getCurrentRequest()->get('_route_params'));exit;
  35.         $accion $this->em->getRepository(\App\Entity\SecAccion::class)->findOneBy(['ruta' => $this->request->getCurrentRequest()->get('_route')]);
  36.         if ($accion) {
  37.             $sesion $this->request->getSession();
  38.             $sesion->start();
  39.             $rutasModalesCCTV = ['app_seg_solicitud_cctv_cargar_reporte''app_seg_solicitud_cctv_validar_reporte'];
  40.             if (in_array($this->request->getCurrentRequest()->get('_route'), $rutasModalesCCTV) && !$this->request->getCurrentRequest()->isXmlHttpRequest()) {
  41.                 $urlDef $this->router->generate($this->request->getCurrentRequest()->get('_route'), $this->request->getCurrentRequest()->get('_route_params'));
  42.                 $sesion->set('loadModalRequest'$urlDef);
  43.                 $response = new RedirectResponse($this->router->generate('app_seg_solicitud_cctv_index'));
  44.                 header('Location: ' $response->getTargetUrl());
  45.                 exit;
  46.             }
  47.             // Rutas de autorización de visitantes: si se accede directamente (desde correo),
  48.             // se redirige al index de solicitud visitantes con ?id= para que el template
  49.             // renderice el modal de autorización correctamente en su página padre
  50.             $rutasModalesVisitante = ['app_seg_ingreso_visitante_autorizar_visitante'];
  51.             if (in_array($this->request->getCurrentRequest()->get('_route'), $rutasModalesVisitante) && !$this->request->getCurrentRequest()->isXmlHttpRequest()) {
  52.                 $routeParams $this->request->getCurrentRequest()->get('_route_params');
  53.                 $urlDef $this->router->generate($this->request->getCurrentRequest()->get('_route'), $routeParams);
  54.                 $sesion->set('loadModalRequest'$urlDef);
  55.                 // Redirigir al index con el id como query param: el template lo cargará
  56.                 // mediante {% if id is not null %} {{ render(path('...autorizar_visitante', {'id': id})) }}
  57.                 $response = new RedirectResponse(
  58.                     $this->router->generate('app_seg_ingreso_visitante_index', ['id' => $routeParams['id'] ?? null])
  59.                 );
  60.                 header('Location: ' $response->getTargetUrl());
  61.                 exit;
  62.             }
  63.             if (in_array($accion->getClass(), ['ajax''ajax2''ajax3']) && !$this->request->getCurrentRequest()->isXmlHttpRequest()) {
  64.                 $funcionalidad $accion->getFuncion();
  65.                 $listar null;
  66.                 $redirect 'app_homepage';
  67.                 if ($funcionalidad->getModulo()) {
  68.                     if ($funcionalidad->getModulo()->getId() == 4) {
  69.                         $response = new RedirectResponse($this->router->generate($redirect));
  70.                         header('Location: ' $response->getTargetUrl());
  71.                         exit;
  72.                     }
  73.                 }
  74.                 foreach ($funcionalidad->getSecAccion() as $acc) {
  75.                     if ($acc->getTipo() == 1) {
  76.                         $redirect $acc->getRuta();
  77.                         break;
  78.                     }
  79.                 }
  80.                 $urlDef $this->router->generate($accion->getRuta(), $this->request->getCurrentRequest()->get('_route_params'));
  81.                 $sesion->set('loadModalRequest'$urlDef);
  82.                 $response = new RedirectResponse($this->router->generate($redirect));
  83.                 header('Location: ' $response->getTargetUrl());
  84.                 exit;
  85.                 return $response;
  86.             }
  87.             else if (in_array($accion->getClass(), ['ajax''ajax2''ajax3']) && $this->request->getCurrentRequest()->isXmlHttpRequest()) {
  88.                 $sesion->set('loadModalRequest'null);
  89.             }
  90.         //        var_dump($this->request->getCurrentRequest()->get('_route'));
  91. //        var_dump($this->request->getCurrentRequest()->get('_route_params'));
  92. //        var_dump($this->request->getCurrentRequest()->get('_controller'));
  93.         /*
  94.          *     return new RedirectResponse($this->router->generate($actbodusu[0]['nombreRuta'], array(
  95.          'idSubModulo' => $actbodusu[0]['idSubModulo'],
  96.          'idFun' => $actbodusu[0]['idFun'],
  97.          'idEntidad' => $ordenTrabajo->getId(),
  98.          'idOTActBodUsu' => $actbodusu[0]['id'],)), 301);
  99.          }
  100.          *///        echo "ya estoy validando;";         //                 //        exit;        }
  101.     }
  102. }
  103. }