vendor/api-platform/core/src/Security/EventListener/DenyAccessListener.php line 62

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.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. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Security\EventListener;
  12. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  13. use ApiPlatform\Core\Security\ExpressionLanguage;
  14. use ApiPlatform\Core\Security\ResourceAccessChecker;
  15. use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
  16. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  22. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  23. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  24. /**
  25.  * Denies access to the current resource if the logged user doesn't have sufficient permissions.
  26.  *
  27.  * @author Kévin Dunglas <dunglas@gmail.com>
  28.  */
  29. final class DenyAccessListener
  30. {
  31.     private $resourceMetadataFactory;
  32.     /**
  33.      * @var ResourceAccessCheckerInterface
  34.      */
  35.     private $resourceAccessChecker;
  36.     public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory/*ResourceAccessCheckerInterface*/ $resourceAccessCheckerOrExpressionLanguage nullAuthenticationTrustResolverInterface $authenticationTrustResolver nullRoleHierarchyInterface $roleHierarchy nullTokenStorageInterface $tokenStorage nullAuthorizationCheckerInterface $authorizationChecker null)
  37.     {
  38.         $this->resourceMetadataFactory $resourceMetadataFactory;
  39.         if ($resourceAccessCheckerOrExpressionLanguage instanceof ResourceAccessCheckerInterface) {
  40.             $this->resourceAccessChecker $resourceAccessCheckerOrExpressionLanguage;
  41.             return;
  42.         }
  43.         $this->resourceAccessChecker = new ResourceAccessChecker($resourceAccessCheckerOrExpressionLanguage$authenticationTrustResolver$roleHierarchy$tokenStorage$authorizationChecker);
  44.         @trigger_error(sprintf('Passing an instance of "%s" or null as second argument of "%s" is deprecated since API Platform 2.2 and will not be possible anymore in API Platform 3. Pass an instance of "%s" and no extra argument instead.'ExpressionLanguage::class, self::class, ResourceAccessCheckerInterface::class), \E_USER_DEPRECATED);
  45.     }
  46.     public function onKernelRequest(RequestEvent $event): void
  47.     {
  48.         @trigger_error(sprintf('Method "%1$s::onKernelRequest" is deprecated since API Platform 2.4 and will not be available anymore in API Platform 3. Prefer calling "%1$s::onSecurity" instead.'self::class), \E_USER_DEPRECATED);
  49.         $this->onSecurityPostDenormalize($event);
  50.     }
  51.     public function onSecurity(RequestEvent $event): void
  52.     {
  53.         $this->checkSecurity($event->getRequest(), 'security'false);
  54.     }
  55.     public function onSecurityPostDenormalize(RequestEvent $event): void
  56.     {
  57.         $request $event->getRequest();
  58.         $this->checkSecurity($request'security_post_denormalize'true, [
  59.             'previous_object' => $request->attributes->get('previous_data'),
  60.         ]);
  61.     }
  62.     /**
  63.      * @throws AccessDeniedException
  64.      */
  65.     private function checkSecurity(Request $requeststring $attributebool $backwardCompatibility, array $extraVariables = []): void
  66.     {
  67.         if (!$attributes RequestAttributesExtractor::extractAttributes($request)) {
  68.             return;
  69.         }
  70.         $resourceMetadata $this->resourceMetadataFactory->create($attributes['resource_class']);
  71.         $isGranted $resourceMetadata->getOperationAttribute($attributes$attributenulltrue);
  72.         if ($backwardCompatibility && null === $isGranted) {
  73.             // Backward compatibility
  74.             $isGranted $resourceMetadata->getOperationAttribute($attributes'access_control'nulltrue);
  75.             if (null !== $isGranted) {
  76.                 @trigger_error('Using "access_control" attribute is deprecated since API Platform 2.4 and will not be possible anymore in API Platform 3. Use "security" attribute instead.', \E_USER_DEPRECATED);
  77.             }
  78.         }
  79.         if (null === $isGranted) {
  80.             return;
  81.         }
  82.         $extraVariables += $request->attributes->all();
  83.         $extraVariables['object'] = $request->attributes->get('data');
  84.         $extraVariables['request'] = $request;
  85.         if (!$this->resourceAccessChecker->isGranted($attributes['resource_class'], $isGranted$extraVariables)) {
  86.             throw new AccessDeniedException($resourceMetadata->getOperationAttribute($attributes$attribute.'_message''Access Denied.'true));
  87.         }
  88.     }
  89. }