-
-
Notifications
You must be signed in to change notification settings - Fork 940
feat(symfony): isGranted before provider #7500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Symfony\Security; | ||
|
|
||
| interface ObjectVariableCheckerInterface | ||
| { | ||
| /** | ||
| * @param string $expression a Expression Language string | ||
| * @param array<string, mixed> $variables | ||
| */ | ||
| public function usesObjectVariable(string $expression, array $variables = []): bool; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |||||
| use ApiPlatform\Metadata\ResourceAccessCheckerInterface; | ||||||
| use ApiPlatform\State\ProviderInterface; | ||||||
| use ApiPlatform\Symfony\Security\Exception\AccessDeniedException; | ||||||
| use ApiPlatform\Symfony\Security\ObjectVariableCheckerInterface; | ||||||
| use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -59,15 +60,15 @@ public function provide(Operation $operation, array $uriVariables = [], array $c | |||||
| $message = $operation->getSecurityMessage(); | ||||||
| } | ||||||
|
|
||||||
| $body = $this->decorated->provide($operation, $uriVariables, $context); | ||||||
| if (null === $isGranted) { | ||||||
| return $body; | ||||||
| if ( | ||||||
| null === $isGranted | ||||||
| // On a GraphQl QueryCollection we want to perform security stage only on the top-level query | ||||||
| || ($operation instanceof QueryCollection && null !== ($context['source'] ?? null)) | ||||||
| ) { | ||||||
| return $this->decorated->provide($operation, $uriVariables, $context); | ||||||
| } | ||||||
|
|
||||||
| // On a GraphQl QueryCollection we want to perform security stage only on the top-level query | ||||||
| if ($operation instanceof QueryCollection && null !== ($context['source'] ?? null)) { | ||||||
| return $body; | ||||||
| } | ||||||
| $body = 'pre_read' === $this->event ? null : $this->decorated->provide($operation, $uriVariables, $context); | ||||||
|
|
||||||
| if ($operation instanceof HttpOperation) { | ||||||
| $request = $context['request'] ?? null; | ||||||
|
|
@@ -84,10 +85,14 @@ public function provide(Operation $operation, array $uriVariables = [], array $c | |||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| if ('pre_read' === $this->event && $this->resourceAccessChecker instanceof ObjectVariableCheckerInterface && $this->resourceAccessChecker->usesObjectVariable($isGranted, $resourceAccessCheckerContext)) { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, there is likely a missing test, because the missing use statement hasn't been caught.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it did fail on behat :) |
||||||
| return $this->decorated->provide($operation, $uriVariables, $context); | ||||||
| } | ||||||
|
|
||||||
| if (!$this->resourceAccessChecker->isGranted($operation->getClass(), $isGranted, $resourceAccessCheckerContext)) { | ||||||
| $operation instanceof GraphQlOperation ? throw new AccessDeniedHttpException($message ?? 'Access Denied.') : throw new AccessDeniedException($message ?? 'Access Denied.'); | ||||||
| } | ||||||
|
|
||||||
| return $body; | ||||||
| return 'pre_read' === $this->event ? $this->decorated->provide($operation, $uriVariables, $context) : $body; | ||||||
soyuka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource; | ||
|
|
||
| use ApiPlatform\Metadata\ApiResource; | ||
| use ApiPlatform\Metadata\Get; | ||
| use ApiPlatform\Metadata\Operation; | ||
|
|
||
| #[ApiResource( | ||
| operations: [ | ||
| new Get(uriTemplate: 'is_granted_tests/{id}', security: 'is_granted("ROLE_ADMIN")', uriVariables: ['id'], provider: [self::class, 'provide']), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the test really working? Because I expected
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this test is working, when no There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, that's on me then. I expected the test to work a little bit differently and I was looking for tests both with and without object in security to see that it behaves correctly in all cases
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh we already have a bunch of tests with |
||
| new Get(uriTemplate: 'is_granted_test_call_provider/{id}', uriVariables: ['id'], security: 'is_granted("ROLE_ADMIN")', provider: [self::class, 'provideShouldNotBeCalled']), | ||
| ] | ||
| )] | ||
| class IsGrantedTestResource | ||
| { | ||
| private ?int $id = null; | ||
|
|
||
| public function getId(): ?int | ||
| { | ||
| return $this->id; | ||
| } | ||
|
|
||
| public static function provide(Operation $operation, array $uriVariables = [], array $context = []) | ||
| { | ||
| return new self(); | ||
| } | ||
|
|
||
| public static function provideShouldNotBeCalled(Operation $operation, array $uriVariables = [], array $context = []) | ||
| { | ||
| throw new \RuntimeException('provider should not get called'); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Tests\Functional; | ||
|
|
||
| use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; | ||
| use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\IsGrantedTestResource; | ||
| use ApiPlatform\Tests\SetupClassResourcesTrait; | ||
| use Symfony\Component\Security\Core\User\InMemoryUser; | ||
|
|
||
| final class IsGrantedTest extends ApiTestCase | ||
| { | ||
| use SetupClassResourcesTrait; | ||
|
|
||
| protected static ?bool $alwaysBootKernel = false; | ||
|
|
||
| /** | ||
| * @return class-string[] | ||
| */ | ||
| public static function getResources(): array | ||
| { | ||
| return [IsGrantedTestResource::class]; | ||
| } | ||
|
|
||
| public function testGetIsGrantedAsAdmin(): void | ||
| { | ||
| $client = self::createClient(); | ||
| $client->loginUser(new InMemoryUser('admin', 'password', ['ROLE_ADMIN'])); | ||
|
|
||
| $client->request('GET', '/is_granted_tests/1'); | ||
| $this->assertResponseIsSuccessful(); | ||
| } | ||
|
|
||
| public function testGetIsGrantedAsUser(): void | ||
| { | ||
| $client = self::createClient(); | ||
| $client->loginUser(new InMemoryUser('user', 'password', ['ROLE_USER'])); | ||
|
|
||
| $client->request('GET', '/is_granted_tests/1'); | ||
| $this->assertResponseStatusCodeSame(403); | ||
| } | ||
|
|
||
| public function testGetIsGrantedAsAnonymous(): void | ||
| { | ||
| $client = self::createClient(); | ||
|
|
||
| $client->request('GET', '/is_granted_tests/1'); | ||
| $this->assertResponseStatusCodeSame(401); | ||
| } | ||
|
|
||
| public function testGetIsGrantedShouldNotCallProvider(): void | ||
| { | ||
| $client = self::createClient(); | ||
|
|
||
| $client->request('GET', '/is_granted_test_call_provider/1'); | ||
| $this->assertResponseStatusCodeSame(401); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.