src/Bidcoz/Bundle/FrontendBundle/EventListener/AccessDeniedExceptionListener.php line 40

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\FrontendBundle\EventListener;
  3. use RS\DiExtraBundle\Annotation as DI;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  11. /**
  12.  * @DI\Service
  13.  */
  14. class AccessDeniedExceptionListener
  15. {
  16.     private AuthorizationCheckerInterface $authorizationChecker;
  17.     private RouterInterface $router;
  18.     private RequestStack $requestStack;
  19.     /**
  20.      * @DI\InjectParams({
  21.      *     "authorizationChecker" = @DI\Inject("security.authorization_checker"),
  22.      *     "router" = @DI\Inject("router"),
  23.      *     "requestStack" = @DI\Inject("request_stack")
  24.      * })
  25.      */
  26.     public function __construct(AuthorizationCheckerInterface $authorizationCheckerRouterInterface $routerRequestStack $requestStack)
  27.     {
  28.         $this->authorizationChecker $authorizationChecker;
  29.         $this->router $router;
  30.         $this->requestStack $requestStack;
  31.     }
  32.     /**
  33.      * @DI\Observe(KernelEvents::EXCEPTION)
  34.      */
  35.     public function onKernelException(ExceptionEvent $event): void
  36.     {
  37.         // interested only in AccessDeniedHttpException events
  38.         $exception $event->getThrowable();
  39.         if (!$exception instanceof AccessDeniedHttpException) {
  40.             return;
  41.         }
  42.         // exception must have WITH_CC check
  43.         if (false === strpos($exception->getMessage(), 'WITH_CC')) {
  44.             return;
  45.         }
  46.         $request $event->getRequest();
  47.         if (!$request->attributes->has('campaign')) {
  48.             return;
  49.         }
  50.         // if we have a campaign then we also have an organization
  51.         $campaign $request->attributes->get('campaign');
  52.         $organization $request->attributes->get('organization');
  53.         if (!$this->authorizationChecker->isGranted('VIEW'$organization) ||
  54.             !$this->authorizationChecker->isGranted('FRONT_END'$campaign)
  55.         ) {
  56.             return;
  57.         }
  58.         if ($this->authorizationChecker->isGranted('WITH_CC'$campaign)) {
  59.             return;
  60.         }
  61.         $this->requestStack->getSession()->getFlashBag()->add('info''You must add Credit Card to your profile to access this.');
  62.         $url $this->router->generate('fos_user_profile_edit');
  63.         $response = new RedirectResponse($url);
  64.         // Send the modified response object to the event
  65.         $event->setResponse($response);
  66.     }
  67. }