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