src/EventListener/GeneralListener.php line 44

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.            
  43.           
  44.            if($checkWs[0] == 'ws' || $routeName == 'app_login' || $routeName == '_wdt' || $routeName 'switch_language')
  45.            {
  46.                
  47.            } else {
  48.                if($routeName)
  49.                {
  50.                    
  51.                    $lastUsername $event->getRequest()->getSession()->get(Security::LAST_USERNAME);
  52.                    $perms        $event->getRequest()->getSession()->get($lastUsername."_perms");    
  53.     
  54.                    $routes       = [];
  55.                    if($perms)
  56.                    {
  57.     
  58.                       $listArray = ["_index""_new""_edit""_delete""_show""_custom_1","_custom_2","_custom_3","_custom_4","_custom_5","_custom_6","_custom_7","_custom_8","_custom_9","_custom_10","_custom_11","_custom_12"];
  59.                       $cleanRoute $routeName;
  60.                       foreach ($listArray as $list) {
  61.                          $cleanRoute str_replace($list""$cleanRoute);
  62.                       }    
  63.                                                                              
  64.                        $hasAccess 0;
  65.                        foreach($perms as $perm)
  66.                        {
  67.                            
  68.                          $cleanCurrentRoute $perm['url_access'];
  69.                          foreach ($listArray as $list) {
  70.                             $cleanCurrentRoute str_replace($list""$cleanCurrentRoute);
  71.                          }
  72.                          if ($cleanCurrentRoute == $cleanRoute)                          
  73.                          { 
  74.         
  75.                                 $routes[] = $cleanRoute."_index";                       
  76.                              $routes[] = $cleanRoute."_custom_1";                       
  77.                              $routes[] = $cleanRoute."_custom_2";                       
  78.                              $routes[] = $cleanRoute."_custom_3";                       
  79.                              $routes[] = $cleanRoute."_custom_4";                       
  80.                                
  81.                               if($perm['write_permission'] == 1)
  82.                               {
  83.                                   $routes[] = $cleanRoute."_new";
  84.                               };      
  85.                               if($perm['edit_permission'] == 1)
  86.                               {
  87.                                   $routes[] = $cleanRoute."_edit";
  88.                               };                            
  89.                               if($perm['delete_permission'] == 1)
  90.                               {
  91.                                   $routes[] = $cleanRoute."_delete";
  92.                               };                            
  93.                               if($perm['read_permission'] == 1)
  94.                               {
  95.                                   $routes[] = $cleanRoute."_show";
  96.                               };    
  97.                                 
  98.                           }                                            
  99.                        }        
  100.                                           
  101.                        if(in_array($routeName$routes))
  102.                        {
  103.                            $hasAccess++;
  104.                        }                                                             
  105.                        
  106.                        if($hasAccess == 0)
  107.                        {
  108.                            throw new AccessDeniedHttpException('Se requiere autorización para ingresar a esta sección');
  109.                        }                   
  110.                    }
  111.                    
  112.                }
  113.                
  114.            }
  115.            
  116.      }    
  117. }