Skip to content

[Security] Remove using multiple attributes with #[IsGranted] #47144

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 @@ -21,7 +21,7 @@ public function __construct(
/**
* Sets the first argument that will be passed to isGranted().
*/
public array|string|null $attributes = null,
public string $attribute,

/**
* Sets the second argument passed to isGranted().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event)
}
}

if (!$this->authChecker->isGranted($attribute->attributes, $subject)) {
if (!$this->authChecker->isGranted($attribute->attribute, $subject)) {
$message = $attribute->message ?: sprintf('Access Denied by #[IsGranted(%s)] on controller', $this->getIsGrantedString($attribute));

if ($statusCode = $attribute->statusCode) {
throw new HttpException($statusCode, $message);
}

$accessDeniedException = new AccessDeniedException($message);
$accessDeniedException->setAttributes($attribute->attributes);
$accessDeniedException->setAttributes($attribute->attribute);
$accessDeniedException->setSubject($subject);

throw $accessDeniedException;
Expand All @@ -83,11 +83,13 @@ public static function getSubscribedEvents(): array

private function getIsGrantedString(IsGranted $isGranted): string
{
$attributes = array_map(fn ($attribute) => '"'.$attribute.'"', (array) $isGranted->attributes);
$argsString = 1 === \count($attributes) ? reset($attributes) : '['.implode(', ', $attributes).']';
$processValue = fn ($value) => sprintf('"%s"', $value);

if (null !== $isGranted->subject) {
$argsString .= ', "'.implode('", "', (array) $isGranted->subject).'"';
$argsString = $processValue($isGranted->attribute);

if (null !== $subject = $isGranted->subject) {
$subject = array_map($processValue, (array) $subject);
$argsString .= ', '.(1 === \count($subject) ? reset($subject) : '['.implode(', ', $subject).']');
}

return $argsString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,15 @@ public function testExceptionWhenMissingSubjectAttribute()
/**
* @dataProvider getAccessDeniedMessageTests
*/
public function testAccessDeniedMessages(array $attributes, ?string $subject, string $method, string $expectedMessage)
public function testAccessDeniedMessages(string $attribute, string|array|null $subject, string $method, int $numOfArguments, string $expectedMessage)
{
$authChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
$authChecker->expects($this->any())
->method('isGranted')
->willReturn(false);

// avoid the error of the subject not being found in the request attributes
$arguments = [];
if (null !== $subject) {
$arguments[] = 'bar';
}
$arguments = array_fill(0, $numOfArguments, 'bar');

$listener = new IsGrantedAttributeListener($authChecker);

Expand All @@ -236,9 +233,9 @@ public function testAccessDeniedMessages(array $attributes, ?string $subject, st
$this->fail();
} catch (AccessDeniedException $e) {
$this->assertSame($expectedMessage, $e->getMessage());
$this->assertSame($attributes, $e->getAttributes());
$this->assertSame([$attribute], $e->getAttributes());
if (null !== $subject) {
$this->assertSame('bar', $e->getSubject());
$this->assertSame($subject, $e->getSubject());
} else {
$this->assertNull($e->getSubject());
}
Expand All @@ -247,9 +244,9 @@ public function testAccessDeniedMessages(array $attributes, ?string $subject, st

public function getAccessDeniedMessageTests()
{
yield [['ROLE_ADMIN'], null, 'admin', 'Access Denied by #[IsGranted("ROLE_ADMIN")] on controller'];
yield [['ROLE_ADMIN', 'ROLE_USER'], null, 'adminOrUser', 'Access Denied by #[IsGranted(["ROLE_ADMIN", "ROLE_USER"])] on controller'];
yield [['ROLE_ADMIN', 'ROLE_USER'], 'product', 'adminOrUserWithSubject', 'Access Denied by #[IsGranted(["ROLE_ADMIN", "ROLE_USER"], "product")] on controller'];
yield ['ROLE_ADMIN', null, 'admin', 0, 'Access Denied by #[IsGranted("ROLE_ADMIN")] on controller'];
yield ['ROLE_ADMIN', 'bar', 'withSubject', 2, 'Access Denied by #[IsGranted("ROLE_ADMIN", "arg2Name")] on controller'];
yield ['ROLE_ADMIN', ['arg1Name' => 'bar', 'arg2Name' => 'bar'], 'withSubjectArray', 2, 'Access Denied by #[IsGranted("ROLE_ADMIN", ["arg1Name", "arg2Name"])] on controller'];
}

public function testNotFoundHttpException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

use Symfony\Component\Security\Http\Attribute\IsGranted;

#[IsGranted(attributes: ['ROLE_ADMIN', 'ROLE_USER'])]
#[IsGranted(attribute: 'ROLE_USER')]
class IsGrantedAttributeController
{
#[IsGranted(attributes: ['ROLE_ADMIN'])]
#[IsGranted(attribute: 'ROLE_ADMIN')]
public function foo()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,27 @@ public function noAttribute()
{
}

#[IsGranted()]
public function emptyAttribute()
{
}

#[IsGranted(attributes: 'ROLE_ADMIN')]
#[IsGranted(attribute: 'ROLE_ADMIN')]
public function admin()
{
}

#[IsGranted(attributes: ['ROLE_ADMIN', 'ROLE_USER'])]
public function adminOrUser()
{
}

#[IsGranted(attributes: ['ROLE_ADMIN', 'ROLE_USER'], subject: 'product')]
public function adminOrUserWithSubject($product)
{
}

#[IsGranted(attributes: 'ROLE_ADMIN', subject: 'arg2Name')]
#[IsGranted(attribute: 'ROLE_ADMIN', subject: 'arg2Name')]
public function withSubject($arg1Name, $arg2Name)
{
}

#[IsGranted(attributes: 'ROLE_ADMIN', subject: ['arg1Name', 'arg2Name'])]
#[IsGranted(attribute: 'ROLE_ADMIN', subject: ['arg1Name', 'arg2Name'])]
public function withSubjectArray($arg1Name, $arg2Name)
{
}

#[IsGranted(attributes: 'ROLE_ADMIN', subject: 'non_existent')]
#[IsGranted(attribute: 'ROLE_ADMIN', subject: 'non_existent')]
public function withMissingSubject()
{
}

#[IsGranted(attributes: 'ROLE_ADMIN', statusCode: 404, message: 'Not found')]
#[IsGranted(attribute: 'ROLE_ADMIN', message: 'Not found', statusCode: 404)]
public function notFound()
{
}
Expand Down