Skip to content

Commit 8ddda03

Browse files
committed
Introduce trait, add tests, fix comments
1 parent 2d70d9b commit 8ddda03

23 files changed

+265
-60
lines changed

features/authorization/deny.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Feature: Authorization checking
5858
"""
5959
Then the response status code should be 201
6060

61-
Scenario: An user retrieves cannot retrieve an item he doesn't own
61+
Scenario: An user cannot retrieve an item he doesn't own
6262
When I add "Accept" header equal to "application/ld+json"
6363
And I add "Authorization" header equal to "Basic ZHVuZ2xhczprZXZpbg=="
6464
And I send a "GET" request to "/secured_dummies/1"

src/Graphql/Action/EntrypointAction.php renamed to src/GraphQl/Action/EntrypointAction.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql\Action;
14+
namespace ApiPlatform\Core\GraphQl\Action;
1515

16-
use ApiPlatform\Core\Graphql\ExecutorInterface;
17-
use ApiPlatform\Core\Graphql\Type\SchemaBuilderInterface;
16+
use ApiPlatform\Core\GraphQl\ExecutorInterface;
17+
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
1818
use GraphQL\Error\Error;
1919
use GraphQL\Executor\ExecutionResult;
2020
use Symfony\Component\HttpFoundation\JsonResponse;

src/Graphql/Executor.php renamed to src/GraphQl/Executor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql;
14+
namespace ApiPlatform\Core\GraphQl;
1515

1616
use GraphQL\Executor\ExecutionResult;
1717
use GraphQL\GraphQL;

src/Graphql/ExecutorInterface.php renamed to src/GraphQl/ExecutorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql;
14+
namespace ApiPlatform\Core\GraphQl;
1515

1616
use GraphQL\Executor\ExecutionResult;
1717
use GraphQL\Type\Schema;

src/Graphql/Resolver/Factory/CollectionResolverFactory.php renamed to src/GraphQl/Resolver/Factory/CollectionResolverFactory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql\Resolver\Factory;
14+
namespace ApiPlatform\Core\GraphQl\Resolver\Factory;
1515

1616
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
1717
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
1818
use ApiPlatform\Core\DataProvider\PaginatorInterface;
1919
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
2020
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
21-
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
21+
use ApiPlatform\Core\GraphQl\Resolver\ResourceAccessCheckerTrait;
22+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
2223
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2324
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
2425
use GraphQL\Error\Error;
@@ -36,6 +37,8 @@
3637
*/
3738
final class CollectionResolverFactory implements ResolverFactoryInterface
3839
{
40+
use ResourceAccessCheckerTrait;
41+
3942
private $collectionDataProvider;
4043
private $subresourceDataProvider;
4144
private $normalizer;
@@ -76,6 +79,7 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
7679
}
7780

7881
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
82+
$this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $collection);
7983

8084
if (null !== $this->resourceAccessChecker) {
8185
$isGranted = $resourceMetadata->getGraphqlAttribute('query', 'access_control', null, true);

src/Graphql/Resolver/Factory/ItemMutationResolverFactory.php renamed to src/GraphQl/Resolver/Factory/ItemMutationResolverFactory.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql\Resolver\Factory;
14+
namespace ApiPlatform\Core\GraphQl\Resolver\Factory;
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
1818
use ApiPlatform\Core\Exception\InvalidArgumentException;
1919
use ApiPlatform\Core\Exception\ItemNotFoundException;
20-
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
20+
use ApiPlatform\Core\GraphQl\Resolver\ResourceAccessCheckerTrait;
21+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
2122
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2223
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
2324
use GraphQL\Error\Error;
@@ -34,6 +35,8 @@
3435
*/
3536
final class ItemMutationResolverFactory implements ResolverFactoryInterface
3637
{
38+
use ResourceAccessCheckerTrait;
39+
3740
private $iriConverter;
3841
private $dataPersister;
3942
private $normalizer;
@@ -68,12 +71,7 @@ public function __invoke(string $resourceClass = null, string $rootClass = null,
6871
}
6972

7073
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
71-
if (null !== $this->resourceAccessChecker) {
72-
$isGranted = $resourceMetadata->getGraphqlAttribute('query', 'access_control', null, true);
73-
if (null !== $isGranted && !$this->resourceAccessChecker->isGranted($resourceClass, $isGranted, ['object' => $item])) {
74-
throw Error::createLocatedError('Access Denied.', $info->fieldNodes, $info->path);
75-
}
76-
}
74+
$this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $item);
7775

7876
switch ($operationName) {
7977
case 'create':

src/Graphql/Resolver/Factory/ResolverFactoryInterface.php renamed to src/GraphQl/Resolver/Factory/ResolverFactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql\Resolver\Factory;
14+
namespace ApiPlatform\Core\GraphQl\Resolver\Factory;
1515

1616
/**
1717
* Builds a GraphQL resolver.

src/Graphql/Resolver/ItemResolver.php renamed to src/GraphQl/Resolver/ItemResolver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql\Resolver;
14+
namespace ApiPlatform\Core\GraphQl\Resolver;
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Exception\ItemNotFoundException;
18-
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
18+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
1919
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2020
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
2121
use ApiPlatform\Core\Util\ClassInfoTrait;
@@ -34,6 +34,7 @@
3434
final class ItemResolver
3535
{
3636
use ClassInfoTrait;
37+
use ResourceAccessCheckerTrait;
3738

3839
private $iriConverter;
3940
private $resourceAccessChecker;
@@ -68,6 +69,7 @@ public function __invoke($source, $args, $context, ResolveInfo $info)
6869

6970
$resourceClass = $this->getObjectClass($item);
7071
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
72+
$this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $item);
7173

7274
if (null !== $this->resourceAccessChecker) {
7375
$isGranted = $resourceMetadata->getGraphqlAttribute('query', 'access_control', null, true);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
/*
15+
* This file is part of the API Platform project.
16+
*
17+
* (c) Kévin Dunglas <[email protected]>
18+
*
19+
* For the full copyright and license information, please view the LICENSE
20+
* file that was distributed with this source code.
21+
*/
22+
23+
namespace ApiPlatform\Core\GraphQl\Resolver;
24+
25+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
26+
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface;
27+
use GraphQL\Error\Error;
28+
use GraphQL\Type\Definition\ResolveInfo;
29+
30+
/**
31+
* Checks if the current logged in user can access to this resource.
32+
*
33+
* @experimental
34+
*
35+
* @author Kévin Dunglas <[email protected]>
36+
*/
37+
trait ResourceAccessCheckerTrait
38+
{
39+
/**
40+
* @param object $object
41+
*
42+
* @throws Error
43+
*/
44+
public function canAccess(ResourceAccessCheckerInterface $resourceAccessChecker = null, ResourceMetadata $resourceMetadata, string $resourceClass, ResolveInfo $info, $object = null)
45+
{
46+
if (null === $resourceAccessChecker) {
47+
return;
48+
}
49+
50+
$isGranted = $resourceMetadata->getGraphqlAttribute('query', 'access_control', null, true);
51+
if (null === $isGranted || $resourceAccessChecker->isGranted($resourceClass, $isGranted, ['object' => $object])) {
52+
return;
53+
}
54+
55+
throw Error::createLocatedError('Access Denied.', $info->fieldNodes, $info->path);
56+
}
57+
}

src/Graphql/Resolver/ResourceFieldResolver.php renamed to src/GraphQl/Resolver/ResourceFieldResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql\Resolver;
14+
namespace ApiPlatform\Core\GraphQl\Resolver;
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
17-
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
17+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
1818
use GraphQL\Type\Definition\ResolveInfo;
1919

2020
/**

src/Graphql/Serializer/ItemNormalizer.php renamed to src/GraphQl/Serializer/ItemNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* file that was distributed with this source code.
2020
*/
2121

22-
namespace ApiPlatform\Core\Graphql\Serializer;
22+
namespace ApiPlatform\Core\GraphQl\Serializer;
2323

2424
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
2525
use ApiPlatform\Core\Serializer\AbstractItemNormalizer;

src/Graphql/Type/SchemaBuilder.php renamed to src/GraphQl/Type/SchemaBuilder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql\Type;
14+
namespace ApiPlatform\Core\GraphQl\Type;
1515

1616
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
17-
use ApiPlatform\Core\Graphql\Resolver\Factory\ResolverFactoryInterface;
18-
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
17+
use ApiPlatform\Core\GraphQl\Resolver\Factory\ResolverFactoryInterface;
18+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
1919
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2020
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2121
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;

src/Graphql/Type/SchemaBuilderInterface.php renamed to src/GraphQl/Type/SchemaBuilderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Graphql\Type;
14+
namespace ApiPlatform\Core\GraphQl\Type;
1515

1616
use GraphQL\Type\Schema;
1717

src/Security/EventListener/DenyAccessListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
4646
}
4747

4848
$this->resourceAccessChecker = new ResourceAccessChecker($resourceAccessCheckerOrExpressionLanguage, $authenticationTrustResolver, $roleHierarchy, $tokenStorage, $authorizationChecker);
49-
@trigger_error(sprintf('Passing an instance of "%s" 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);
49+
@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);
5050
}
5151

5252
/**

tests/Graphql/Action/EntrypointActionTest.php renamed to tests/GraphQl/Action/EntrypointActionTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Tests\Graphql\Action;
14+
namespace ApiPlatform\Core\Tests\GraphQl\Action;
1515

16-
use ApiPlatform\Core\Graphql\Action\EntrypointAction;
17-
use ApiPlatform\Core\Graphql\ExecutorInterface;
18-
use ApiPlatform\Core\Graphql\Type\SchemaBuilderInterface;
16+
use ApiPlatform\Core\GraphQl\Action\EntrypointAction;
17+
use ApiPlatform\Core\GraphQl\ExecutorInterface;
18+
use ApiPlatform\Core\GraphQl\Type\SchemaBuilderInterface;
1919
use GraphQL\Executor\ExecutionResult;
2020
use GraphQL\Type\Schema;
2121
use PHPUnit\Framework\TestCase;

tests/Graphql/Resolver/Factory/CollectionResolverFactoryTest.php renamed to tests/GraphQl/Resolver/Factory/CollectionResolverFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Tests\Graphql\Resolver\Factory;
14+
namespace ApiPlatform\Core\Tests\GraphQl\Resolver\Factory;
1515

1616
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
1717
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
1818
use ApiPlatform\Core\DataProvider\PaginatorInterface;
1919
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
20-
use ApiPlatform\Core\Graphql\Resolver\Factory\CollectionResolverFactory;
21-
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
20+
use ApiPlatform\Core\GraphQl\Resolver\Factory\CollectionResolverFactory;
21+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
2222
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2323
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2424
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;

tests/Graphql/Resolver/Factory/ItemMutationResolverFactoryTest.php renamed to tests/GraphQl/Resolver/Factory/ItemMutationResolverFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Tests\Graphql\Resolver\Factory;
14+
namespace ApiPlatform\Core\Tests\GraphQl\Resolver\Factory;
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
1818
use ApiPlatform\Core\Exception\ItemNotFoundException;
19-
use ApiPlatform\Core\Graphql\Resolver\Factory\ItemMutationResolverFactory;
20-
use ApiPlatform\Core\Graphql\Resolver\Factory\ResolverFactoryInterface;
19+
use ApiPlatform\Core\GraphQl\Resolver\Factory\ItemMutationResolverFactory;
20+
use ApiPlatform\Core\GraphQl\Resolver\Factory\ResolverFactoryInterface;
2121
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2222
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2323
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;

tests/Graphql/Resolver/ItemResolverTest.php renamed to tests/GraphQl/Resolver/ItemResolverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Tests\Graphql\Resolver;
14+
namespace ApiPlatform\Core\Tests\GraphQl\Resolver;
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Exception\ItemNotFoundException;
18-
use ApiPlatform\Core\Graphql\Resolver\ItemResolver;
18+
use ApiPlatform\Core\GraphQl\Resolver\ItemResolver;
1919
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2020
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
2121
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;

tests/Graphql/Resolver/ResourceFieldResolverTest.php renamed to tests/GraphQl/Resolver/ResourceFieldResolverTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Tests\Graphql\Resolver;
14+
namespace ApiPlatform\Core\Tests\GraphQl\Resolver;
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
17-
use ApiPlatform\Core\Graphql\Resolver\ResourceFieldResolver;
18-
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
17+
use ApiPlatform\Core\GraphQl\Resolver\ResourceFieldResolver;
18+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
1919
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
2020
use GraphQL\Type\Definition\ResolveInfo;
2121
use PHPUnit\Framework\TestCase;

tests/Graphql/Serializer/ItemNormalizerTest.php renamed to tests/GraphQl/Serializer/ItemNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Tests\Graphql\Serializer;
14+
namespace ApiPlatform\Core\Tests\GraphQl\Serializer;
1515

1616
use ApiPlatform\Core\Api\IriConverterInterface;
1717
use ApiPlatform\Core\Api\ResourceClassResolverInterface;
1818
use ApiPlatform\Core\Exception\InvalidArgumentException;
19-
use ApiPlatform\Core\Graphql\Serializer\ItemNormalizer;
19+
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
2020
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2121
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2222
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;

tests/Graphql/Type/SchemaBuilderTest.php renamed to tests/GraphQl/Type/SchemaBuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Core\Tests\Graphql\Type;
14+
namespace ApiPlatform\Core\Tests\GraphqQl\Type;
1515

1616
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
17-
use ApiPlatform\Core\Graphql\Resolver\Factory\ResolverFactoryInterface;
18-
use ApiPlatform\Core\Graphql\Type\SchemaBuilder;
17+
use ApiPlatform\Core\GraphQl\Resolver\Factory\ResolverFactoryInterface;
18+
use ApiPlatform\Core\GraphQl\Type\SchemaBuilder;
1919
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2020
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2121
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;

0 commit comments

Comments
 (0)