Skip to content

Commit 4d4c411

Browse files
committed
minor #48102 [Security] Remove special case for #[IsGranted()] subject (HypeMC)
This PR was merged into the 6.2 branch. Discussion ---------- [Security] Remove special case for `#[IsGranted()]` subject | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Fix #48080 (comment) | License | MIT | Doc PR | - Addresses a comment by `@stof` Instead of having `request` as a special case, an expression can be used instead: ```diff -#[IsGranted(attribute: 'SOME_ATTRIBUTE', subject: 'request')] +#[IsGranted(attribute: 'SOME_ATTRIBUTE', subject: new Expression('request'))] public function index() { } ``` Commits ------- 3e0ac4f [Security] Remove special case for #[IsGranted()] subject
2 parents bc6e689 + 3e0ac4f commit 4d4c411

File tree

3 files changed

+6
-34
lines changed

3 files changed

+6
-34
lines changed

src/Symfony/Component/Security/Http/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

src/Symfony/Component/Security/Http/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
}

src/Symfony/Component/Security/Http/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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy