src/EventListener/GeneralListener.php line 43

Open in your IDE?
  1. <?php
  2. // src/EventListener/ExceptionListener.php
  3. namespace App\EventListener;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  6. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  10. class GeneralListener {
  11.     /* public function onKernelException(ExceptionEvent $event)
  12.       {
  13.       // You get the exception object from the received event
  14.       $exception = $event->getThrowable();
  15.       $message = sprintf(
  16.       'My Error says: %s with code: %s',
  17.       $exception->getMessage(),
  18.       $exception->getCode()
  19.       );
  20.       // Customize your response object to display the exception details
  21.       $response = new Response();
  22.       $response->setContent($message);
  23.       // HttpExceptionInterface is a special type of exception that
  24.       // holds status code and header details
  25.       if ($exception instanceof HttpExceptionInterface) {
  26.       $response->setStatusCode($exception->getStatusCode());
  27.       $response->headers->replace($exception->getHeaders());
  28.       } else {
  29.       $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  30.       }
  31.       // sends the modified response object to the event
  32.       $event->setResponse($response);
  33.       }
  34.      */
  35.     public function onKernelController(ControllerEvent $event) {
  36.         $routeName $event->getRequest()->get('_route');
  37.         $checkWs explode("_"$routeName);
  38.         if ($checkWs[0] == 'ws' || $routeName == 'app_login' || $checkWs == 'homepage') {
  39.             
  40.         } else {
  41.             if ($routeName) {
  42.                 $lastUsername $event->getRequest()->getSession()->get(Security::LAST_USERNAME);
  43.                 $perms $event->getRequest()->getSession()->get($lastUsername "_perms");
  44.                 $routes = [];
  45.                 if ($perms) {
  46.                     $listArray = ["_index""_new""_edit""_delete""_show"];
  47.                     $cleanRoute $routeName;
  48.                     foreach ($listArray as $list) {
  49.                         $cleanRoute str_replace($list""$cleanRoute);
  50.                     }
  51.                     $hasAccess 0;
  52.                     foreach ($perms as $perm) {
  53.                         $cleanCurrentRoute $perm['url_access'];
  54.                         foreach ($listArray as $list) {
  55.                             $cleanCurrentRoute str_replace($list""$cleanCurrentRoute);
  56.                         }
  57.                         if ($cleanCurrentRoute == $cleanRoute) {
  58.                             $routes[] = $cleanRoute "_index";
  59.                             $routes[] = $cleanRoute[0] . "_selector_1";
  60.                             $routes[] = $cleanRoute[0] . "_selector_2";
  61.                             $routes[] = $cleanRoute[0] . "_selector_3";
  62.                             $routes[] = $cleanRoute[0] . "_selector_4";
  63.                             $routes[] = $cleanRoute[0] . "_selector_5";
  64.                             if ($perm['write_permission'] == 1) {
  65.                                 $routes[] = $cleanRoute "_new";
  66.                             };
  67.                             if ($perm['edit_permission'] == 1) {
  68.                                 $routes[] = $cleanRoute "_edit";
  69.                             };
  70.                             if ($perm['delete_permission'] == 1) {
  71.                                 $routes[] = $cleanRoute "_delete";
  72.                             };
  73.                             if ($perm['read_permission'] == 1) {
  74.                                 $routes[] = $cleanRoute "_show";
  75.                             };
  76.                         }
  77.                     }
  78.                     if (in_array($routeName$routes)) {
  79.                         $hasAccess++;
  80.                     }
  81. //                    if ($hasAccess == 0) {
  82. //                        throw new AccessDeniedHttpException('Se requiere autorización para ingresar a esta sección');
  83. //                    }
  84.                 }
  85.             }
  86.         }
  87.     }
  88. }