Skip to content

Commit 435b475

Browse files
committed
[Security] Remove special case for #[IsGranted()] subject
1 parent 7e4e1b5 commit 435b475

File tree

3 files changed

+6
-34
lines changed

3 files changed

+6
-34
lines changed

EventListener/IsGrantedAttributeListener.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ private function getIsGrantedSubject(string|Expression $subjectRef, Request $req
9292
}
9393

9494
if (!\array_key_exists($subjectRef, $arguments)) {
95-
if ('request' === $subjectRef) {
96-
return $request;
97-
}
9895
throw new RuntimeException(sprintf('Could not find the subject "%s" for the #[IsGranted] attribute. Try adding a "$%s" argument to your controller method.', $subjectRef, $subjectRef));
9996
}
10097

Tests/EventListener/IsGrantedAttributeListenerTest.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\ExpressionLanguage\Expression;
16+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
1819
use Symfony\Component\HttpKernel\Exception\HttpException;
1920
use Symfony\Component\HttpKernel\HttpKernelInterface;
2021
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
21-
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
2222
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
2323
use Symfony\Component\Security\Http\EventListener\IsGrantedAttributeListener;
2424
use Symfony\Component\Security\Http\Tests\Fixtures\IsGrantedAttributeController;
@@ -363,7 +363,7 @@ public function testIsGrantedWithNestedExpressionInSubject()
363363
$listener->onKernelControllerArguments($event);
364364
}
365365

366-
public function testIsGrantedWithRequestAsSubjectAndNoArgument()
366+
public function testIsGrantedWithRequestAsSubject()
367367
{
368368
$request = new Request();
369369

@@ -375,33 +375,13 @@ public function testIsGrantedWithRequestAsSubjectAndNoArgument()
375375

376376
$event = new ControllerArgumentsEvent(
377377
$this->createMock(HttpKernelInterface::class),
378-
[new IsGrantedAttributeMethodsController(), 'withRequestAsSubjectAndNoArgument'],
378+
[new IsGrantedAttributeMethodsController(), 'withRequestAsSubject'],
379379
[],
380380
$request,
381381
null
382382
);
383383

384-
$listener = new IsGrantedAttributeListener($authChecker);
385-
$listener->onKernelControllerArguments($event);
386-
}
387-
388-
public function testIsGrantedWithRequestAsSubjectAndArgument()
389-
{
390-
$authChecker = $this->createMock(AuthorizationCheckerInterface::class);
391-
$authChecker->expects($this->once())
392-
->method('isGranted')
393-
->with('SOME_VOTER', 'foobar')
394-
->willReturn(true);
395-
396-
$event = new ControllerArgumentsEvent(
397-
$this->createMock(HttpKernelInterface::class),
398-
[new IsGrantedAttributeMethodsController(), 'withRequestAsSubjectAndArgument'],
399-
['foobar'],
400-
new Request(),
401-
null
402-
);
403-
404-
$listener = new IsGrantedAttributeListener($authChecker);
384+
$listener = new IsGrantedAttributeListener($authChecker, new ExpressionLanguage());
405385
$listener->onKernelControllerArguments($event);
406386
}
407387
}

Tests/Fixtures/IsGrantedAttributeMethodsController.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,8 @@ public function withNestedExpressionInSubject($post, $arg2Name)
6363
{
6464
}
6565

66-
#[IsGranted(attribute: 'SOME_VOTER', subject: 'request')]
67-
public function withRequestAsSubjectAndNoArgument()
68-
{
69-
}
70-
71-
#[IsGranted(attribute: 'SOME_VOTER', subject: 'request')]
72-
public function withRequestAsSubjectAndArgument($request)
66+
#[IsGranted(attribute: 'SOME_VOTER', subject: new Expression('request'))]
67+
public function withRequestAsSubject()
7368
{
7469
}
7570
}

0 commit comments

Comments
 (0)