|
29 | 29 | use Symfony\Component\Security\Core\Authorization\Voter\Vote;
|
30 | 30 | use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
31 | 31 | use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
| 32 | +use Symfony\Component\Security\Http\Attribute\IsGranted; |
32 | 33 | use Symfony\Component\Security\Http\EventListener\IsGrantedAttributeListener;
|
33 | 34 | use Symfony\Component\Security\Http\Tests\Fixtures\IsGrantedAttributeController;
|
34 | 35 | use Symfony\Component\Security\Http\Tests\Fixtures\IsGrantedAttributeMethodsController;
|
@@ -524,4 +525,59 @@ public function testSkipsAuthorizationWhenMethodDoesNotMatchStringConstraint()
|
524 | 525 | $listener = new IsGrantedAttributeListener($authChecker);
|
525 | 526 | $listener->onKernelControllerArguments($event);
|
526 | 527 | }
|
| 528 | + |
| 529 | + public function testFiltersOnlyIsGrantedAttributesUsingInstanceof() |
| 530 | + { |
| 531 | + $authChecker = $this->createMock(AuthorizationCheckerInterface::class); |
| 532 | + $authChecker->expects($this->once()) |
| 533 | + ->method('isGranted') |
| 534 | + ->with('ROLE_ADMIN') |
| 535 | + ->willReturn(true); |
| 536 | + |
| 537 | + $controller = [new IsGrantedAttributeMethodsController(), 'admin']; |
| 538 | + $event = new ControllerArgumentsEvent( |
| 539 | + $this->createMock(HttpKernelInterface::class), |
| 540 | + $controller, |
| 541 | + [], |
| 542 | + new Request(), |
| 543 | + null |
| 544 | + ); |
| 545 | + |
| 546 | + // Inject mixed attributes: one IsGranted and one unrelated object; only IsGranted should be processed |
| 547 | + $event->setController($controller, [ |
| 548 | + IsGranted::class => [new IsGranted('ROLE_ADMIN')], |
| 549 | + \stdClass::class => [new \stdClass()], |
| 550 | + ]); |
| 551 | + |
| 552 | + $listener = new IsGrantedAttributeListener($authChecker); |
| 553 | + $listener->onKernelControllerArguments($event); |
| 554 | + } |
| 555 | + |
| 556 | + public function testSupportsSubclassOfIsGrantedViaInstanceof() |
| 557 | + { |
| 558 | + $authChecker = $this->createMock(AuthorizationCheckerInterface::class); |
| 559 | + $authChecker->expects($this->once()) |
| 560 | + ->method('isGranted') |
| 561 | + ->with('ROLE_ADMIN') |
| 562 | + ->willReturn(true); |
| 563 | + |
| 564 | + $controller = [new IsGrantedAttributeMethodsController(), 'admin']; |
| 565 | + $event = new ControllerArgumentsEvent( |
| 566 | + $this->createMock(HttpKernelInterface::class), |
| 567 | + $controller, |
| 568 | + [], |
| 569 | + new Request(), |
| 570 | + null |
| 571 | + ); |
| 572 | + |
| 573 | + $custom = new class('ROLE_ADMIN') extends IsGranted {}; |
| 574 | + |
| 575 | + // Inject subclass instance; instanceof IsGranted should match |
| 576 | + $event->setController($controller, [ |
| 577 | + $custom::class => [$custom], |
| 578 | + ]); |
| 579 | + |
| 580 | + $listener = new IsGrantedAttributeListener($authChecker); |
| 581 | + $listener->onKernelControllerArguments($event); |
| 582 | + } |
527 | 583 | }
|
0 commit comments