src/EventListener/GeneralListener.php line 60

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')
  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"];
  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.                              $routes[] = $cleanRoute."_custom_5";
  81.                              $routes[] = $cleanRoute."_custom_6";
  82.                              $routes[] = $cleanRoute."_custom_7";
  83.                              $routes[] = $cleanRoute."_custom_8";
  84.                              
  85.                                
  86.                               if($perm['write_permission'] == 1)
  87.                               {
  88.                                   $routes[] = $cleanRoute."_new";
  89.                               };      
  90.                               if($perm['edit_permission'] == 1)
  91.                               {
  92.                                   $routes[] = $cleanRoute."_edit";
  93.                               };                            
  94.                               if($perm['delete_permission'] == 1)
  95.                               {
  96.                                   $routes[] = $cleanRoute."_delete";
  97.                               };                            
  98.                               if($perm['read_permission'] == 1)
  99.                               {
  100.                                   $routes[] = $cleanRoute."_show";
  101.                               };    
  102.                                 
  103.                           }                                            
  104.                        }        
  105.                                           
  106. //                       if(in_array($routeName, $routes))
  107. //                       {
  108. //                           $hasAccess++;
  109. //                       }                                                             
  110.                        
  111. //                       if($hasAccess == 0)
  112. //                       {
  113. //                           throw new AccessDeniedHttpException('Se requiere autorización para ingresar a esta sección');
  114. //                       }                   
  115.                    }
  116.                    
  117.                }
  118.                
  119.            }
  120.            
  121.      }    
  122. }