You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #46978 [Security] Allow using expressions with the #[IsGranted] attribute (HypeMC)
This PR was merged into the 6.2 branch.
Discussion
----------
[Security] Allow using expressions with the #[IsGranted] attribute
| Q | A
| ------------- | ---
| Branch? | 6.2
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | Fix#46912
| License | MIT
| Doc PR | -
Allows using the expression language with the `#[IsGranted]` attribute:
```php
#[IsGranted(
attribute: new Expression('"ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)'),
subject: 'post',
)]
public function index(Post $post)
{
}
#[IsGranted(
attribute: new Expression('user === subject'),
subject: new Expression('args["post"].getAuthor()'),
)]
public function index(Post $post)
{
}
#[IsGranted(
attribute: new Expression('user === subject["author"] and subject["post"].isPublished()'),
subject: [
'author' => new Expression('args["post"].getAuthor()'),
'post' => 'post',
],
)]
public function index(Post $post)
{
}
```
Commits
-------
f5cee77 [Security] Allow using expressions with the #[IsGranted] attribute
@@ -42,21 +45,15 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event)
42
45
$arguments = $event->getNamedArguments();
43
46
44
47
foreach ($attributesas$attribute) {
45
-
$subjectRef = $attribute->subject;
46
48
$subject = null;
47
49
48
-
if ($subjectRef) {
50
+
if ($subjectRef = $attribute->subject) {
49
51
if (\is_array($subjectRef)) {
50
-
foreach ($subjectRefas$ref) {
51
-
if (!\array_key_exists($ref, $arguments)) {
52
-
thrownewRuntimeException(sprintf('Could not find the subject "%s" for the #[IsGranted] attribute. Try adding a "$%s" argument to your controller method.', $ref, $ref));
thrownewRuntimeException(sprintf('Could not find the subject "%s" for the #[IsGranted] attribute. Try adding a "$%s" argument to your controller method.', $subjectRef, $subjectRef));
if (!\array_key_exists($subjectRef, $arguments)) {
92
+
thrownewRuntimeException(sprintf('Could not find the subject "%s" for the #[IsGranted] attribute. Try adding a "$%s" argument to your controller method.', $subjectRef, $subjectRef));
@@ -247,6 +254,9 @@ public function getAccessDeniedMessageTests()
247
254
yield ['ROLE_ADMIN', null, 'admin', 0, 'Access Denied by #[IsGranted("ROLE_ADMIN")] on controller'];
248
255
yield ['ROLE_ADMIN', 'bar', 'withSubject', 2, 'Access Denied by #[IsGranted("ROLE_ADMIN", "arg2Name")] on controller'];
249
256
yield ['ROLE_ADMIN', ['arg1Name' => 'bar', 'arg2Name' => 'bar'], 'withSubjectArray', 2, 'Access Denied by #[IsGranted("ROLE_ADMIN", ["arg1Name", "arg2Name"])] on controller'];
257
+
yield [newExpression('"ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)'), 'bar', 'withExpressionInAttribute', 1, 'Access Denied by #[IsGranted(new Expression(""ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)"), "post")] on controller'];
258
+
yield [newExpression('user === subject'), 'bar', 'withExpressionInSubject', 1, 'Access Denied by #[IsGranted(new Expression("user === subject"), new Expression("args["post"].getAuthor()"))] on controller'];
259
+
yield [newExpression('user === subject["author"]'), ['author' => 'bar', 'alias' => 'bar'], 'withNestedExpressionInSubject', 2, 'Access Denied by #[IsGranted(new Expression("user === subject["author"]"), ["author" => new Expression("args["post"].getAuthor()"), "alias" => "arg2Name"])] on controller'];
250
260
}
251
261
252
262
publicfunctiontestNotFoundHttpException()
@@ -270,4 +280,80 @@ public function testNotFoundHttpException()
0 commit comments