src/EventListener/GeneralListener.php line 59

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. {
  12.    /* public function onKernelException(ExceptionEvent $event)
  13.     {
  14.         // You get the exception object from the received event
  15.         $exception = $event->getThrowable();
  16.         $message = sprintf(
  17.             'My Error says: %s with code: %s',
  18.             $exception->getMessage(),
  19.             $exception->getCode()
  20.         );
  21.         // Customize your response object to display the exception details
  22.         $response = new Response();
  23.         $response->setContent($message);
  24.         // HttpExceptionInterface is a special type of exception that
  25.         // holds status code and header details
  26.         if ($exception instanceof HttpExceptionInterface) {
  27.             $response->setStatusCode($exception->getStatusCode());
  28.             $response->headers->replace($exception->getHeaders());
  29.         } else {
  30.             $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  31.         }
  32.         // sends the modified response object to the event
  33.         $event->setResponse($response);
  34.     }
  35.     */
  36.     
  37.      public function onKernelController(ControllerEvent $event)
  38.      {
  39.            
  40.            $routeName $event->getRequest()->get('_route');                                        
  41.            $checkWs explode("_",$routeName);
  42.            if(str_replace("/","",$checkWs[0]) == 'ws' || $routeName == 'app_login' || $checkWs == 'homepage' || $routeName == "_wdt" || $routeName == "_profiler")
  43.            {
  44.                
  45.            } else {
  46.                if($routeName)
  47.                {
  48.                    
  49.                    $lastUsername $event->getRequest()->getSession()->get(Security::LAST_USERNAME);
  50.                    $perms        $event->getRequest()->getSession()->get($lastUsername."_perms");    
  51.     
  52.                    $routes       = [];
  53.                    if($perms)
  54.                    {
  55.                        
  56.                          $listArray  = ["_index","_new","_edit","_delete","_show"];
  57.                          $cleanRoute $routeName
  58.                          foreach($listArray as $list)
  59.                          {
  60.                              $cleanRoute str_replace($list""$cleanRoute);
  61.                          }
  62.                                                
  63.                        $hasAccess 0;
  64.                        foreach($perms as $perm)
  65.                        {
  66.                            
  67.                             $cleanCurrentRoute $perm['url_access'];                             
  68.                             foreach($listArray as $list)
  69.                             {
  70.                                  $cleanCurrentRoute str_replace($list""$cleanCurrentRoute);
  71.                             }                           
  72.                           if($cleanCurrentRoute == $cleanRoute)
  73.                           {         
  74.                                                                               
  75.                                 $routes[] = $cleanRoute."_index";                       
  76.                                
  77.                               if($perm['write_permission'] == 1)
  78.                               {
  79.                                   $routes[] = $cleanRoute."_new";
  80.                               };      
  81.                               if($perm['edit_permission'] == 1)
  82.                               {
  83.                                   $routes[] = $cleanRoute."_edit";
  84.                               };                            
  85.                               if($perm['delete_permission'] == 1)
  86.                               {
  87.                                   $routes[] = $cleanRoute."_delete";
  88.                               };                            
  89.                               if($perm['read_permission'] == 1)
  90.                               {
  91.                                   $routes[] = $cleanRoute."_show";
  92.                               };    
  93.                                 
  94.                           }                                            
  95.                        }        
  96.                                                 
  97.                        if(in_array($routeName$routes))
  98.                        {
  99.                            $hasAccess++;
  100.                        }                                                             
  101.                        
  102.                        if($hasAccess == 0)
  103.                        {
  104.                            throw new AccessDeniedHttpException('Se requiere autorización para ingresar a esta sección');
  105.                        }                   
  106.                    }
  107.                    
  108.                }
  109.                
  110.            }
  111.            
  112.      }    
  113. }