src/EventListener/Authenticator.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\AbstractClass\Controller\AbstractController;
  4. use App\Component\Authenticator\Authenticator as UserAuthenticator;
  5. use App\Component\Authenticator\Firewall;
  6. use App\Component\Response\Response;
  7. use Exception;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. class Authenticator extends AbstractController
  10. {
  11.     /**
  12.      * @var UserAuthenticator
  13.      */
  14.     private $authenticator;
  15.     /**
  16.      * @var Response
  17.      */
  18.     private $response;
  19.     function __construct(UserAuthenticator $authenticatorResponse $response)
  20.     {
  21.         $this->authenticator $authenticator;
  22.         $this->response $response;
  23.     }
  24.     /**
  25.      * Authentication checker before controller execution.
  26.      *
  27.      * @param ControllerEvent $event
  28.      *
  29.      * @throws Exception
  30.      */
  31.     public function onKernelController(ControllerEvent $event)
  32.     {
  33.         if(!$event->isMasterRequest()) {
  34.             return;
  35.         }
  36.         $controller $event->getRequest()->get('_controller');
  37.         if($controller === 'error_controller') {
  38.             return;
  39.         }
  40.         $controllerParts explode(strpos($controller'::') !== false '::' ':'$controller2);
  41.         $controllerClass $controllerParts[0];
  42.         $controllerAction $controllerParts[1];
  43.         $firewall = new Firewall($controllerClass);
  44.         $protectedActions $firewall->getProtectedActions();
  45.         if ($firewall->needsAuthorization() && !$this->authenticator->isAuthenticated()) {
  46.             $event->setController(function() {
  47.                 return $this->response->unauthorized();
  48.             });
  49.             return;
  50.         }
  51.         if (count($protectedActions) && in_array($controllerAction$protectedActions) && !$this->authenticator->hasPermission()) {
  52.             $event->setController(function() {
  53.                 return $this->response->noPermissions();
  54.             });
  55.         }
  56.     }
  57. }