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