Skip to content

Commit 42444de

Browse files
committed
refactor(state): merge parameter and link security
1 parent e6e7760 commit 42444de

24 files changed

+655
-238
lines changed

src/Elasticsearch/Extension/SortExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function applyToCollection(array $requestBody, string $resourceClass, ?Op
6262
} elseif (null !== $this->defaultDirection) {
6363
$property = 'id';
6464
if ($operation instanceof HttpOperation) {
65-
$uriVariables = $operation->getUriVariables()[0] ?? null;
65+
$uriVariables = current($operation->getUriVariables()) ?? null;
6666
$property = $uriVariables ? $uriVariables->getIdentifiers()[0] ?? 'id' : 'id';
6767
}
6868

src/Elasticsearch/Filter/AbstractSearchFilter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ protected function isIdentifier(string $resourceClass, string $property, ?Operat
139139
{
140140
$identifier = 'id';
141141
if ($operation instanceof HttpOperation) {
142-
$uriVariable = $operation->getUriVariables()[0] ?? null;
142+
$uriVariable = current($operation->getUriVariables()) ?? null;
143143

144144
if ($uriVariable) {
145145
$identifier = $uriVariable->getIdentifiers()[0] ?? 'id';

src/Elasticsearch/Serializer/DocumentNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private function populateIdentifier(array $data, string $class): array
102102

103103
$operation = $resourceMetadata->getOperation();
104104
if ($operation instanceof HttpOperation) {
105-
$uriVariable = $operation->getUriVariables()[0] ?? null;
105+
$uriVariable = current($operation->getUriVariables()) ?? null;
106106

107107
if ($uriVariable) {
108108
$identifier = $uriVariable->getIdentifiers()[0] ?? 'id';

src/Hydra/Serializer/CollectionFiltersNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
8484
$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
8585
$operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
8686

87-
$parameters = $operation->getParameters();
87+
$parameters = $operation->getParameters() ?? new Parameters();
8888
$resourceFilters = $operation->getFilters();
89-
if (!$resourceFilters && !$parameters) {
89+
if (!$resourceFilters && 0 === \count($parameters)) {
9090
return $data;
9191
}
9292

@@ -103,7 +103,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
103103

104104
$resourceClass = $this->getStateOptionsClass($operation, $resourceClass);
105105

106-
if ($currentFilters || ($parameters && \count($parameters))) {
106+
if ($currentFilters || \count($parameters) > 0) {
107107
$hydraPrefix = $this->getHydraPrefix($context + $this->defaultContext);
108108
$data[$hydraPrefix.'search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters, $parameters, $hydraPrefix);
109109
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Metadata\Exception;
15+
16+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
17+
18+
final class AccessDeniedException extends AccessDeniedHttpException implements HttpExceptionInterface
19+
{
20+
public function getStatusCode(): int
21+
{
22+
return 403;
23+
}
24+
25+
public function getHeaders(): array
26+
{
27+
return [];
28+
}
29+
}

src/Metadata/HttpOperation.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class HttpOperation extends Operation
3434
* @param array<int|string, string|string[]>|string|null $formats {@see https://api-platform.com/docs/core/content-negotiation/#configuring-formats-for-a-specific-resource-or-operation}
3535
* @param array<int|string, string|string[]>|string|null $inputFormats {@see https://api-platform.com/docs/core/content-negotiation/#configuring-formats-for-a-specific-resource-or-operation}
3636
* @param array<int|string, string|string[]>|string|null $outputFormats {@see https://api-platform.com/docs/core/content-negotiation/#configuring-formats-for-a-specific-resource-or-operation}
37-
* @param array<string,array{
37+
* @param Parameters|array<string,array{
3838
* 0: string,
3939
* 1: string
4040
* }|array{
@@ -344,11 +344,17 @@ public function withOutputFormats($outputFormats = null): static
344344
return $self;
345345
}
346346

347-
public function getUriVariables()
347+
/**
348+
* @return array<string, mixed>|null
349+
*/
350+
public function getUriVariables(): mixed
348351
{
349352
return $this->uriVariables;
350353
}
351354

355+
/**
356+
* @param array<string, mixed>|array<int, Link>|list<string> $uriVariables
357+
*/
352358
public function withUriVariables($uriVariables): static
353359
{
354360
$self = clone $this;

src/Metadata/Link.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use ApiPlatform\OpenApi;
1717

1818
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::TARGET_PARAMETER)]
19-
final class Link extends Parameter
19+
final class Link extends Parameter implements UriVariableParameterInterface
2020
{
2121
public function __construct(
2222
private ?string $parameterName = null,

src/Metadata/Parameter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ public function getValue(mixed $default = new ParameterNotFound()): mixed
133133
return $this->extraProperties['_api_values'] ?? $default;
134134
}
135135

136+
/**
137+
* Only use this in a parameter provider, the ApiPlatform\State\Provider\ParameterProvider
138+
* resets this value to extract the correct value on each request.
139+
* It's also possible to set the `_api_query_parameters` request attribute directly and
140+
* API Platform will extract the value from there.
141+
*/
136142
public function setValue(mixed $value): static
137143
{
138144
$this->extraProperties['_api_values'] = $value;

src/Metadata/Parameters.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ public function has(string $key, string $parameterClass = QueryParameter::class)
122122
return false;
123123
}
124124

125+
/**
126+
* @return list<string>
127+
*/
128+
public function keys(): array
129+
{
130+
$keys = [];
131+
foreach ($this->parameters as [$key]) {
132+
$keys[] = $key;
133+
}
134+
135+
return $keys;
136+
}
137+
125138
public function count(): int
126139
{
127140
return \count($this->parameters);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Metadata;
15+
16+
/**
17+
* @experimental
18+
*/
19+
interface UriVariableParameterInterface
20+
{
21+
}

0 commit comments

Comments
 (0)