ERP/SecurityBundle/EventListener/DenyAccessListener.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace ERP\SecurityBundle\EventListener;
  4. use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
  5. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  6. use ApiPlatform\Core\Security\ExpressionLanguage;
  7. use ApiPlatform\Core\Security\ResourceAccessChecker;
  8. use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
  9. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  10. use ERP\SecurityBundle\Services\RoleHierarchyService;
  11. use Symfony\Component\HttpKernel\Event\RequestEvent;
  12. use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  16. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  17. /**
  18.  * Denies access to the current resource if the logged user doesn't have sufficient permissions.
  19.  */
  20. final class DenyAccessListener
  21. {
  22.     private $resourceMetadataFactory;
  23.     private $resourceAccessChecker;
  24.     /**
  25.      * @var RoleHierarchyService
  26.      */
  27.     private $roleHierarchyService;
  28.     public function __construct(
  29.         ResourceMetadataFactoryInterface $resourceMetadataFactory,
  30.         RoleHierarchyService $roleHierarchyService,
  31.         /* ResourceAccessCheckerInterface */ $resourceAccessCheckerOrExpressionLanguage null,
  32.         AuthenticationTrustResolverInterface $authenticationTrustResolver null,
  33.         RoleHierarchyInterface $roleHierarchy null,
  34.         TokenStorageInterface $tokenStorage null,
  35.         AuthorizationCheckerInterface $authorizationChecker null)
  36.     {
  37.         $this->resourceMetadataFactory $resourceMetadataFactory;
  38.         $this->roleHierarchyService $roleHierarchyService;
  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.     /**
  47.      * Sets the applicable format to the HttpFoundation Request.
  48.      *
  49.      * @throws ResourceClassNotFoundException
  50.      */
  51.     public function onKernelRequest(RequestEvent $event): void
  52.     {
  53.         $request $event->getRequest();
  54.         if (!$attributes RequestAttributesExtractor::extractAttributes($request)) {
  55.             return;
  56.         }
  57.         $resourceMetadata $this->resourceMetadataFactory->create($attributes['resource_class']);
  58.         $metaAccess $resourceMetadata->getOperationAttribute($attributes'access_control'nulltrue);
  59.         if ('/api/' !== substr($request->getPathInfo(), 05)) {
  60.             // return;
  61.         }
  62.         if (null === $metaAccess) {
  63.             try {
  64.                 $attribute $this->roleHierarchyService->getOperationAccessAttribute($attributes['resource_class']);
  65.                 $isGranted "is_granted('{$attribute}')";
  66.             } catch (\InvalidArgumentException $exception) {
  67.                 $isGranted null;
  68.             }
  69.             if (null === $isGranted) {
  70.                 return;
  71.             }
  72.             $extraVariables $request->attributes->all();
  73.             $extraVariables['object'] = $request->attributes->get('data');
  74.             $extraVariables['request'] = $request;
  75.             if (isset($extraVariables['_route_params']) && isset($extraVariables['_route_params']['_api_extra_access_control'])) {
  76.                 switch ($extraVariables['_route_params']['_api_extra_access_control']) {
  77.                     case 'IS_AUTHENTICATED_ANONYMOUSLY':
  78.                         return;
  79.                 }
  80.             }
  81.             $access $this->resourceAccessChecker->isGranted($attributes['resource_class'], $isGranted$extraVariables);
  82.             if (!$access) {
  83.                 throw new AccessDeniedException((string) $resourceMetadata->getOperationAttribute($attributes'access_control_message''Access Denied.'true));
  84.             }
  85.         }
  86.     }
  87. }