Skip to content

[Security] Remove special case for #[IsGranted()] subject #48102

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ private function getIsGrantedSubject(string|Expression $subjectRef, Request $req
}

if (!\array_key_exists($subjectRef, $arguments)) {
if ('request' === $subjectRef) {
return $request;
}
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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;
Copy link
Member Author

@HypeMC HypeMC Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a mistake I made in my original PR #46978

use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Http\EventListener\IsGrantedAttributeListener;
use Symfony\Component\Security\Http\Tests\Fixtures\IsGrantedAttributeController;
Expand Down Expand Up @@ -363,7 +363,7 @@ public function testIsGrantedWithNestedExpressionInSubject()
$listener->onKernelControllerArguments($event);
}

public function testIsGrantedWithRequestAsSubjectAndNoArgument()
public function testIsGrantedWithRequestAsSubject()
{
$request = new Request();

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

$event = new ControllerArgumentsEvent(
$this->createMock(HttpKernelInterface::class),
[new IsGrantedAttributeMethodsController(), 'withRequestAsSubjectAndNoArgument'],
[new IsGrantedAttributeMethodsController(), 'withRequestAsSubject'],
[],
$request,
null
);

$listener = new IsGrantedAttributeListener($authChecker);
$listener->onKernelControllerArguments($event);
}

public function testIsGrantedWithRequestAsSubjectAndArgument()
{
$authChecker = $this->createMock(AuthorizationCheckerInterface::class);
$authChecker->expects($this->once())
->method('isGranted')
->with('SOME_VOTER', 'foobar')
->willReturn(true);

$event = new ControllerArgumentsEvent(
$this->createMock(HttpKernelInterface::class),
[new IsGrantedAttributeMethodsController(), 'withRequestAsSubjectAndArgument'],
['foobar'],
new Request(),
null
);

$listener = new IsGrantedAttributeListener($authChecker);
$listener = new IsGrantedAttributeListener($authChecker, new ExpressionLanguage());
$listener->onKernelControllerArguments($event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@ public function withNestedExpressionInSubject($post, $arg2Name)
{
}

#[IsGranted(attribute: 'SOME_VOTER', subject: 'request')]
public function withRequestAsSubjectAndNoArgument()
{
}

#[IsGranted(attribute: 'SOME_VOTER', subject: 'request')]
public function withRequestAsSubjectAndArgument($request)
#[IsGranted(attribute: 'SOME_VOTER', subject: new Expression('request'))]
public function withRequestAsSubject()
{
}
}