src/EventListener/ForceRoute/AbstractForceRouteListener.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\EventListener\ForceRoute;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. abstract class AbstractForceRouteListener implements ForceRouteListenerInterface{
  12.     const MODE_REJECT_ROUTES 'mode_reject_routes';
  13.     const MODE_ALLOW_ROUTES 'mode_allow_routes';
  14.     protected RouterInterface $router;
  15.     protected TokenStorageInterface $tokenStorage;
  16.     protected AuthorizationCheckerInterface $authorizationChecker;
  17.     protected string $environment;
  18.     protected RequestEvent $event;
  19.     public function __construct(
  20.         RouterInterface $router,
  21.         TokenStorageInterface $tokenStorage,
  22.         AuthorizationCheckerInterface $authorizationChecker,
  23.         string $environment
  24.     ){
  25.         $this->router $router;
  26.         $this->tokenStorage $tokenStorage;
  27.         $this->authorizationChecker $authorizationChecker;
  28.         $this->environment $environment;
  29.     }
  30.     public function onCheckExpired(RequestEvent $event): void {
  31.         $this->event $event;
  32.         $request $event->getRequest();
  33.         // Force the route if we are not using the test environments nor if the route is authorized
  34.         if(
  35.             $this->environment !== 'test'
  36.             && !$this->isRequestedRouteAllowed($request)
  37.             && $this->mustForceRoute($request)
  38.         ){
  39.             $response = new RedirectResponse(
  40.                 $this->router->generate(
  41.                     $this->getRouteToForce(),
  42.                     $this->getRouteParametersToForce()
  43.                 )
  44.             );
  45.             $event->setResponse($response);
  46.         }
  47.     }
  48.     /**
  49.      * Get the requested route name
  50.      * @param Request $request
  51.      * @return string
  52.      */
  53.     public function getRequestedRouteName(Request $request): string {
  54.         $pathInfo $request->getPathInfo();
  55.         try{
  56.             $route $this->router->match($pathInfo);
  57.         }catch(ResourceNotFoundException $exception){
  58.             return '';
  59.         }
  60.         return $route['_route'];
  61.     }
  62.     /**
  63.      * Checks if the requested route is the change password route
  64.      * @param Request $request
  65.      * @return bool
  66.      */
  67.     public function isRequestedRouteAllowed(Request $request): bool
  68.     {
  69.         $routeName $this->getRequestedRouteName($request);
  70.         $routeInObservedRoutes in_array($routeName$this->getRoutes(), true);
  71.         return $routeName === $this->getRouteToForce()
  72.             || $this->getMode() === self::MODE_REJECT_ROUTES && !$routeInObservedRoutes
  73.             || $this->getMode() === self::MODE_ALLOW_ROUTES && $routeInObservedRoutes
  74.         ;
  75.     }
  76.     /**
  77.      * Returns the current user
  78.      * @return UserInterface
  79.      */
  80.     public function getCurrentUser(): UserInterface {
  81.         return $this->tokenStorage->getToken()->getUser();
  82.     }
  83.     /**
  84.      * Checks if the requester is a user that is fully authenticated
  85.      * @return bool
  86.      */
  87.     public function isRequesterAnAuthenticatedUser(): bool {
  88.         return $this->tokenStorage->getToken()
  89.             && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY');
  90.     }
  91.     /**
  92.      * @param $role
  93.      * @return bool
  94.      */
  95.     public function isGranted($role): bool {
  96.         return $this->authorizationChecker->isGranted($role);
  97.     }
  98.     public function getMode(): string
  99.     {
  100.         return self::MODE_ALLOW_ROUTES;
  101.     }
  102.     /**
  103.      * @return array
  104.      */
  105.     public function getRouteParametersToForce(): array
  106.     {
  107.         return [];
  108.     }