|
| 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\State\ParameterProvider; |
| 15 | + |
| 16 | +use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation; |
| 17 | +use ApiPlatform\Metadata\HttpOperation; |
| 18 | +use ApiPlatform\Metadata\Link; |
| 19 | +use ApiPlatform\Metadata\Operation; |
| 20 | +use ApiPlatform\Metadata\Parameter; |
| 21 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 22 | +use ApiPlatform\State\Exception\ProviderNotFoundException; |
| 23 | +use ApiPlatform\State\ParameterProviderInterface; |
| 24 | +use ApiPlatform\State\ProviderInterface; |
| 25 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 26 | + |
| 27 | +/** |
| 28 | + * Checks if the linked resources have security attributes and prepares them for access checking. |
| 29 | + * |
| 30 | + * @experimental |
| 31 | + */ |
| 32 | +final class ReadLinkParameterProvider implements ParameterProviderInterface |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @param ProviderInterface<object> $locator |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + private readonly ProviderInterface $locator, |
| 39 | + private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, |
| 40 | + ) { |
| 41 | + } |
| 42 | + |
| 43 | + public function provide(Parameter $parameter, array $parameters = [], array $context = []): ?Operation |
| 44 | + { |
| 45 | + $operation = $context['operation']; |
| 46 | + $extraProperties = $parameter->getExtraProperties(); |
| 47 | + |
| 48 | + if ($parameter instanceof Link) { |
| 49 | + $linkClass = $parameter->getFromClass() ?? $parameter->getToClass(); |
| 50 | + $securityObjectName = $parameter->getSecurityObjectName() ?? $parameter->getToProperty() ?? $parameter->getFromProperty(); |
| 51 | + } |
| 52 | + |
| 53 | + $securityObjectName ??= $parameter->getKey(); |
| 54 | + |
| 55 | + $linkClass ??= $extraProperties['resource_class'] ?? $operation->getClass(); |
| 56 | + |
| 57 | + if (!$linkClass) { |
| 58 | + return $operation; |
| 59 | + } |
| 60 | + |
| 61 | + $linkOperation = $this->resourceMetadataCollectionFactory |
| 62 | + ->create($linkClass) |
| 63 | + ->getOperation($operation->getExtraProperties()['parent_uri_template'] ?? $extraProperties['uri_template'] ?? null); |
| 64 | + |
| 65 | + $value = $parameter->getValue(); |
| 66 | + |
| 67 | + if (\is_array($value) && array_is_list($value)) { |
| 68 | + $relation = []; |
| 69 | + |
| 70 | + foreach ($value as $v) { |
| 71 | + try { |
| 72 | + $r = $relation[] = $this->locator->provide($linkOperation, $this->getUriVariables($v, $parameter, $linkOperation), $context); |
| 73 | + } catch (ProviderNotFoundException) { |
| 74 | + } |
| 75 | + } |
| 76 | + } else { |
| 77 | + try { |
| 78 | + $relation = $this->locator->provide($linkOperation, $this->getUriVariables($value, $parameter, $linkOperation), $context); |
| 79 | + } catch (ProviderNotFoundException) { |
| 80 | + $relation = null; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + $parameter->setValue($relation); |
| 85 | + |
| 86 | + if (null === $relation && true === ($extraProperties['throw_not_found'] ?? true)) { |
| 87 | + throw new NotFoundHttpException('Relation for link security not found.'); |
| 88 | + } |
| 89 | + |
| 90 | + $context['request']?->attributes->set($securityObjectName, $relation); |
| 91 | + |
| 92 | + return $operation; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @return array<string, string> |
| 97 | + */ |
| 98 | + private function getUriVariables(mixed $value, Parameter $parameter, Operation $operation): array |
| 99 | + { |
| 100 | + $extraProperties = $parameter->getExtraProperties(); |
| 101 | + |
| 102 | + if ($operation instanceof HttpOperation) { |
| 103 | + $links = $operation->getUriVariables(); |
| 104 | + } elseif ($operation instanceof GraphQlOperation) { |
| 105 | + $links = $operation->getLinks(); |
| 106 | + } else { |
| 107 | + $links = []; |
| 108 | + } |
| 109 | + |
| 110 | + if (!\is_array($value)) { |
| 111 | + return [1 === \count($links) ? current($links)->getKey() : ($extraProperties['uri_variable'] ?? $parameter->getKey()) => $value]; |
| 112 | + } |
| 113 | + |
| 114 | + return $value; |
| 115 | + } |
| 116 | +} |
0 commit comments