|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Symfony\Security\State; |
| 15 | + |
| 16 | +use ApiPlatform\Exception\InvalidIdentifierException; |
| 17 | +use ApiPlatform\Exception\InvalidUriVariableException; |
| 18 | +use ApiPlatform\Metadata\HttpOperation; |
| 19 | +use ApiPlatform\Metadata\Link; |
| 20 | +use ApiPlatform\Metadata\Operation; |
| 21 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 22 | +use ApiPlatform\State\Exception\ProviderNotFoundException; |
| 23 | +use ApiPlatform\State\ProviderInterface; |
| 24 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 25 | + |
| 26 | +/** |
| 27 | + * Checks if the linked resources have security attributes and prepares them for access checking. |
| 28 | + * |
| 29 | + * @experimental |
| 30 | + */ |
| 31 | +final class LinkedReadProvider implements ProviderInterface |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + private readonly ProviderInterface $decorated, |
| 35 | + private readonly ProviderInterface $locator, |
| 36 | + private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null |
| 41 | + { |
| 42 | + $data = $this->decorated->provide($operation, $uriVariables, $context); |
| 43 | + |
| 44 | + if (!$operation instanceof HttpOperation) { |
| 45 | + return $data; |
| 46 | + } |
| 47 | + |
| 48 | + $request = ($context['request'] ?? null); |
| 49 | + |
| 50 | + if ($operation->getUriVariables()) { |
| 51 | + foreach ($operation->getUriVariables() as $key => $uriVariable) { |
| 52 | + if (!$uriVariable instanceof Link || !$uriVariable->getSecurity()) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + $relationClass = $uriVariable->getFromClass() ?? $uriVariable->getToClass(); |
| 57 | + |
| 58 | + if (!$relationClass) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $parentOperation = $this->resourceMetadataCollectionFactory |
| 63 | + ->create($relationClass) |
| 64 | + ->getOperation($operation->getExtraProperties()['parent_uri_template'] ?? null); |
| 65 | + try { |
| 66 | + $relation = $this->locator->provide($parentOperation, [$uriVariable->getIdentifiers()[0] => $request?->attributes->all()[$key]], $context); |
| 67 | + } catch (ProviderNotFoundException) { |
| 68 | + $relation = null; |
| 69 | + } |
| 70 | + |
| 71 | + if (!$relation) { |
| 72 | + throw new NotFoundHttpException('Relation for link security not found.'); |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + $securityObjectName = $uriVariable->getSecurityObjectName(); |
| 77 | + |
| 78 | + if (!$securityObjectName) { |
| 79 | + $securityObjectName = $uriVariable->getToProperty() ?? $uriVariable->getFromProperty(); |
| 80 | + } |
| 81 | + |
| 82 | + $request?->attributes->set($securityObjectName, $relation); |
| 83 | + } catch (InvalidIdentifierException|InvalidUriVariableException $e) { |
| 84 | + throw new NotFoundHttpException('Invalid identifier value or configuration.', $e); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return $data; |
| 90 | + } |
| 91 | +} |
0 commit comments