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

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. use FOS\RestBundle\FOSRestBundle;
  12. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  13. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  14. use Symfony\Component\HttpKernel\Exception\HttpException;
  15. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  18. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. /**
  21.  * This listener handles ensures that for specific formats AccessDeniedExceptions
  22.  * will return a 403 regardless of how the firewall is configured.
  23.  *
  24.  * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  25.  *
  26.  * @internal
  27.  */
  28. class AccessDeniedListener implements EventSubscriberInterface
  29. {
  30.     private $formats;
  31.     private $challenge;
  32.     /**
  33.      * Constructor.
  34.      *
  35.      * @param array  $formats   An array with keys corresponding to request formats or content types
  36.      *                          that must be processed by this listener
  37.      * @param string $challenge
  38.      */
  39.     public function __construct($formats$challenge)
  40.     {
  41.         $this->formats $formats;
  42.         $this->challenge $challenge;
  43.     }
  44.     public function onKernelException(GetResponseForExceptionEvent $event)
  45.     {
  46.         static $handling;
  47.         if (true === $handling) {
  48.             return false;
  49.         }
  50.         $request $event->getRequest();
  51.         if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTEtrue)) {
  52.             return false;
  53.         }
  54.         if (empty($this->formats[$request->getRequestFormat()]) && empty($this->formats[$request->getContentType()])) {
  55.             return false;
  56.         }
  57.         $handling true;
  58.         $exception $event->getException();
  59.         if ($exception instanceof AccessDeniedException) {
  60.             $exception = new AccessDeniedHttpException('You do not have the necessary permissions'$exception);
  61.             $event->setException($exception);
  62.         } elseif ($exception instanceof AuthenticationException) {
  63.             if ($this->challenge) {
  64.                 $exception = new UnauthorizedHttpException($this->challenge'You are not authenticated'$exception);
  65.             } else {
  66.                 $exception = new HttpException(401'You are not authenticated'$exception);
  67.             }
  68.             $event->setException($exception);
  69.         }
  70.         $handling false;
  71.     }
  72.     public static function getSubscribedEvents()
  73.     {
  74.         return [
  75.             KernelEvents::EXCEPTION => ['onKernelException'5],
  76.         ];
  77.     }
  78. }