-
Notifications
You must be signed in to change notification settings - Fork 2
IBX-10170: Introduced AccessCheckController contract #111
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
293d4b0
IBX-10170: Introduced AccessCheckController contract
ViniTou 51b55b0
IBX-10170: renamed to RestrictedControllerInterface
ViniTou cf2acf3
IBX-10170: Make access check work with all types of controller refere…
ViniTou 0daae07
cs fix
ViniTou 10124be
added phpdoc
ViniTou 2ba7b59
moved from ControllerArgumentsEvent to ControllerEvent
ViniTou 0e801fb
applied cr remarks
ViniTou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/contracts/Controller/AuthenticatedRememberedCheckTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Contracts\User\Controller; | ||
|
|
||
| trait AuthenticatedRememberedCheckTrait | ||
| { | ||
| public function performAccessCheck(): void | ||
| { | ||
| $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED'); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/contracts/Controller/RestrictedControllerInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Contracts\User\Controller; | ||
|
|
||
| interface RestrictedControllerInterface | ||
| { | ||
| public function performAccessCheck(): void; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\User\EventListener; | ||
|
|
||
| use Ibexa\Contracts\User\Controller\RestrictedControllerInterface; | ||
| use Symfony\Component\DependencyInjection\Attribute\AutowireIterator; | ||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
| use Symfony\Component\HttpKernel\Event\ControllerEvent; | ||
|
|
||
| final class PerformAccessCheckSubscriber implements EventSubscriberInterface | ||
| { | ||
| /** | ||
| * @param iterable<object> $controllers | ||
| */ | ||
| public function __construct( | ||
| #[AutowireIterator('controller.service_arguments')] | ||
| private readonly iterable $controllers | ||
| ) { | ||
| } | ||
|
|
||
| public function onControllerEvent(ControllerEvent $event): void | ||
| { | ||
| $controller = $event->getController(); | ||
| if (is_array($controller) && $controller[0] instanceof RestrictedControllerInterface) { | ||
| $controller[0]->performAccessCheck(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ($controller instanceof RestrictedControllerInterface) { | ||
| $controller->performAccessCheck(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (is_string($controller) && str_contains($controller, '::')) { | ||
| [$class] = explode('::', $controller, 2); | ||
|
|
||
| foreach ($this->controllers as $controllerInstance) { | ||
| if ($controllerInstance::class === $class && $controllerInstance instanceof RestrictedControllerInterface) { | ||
| $controllerInstance->performAccessCheck(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static function getSubscribedEvents(): array | ||
| { | ||
| return [ | ||
| ControllerEvent::class => 'onControllerEvent', | ||
| ]; | ||
| } | ||
| } |
106 changes: 106 additions & 0 deletions
106
tests/lib/EventListener/PerformAccessCheckSubscriberTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\User\EventListener; | ||
|
|
||
| use Ibexa\Contracts\User\Controller\RestrictedControllerInterface; | ||
| use Ibexa\User\EventListener\PerformAccessCheckSubscriber; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpKernel\Event\ControllerEvent; | ||
| use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
|
||
| final class PerformAccessCheckSubscriberTest extends TestCase | ||
| { | ||
| private PerformAccessCheckSubscriber $subscriber; | ||
|
|
||
| private HttpKernelInterface $kernel; | ||
|
|
||
| private Request $request; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->kernel = $this->createMock(HttpKernelInterface::class); | ||
| $this->request = new Request(); | ||
| $this->subscriber = new PerformAccessCheckSubscriber([]); | ||
| } | ||
|
|
||
| public function testArrayController(): void | ||
| { | ||
| $controller = new MockControllerInterface(); | ||
| $event = new ControllerEvent( | ||
| $this->kernel, | ||
| [$controller, 'action'], | ||
| $this->request, | ||
| HttpKernelInterface::MAIN_REQUEST | ||
| ); | ||
|
|
||
| $this->subscriber->onControllerEvent($event); | ||
|
|
||
| self::assertTrue($controller->wasCheckPerformed()); | ||
| } | ||
|
|
||
| public function testInvokableController(): void | ||
| { | ||
| $controller = new MockControllerInterface(); | ||
| $event = new ControllerEvent( | ||
| $this->kernel, | ||
| $controller, | ||
| $this->request, | ||
| HttpKernelInterface::MAIN_REQUEST | ||
| ); | ||
|
|
||
| $this->subscriber->onControllerEvent($event); | ||
|
|
||
| self::assertTrue($controller->wasCheckPerformed()); | ||
| } | ||
|
|
||
| public function testStringControllerWithServiceLookup(): void | ||
| { | ||
| $controller = new MockControllerInterface(); | ||
| $this->subscriber = new PerformAccessCheckSubscriber([$controller]); | ||
|
|
||
| $event = new ControllerEvent( | ||
| $this->kernel, | ||
| MockControllerInterface::class . '::staticAction', | ||
| $this->request, | ||
| HttpKernelInterface::MAIN_REQUEST | ||
| ); | ||
|
|
||
| $this->subscriber->onControllerEvent($event); | ||
|
|
||
| self::assertTrue($controller->wasCheckPerformed()); | ||
| } | ||
| } | ||
|
|
||
| final class MockControllerInterface implements RestrictedControllerInterface | ||
| { | ||
| private bool $checkPerformed = false; | ||
|
|
||
| public function performAccessCheck(): void | ||
| { | ||
| $this->checkPerformed = true; | ||
| } | ||
|
|
||
| public function wasCheckPerformed(): bool | ||
| { | ||
| return $this->checkPerformed; | ||
| } | ||
|
|
||
| public function __invoke(): void | ||
| { | ||
| } | ||
|
|
||
| public function action(): void | ||
| { | ||
| } | ||
|
|
||
| public static function staticAction(): void | ||
| { | ||
| } | ||
| } | ||
ViniTou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.