Skip to content

Commit 7d0320e

Browse files
committed
Fixes
1 parent e9735c6 commit 7d0320e

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/Symfony/Component/Security/Core/Exception/AccessDeniedException.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Security\Core\Exception;
1313

14+
use Symfony\Component\ExpressionLanguage\Expression;
15+
1416
/**
1517
* AccessDeniedException is thrown when the account has not the required role.
1618
*
@@ -31,9 +33,9 @@ public function getAttributes(): array
3133
return $this->attributes;
3234
}
3335

34-
public function setAttributes(array|string $attributes)
36+
public function setAttributes(array|string|Expression $attributes)
3537
{
36-
$this->attributes = (array) $attributes;
38+
$this->attributes = $attributes instanceof Expression ? [$attributes] : (array) $attributes;
3739
}
3840

3941
public function getSubject(): mixed

src/Symfony/Component/Security/Http/EventListener/IsGrantedAttributeListener.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event)
5050

5151
if ($subjectRef) {
5252
if ($subjectRef instanceof Expression) {
53-
$this->expressionLanguage ??= new ExpressionLanguage();
54-
53+
$this->expressionLanguage ??= class_exists(Expression::class)
54+
? new ExpressionLanguage()
55+
: throw new \LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".')
56+
;
5557
$subject = $this->expressionLanguage->evaluate($subjectRef, [
5658
'args' => $arguments,
5759
]);
@@ -77,7 +79,7 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event)
7779
}
7880

7981
$accessDeniedException = new AccessDeniedException($message);
80-
$accessDeniedException->setAttributes($attribute->attributes instanceof Expression ? (string) $attribute->attributes : $attribute->attributes);
82+
$accessDeniedException->setAttributes($attribute->attributes);
8183
$accessDeniedException->setSubject($subject);
8284

8385
throw $accessDeniedException;

src/Symfony/Component/Security/Http/Tests/EventListener/IsGrantedAttributeListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function testAccessDeniedMessages(array $attributes, ?string $subject, st
243243
$this->fail();
244244
} catch (AccessDeniedException $e) {
245245
$this->assertSame($expectedMessage, $e->getMessage());
246-
$this->assertSame($attributes, $e->getAttributes());
246+
$this->assertEquals($attributes, $e->getAttributes());
247247
if (null !== $subject) {
248248
$this->assertSame('bar', $e->getSubject());
249249
} else {
@@ -257,8 +257,8 @@ public function getAccessDeniedMessageTests()
257257
yield [['ROLE_ADMIN'], null, 'admin', 'Access Denied by #[IsGranted("ROLE_ADMIN")] on controller'];
258258
yield [['ROLE_ADMIN', 'ROLE_USER'], null, 'adminOrUser', 'Access Denied by #[IsGranted(["ROLE_ADMIN", "ROLE_USER"])] on controller'];
259259
yield [['ROLE_ADMIN', 'ROLE_USER'], 'product', 'adminOrUserWithSubject', 'Access Denied by #[IsGranted(["ROLE_ADMIN", "ROLE_USER"], "product")] on controller'];
260-
yield [['"ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)'], 'post', 'withExpressionInAttribute', 'Access Denied by #[IsGranted(""ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)", "post")] on controller'];
261-
yield [['user === subject'], 'post', 'withExpressionInSubject', 'Access Denied by #[IsGranted("user === subject", "args["post"].getAuthor()")] on controller'];
260+
yield [[new Expression('"ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)')], 'post', 'withExpressionInAttribute', 'Access Denied by #[IsGranted(""ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)", "post")] on controller'];
261+
yield [[new Expression('user === subject')], 'post', 'withExpressionInSubject', 'Access Denied by #[IsGranted("user === subject", "args["post"].getAuthor()")] on controller'];
262262
}
263263

264264
public function testNotFoundHttpException()

0 commit comments

Comments
 (0)