vendor/friendsofsymfony/rest-bundle/EventListener/AccessDeniedListener.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\RestBundle\EventListener;
  11. @trigger_error(sprintf('The %s\AccessDeniedListener class is deprecated since FOSRestBundle 2.8.'__NAMESPACE__), E_USER_DEPRECATED);
  12. use FOS\RestBundle\FOSRestBundle;
  13. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  14. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  15. use Symfony\Component\HttpKernel\Exception\HttpException;
  16. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  19. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. /**
  22.  * This listener handles ensures that for specific formats AccessDeniedExceptions
  23.  * will return a 403 regardless of how the firewall is configured.
  24.  *
  25.  * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  26.  *
  27.  * @internal
  28.  */
  29. class AccessDeniedListener implements EventSubscriberInterface
  30. {
  31.     private $formats;
  32.     private $challenge;
  33.     /**
  34.      * @param array $formats An array with keys corresponding to request formats or content types
  35.      *                       that must be processed by this listener
  36.      */
  37.     public function __construct(array $formats, ?string $challenge)
  38.     {
  39.         $this->formats $formats;
  40.         $this->challenge $challenge;
  41.     }
  42.     /**
  43.      * @param ExceptionEvent $event
  44.      */
  45.     public function onKernelException($event)
  46.     {
  47.         static $handling;
  48.         if (true === $handling) {
  49.             return false;
  50.         }
  51.         $request $event->getRequest();
  52.         if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTEtrue)) {
  53.             return false;
  54.         }
  55.         if (empty($this->formats[$request->getRequestFormat()]) && empty($this->formats[$request->getContentType()])) {
  56.             return false;
  57.         }
  58.         $handling true;
  59.         if (method_exists($event'getThrowable')) {
  60.             $exception $event->getThrowable();
  61.         } else {
  62.             $exception $event->getException();
  63.         }
  64.         if ($exception instanceof AccessDeniedException) {
  65.             $exception = new AccessDeniedHttpException('You do not have the necessary permissions');
  66.         } elseif ($exception instanceof AuthenticationException) {
  67.             if ($this->challenge) {
  68.                 $exception = new UnauthorizedHttpException($this->challenge'You are not authenticated');
  69.             } else {
  70.                 $exception = new HttpException(401'You are not authenticated');
  71.             }
  72.         }
  73.         if (method_exists($event'setThrowable')) {
  74.             $event->setThrowable($exception);
  75.         } else {
  76.             $event->setException($exception);
  77.         }
  78.         $handling false;
  79.     }
  80.     public static function getSubscribedEvents(): array
  81.     {
  82.         return [
  83.             KernelEvents::EXCEPTION => ['onKernelException'5],
  84.         ];
  85.     }
  86. }