Skip to content

Commit 9bce092

Browse files
committed
Improvements and tests for the me endpoint
1 parent 74ef5ed commit 9bce092

2 files changed

Lines changed: 38 additions & 37 deletions

File tree

src/ApiPlatform/Api/IriConverter.php

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace Silverback\ApiComponentsBundle\ApiPlatform\Api;
1515

16-
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
17-
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
1816
use ApiPlatform\Metadata\Get;
1917
use ApiPlatform\Metadata\IriConverterInterface;
2018
use ApiPlatform\Metadata\Operation;
@@ -37,40 +35,43 @@ public function getResourceFromIri(string $iri, array $context = [], ?Operation
3735
return $this->decorated->getResourceFromIri($iri, $context, $operation);
3836
}
3937

40-
// We want relations when they are found, to use the IRI with the path
41-
42-
/**
43-
* @throws ResourceClassNotFoundException
44-
* @throws \Exception
45-
*/
46-
public function getIriFromResource($resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null, array $context = []): ?string
38+
private function getUserGetOperation(?Operation $operation = null): Operation
4739
{
48-
if ($operation?->getName() === 'me') {
49-
// we do not want to return the /me IRI if a Get endpoint is configured. The IRI should be canonical to the user's ID etc.
40+
// we do not want to return the /me IRI if a Get endpoint is configured. The IRI should be canonical to the user's ID etc.
5041

51-
// get the API metadata of the class
52-
// find the Get operation - ApiPlatform\Metadata\Get
53-
// use this uriTemplate instead, overwrite $operation and the operation in context
54-
$resourceIterator = $this->resourceMetadataCollectionFactory->create($operation->getClass())->getIterator();
55-
while($resourceIterator->valid()) {
56-
$current = $resourceIterator->current();
57-
/**
58-
* @var Operations $resourceOperations
59-
*/
60-
$resourceOperations = $current->getOperations();
61-
$operationIterator = $resourceOperations->getIterator();
62-
while($operationIterator->valid()) {
63-
$checkOperation = $operationIterator->current();
64-
if ($checkOperation instanceof Get) {
65-
$operation = $checkOperation;
66-
$context['operation'] = $checkOperation;
67-
break 2;
68-
}
69-
$operationIterator->next();
42+
// get the API metadata of the class
43+
// find the Get operation - ApiPlatform\Metadata\Get
44+
// use this uriTemplate instead, overwrite $operation and the operation in context
45+
$resourceIterator = $this->resourceMetadataCollectionFactory->create($operation->getClass())->getIterator();
46+
while ($resourceIterator->valid()) {
47+
$current = $resourceIterator->current();
48+
/**
49+
* @var Operations $resourceOperations
50+
*/
51+
$resourceOperations = $current->getOperations();
52+
$operationIterator = $resourceOperations->getIterator();
53+
while ($operationIterator->valid()) {
54+
$checkOperation = $operationIterator->current();
55+
if ($checkOperation instanceof Get) {
56+
return $checkOperation;
7057
}
71-
$resourceIterator->next();
58+
$operationIterator->next();
7259
}
60+
$resourceIterator->next();
7361
}
62+
63+
return $operation;
64+
}
65+
66+
// We want relations when they are found, to use the IRI with the path
67+
public function getIriFromResource($resource, int $referenceType = UrlGeneratorInterface::ABS_PATH, ?Operation $operation = null, array $context = []): ?string
68+
{
69+
if ('me' === $operation?->getName()) {
70+
$checkOperation = $this->getUserGetOperation($operation);
71+
$operation = $checkOperation;
72+
$context['operation'] = $checkOperation;
73+
}
74+
7475
$originalIri = $this->decorated->getIriFromResource($resource, $referenceType, $operation, $context);
7576

7677
if (!$resource instanceof Route || !($path = $resource->getPath())) {

tests/Functional/TestBundle/Entity/User.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
* @author Daniel West <daniel@silverback.is>
2828
*/
2929
#[ApiResource(operations: [
30-
new GetCollection( order: ['createdAt' => 'DESC'], security: "is_granted('ROLE_SUPER_ADMIN')"),
31-
new Post( security: "is_granted('ROLE_SUPER_ADMIN')" ),
32-
new Get( security: "is_granted('ROLE_SUPER_ADMIN') or object.getId() == user.getId()" ),
33-
new Put( security: "is_granted('ROLE_SUPER_ADMIN') or object.getId() == user.getId()" ),
34-
new Patch( security: "is_granted('ROLE_SUPER_ADMIN') or object.getId() == user.getId()" ),
35-
new Delete( security: "is_granted('ROLE_SUPER_ADMIN')" )
30+
new GetCollection(order: ['createdAt' => 'DESC'], security: "is_granted('ROLE_SUPER_ADMIN')"),
31+
new Post(security: "is_granted('ROLE_SUPER_ADMIN')"),
32+
new Get(security: "is_granted('ROLE_SUPER_ADMIN') or object.getId() == user.getId()"),
33+
new Put(security: "is_granted('ROLE_SUPER_ADMIN') or object.getId() == user.getId()"),
34+
new Patch(security: "is_granted('ROLE_SUPER_ADMIN') or object.getId() == user.getId()"),
35+
new Delete(security: "is_granted('ROLE_SUPER_ADMIN')"),
3636
])]
3737
#[ORM\Entity]
3838
#[ORM\Table(name: '`user`')]

0 commit comments

Comments
 (0)