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

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\HttpKernel\Event\GetResponseEvent;
  18. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  21. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  22. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  23. /**
  24.  * Denies access to the current resource if the logged user doesn't have sufficient permissions.
  25.  *
  26.  * @author Kévin Dunglas <dunglas@gmail.com>
  27.  */
  28. final class DenyAccessListener
  29. {
  30.     private $resourceMetadataFactory;
  31.     /**
  32.      * @var ResourceAccessCheckerInterface
  33.      */
  34.     private $resourceAccessChecker;
  35.     public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory/*ResourceAccessCheckerInterface*/ $resourceAccessCheckerOrExpressionLanguage nullAuthenticationTrustResolverInterface $authenticationTrustResolver nullRoleHierarchyInterface $roleHierarchy nullTokenStorageInterface $tokenStorage nullAuthorizationCheckerInterface $authorizationChecker null)
  36.     {
  37.         $this->resourceMetadataFactory $resourceMetadataFactory;
  38.         if ($resourceAccessCheckerOrExpressionLanguage instanceof ResourceAccessCheckerInterface) {
  39.             $this->resourceAccessChecker $resourceAccessCheckerOrExpressionLanguage;
  40.             return;
  41.         }
  42.         $this->resourceAccessChecker = new ResourceAccessChecker($resourceAccessCheckerOrExpressionLanguage$authenticationTrustResolver$roleHierarchy$tokenStorage$authorizationChecker);
  43.         @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);
  44.     }
  45.     /**
  46.      * @throws AccessDeniedException
  47.      */
  48.     public function onKernelRequest(GetResponseEvent $event): void
  49.     {
  50.         $request $event->getRequest();
  51.         if (!$attributes RequestAttributesExtractor::extractAttributes($request)) {
  52.             return;
  53.         }
  54.         $resourceMetadata $this->resourceMetadataFactory->create($attributes['resource_class']);
  55.         $isGranted $resourceMetadata->getOperationAttribute($attributes'access_control'nulltrue);
  56.         if (null === $isGranted) {
  57.             return;
  58.         }
  59.         $extraVariables $request->attributes->all();
  60.         $extraVariables['object'] = $request->attributes->get('data');
  61.         $extraVariables['previous_object'] = $request->attributes->get('previous_data');
  62.         $extraVariables['request'] = $request;
  63.         if (!$this->resourceAccessChecker->isGranted($attributes['resource_class'], $isGranted$extraVariables)) {
  64.             throw new AccessDeniedException($resourceMetadata->getOperationAttribute($attributes'access_control_message''Access Denied.'true));
  65.         }
  66.     }
  67. }