Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0c6e3ea

Browse filesBrowse files
committed
minor #20669 [Security] Add support for closures in the IsGranted attribute (alexandre-daubois)
This PR was merged into the 7.3 branch. Discussion ---------- [Security] Add support for closures in the `IsGranted` attribute Fixes #20663 #20679 Commits ------- 4ddcc08 [Security] Add support for closures in the `IsGranted` attribute
2 parents 371c4d4 + 4ddcc08 commit 0c6e3ea
Copy full SHA for 0c6e3ea

File tree

Expand file treeCollapse file tree

1 file changed

+35
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+35
-0
lines changed

‎security/expressions.rst

Copy file name to clipboardExpand all lines: security/expressions.rst
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,41 @@ Inside the subject's expression, you have access to two variables:
201201
``args``
202202
An array of controller arguments that are passed to the controller.
203203

204+
Additionally to expressions, the ``#[IsGranted]`` attribute also accepts
205+
closures that return a boolean value. The subject can also be a closure that
206+
returns an array of values that will be injected into the closure::
207+
208+
// src/Controller/MyController.php
209+
namespace App\Controller;
210+
211+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
212+
use Symfony\Component\HttpFoundation\Response;
213+
use Symfony\Component\Security\Http\Attribute\IsGranted;
214+
use Symfony\Component\Security\Http\Attribute\IsGrantedContext;
215+
216+
class MyController extends AbstractController
217+
{
218+
#[IsGranted(static function (
219+
IsGrantedContext $context,
220+
mixed $subject,
221+
) {
222+
return $context->user === $subject['post']->getAuthor();
223+
}, subject: static function (array $args) {
224+
return [
225+
'post' => $args['post'],
226+
];
227+
})]
228+
public function index($post): Response
229+
{
230+
// ...
231+
}
232+
}
233+
234+
.. versionadded:: 7.3
235+
236+
The support for closures in the ``#[IsGranted]`` attribute was introduced
237+
in Symfony 7.3 and requires PHP 8.5.
238+
204239
Learn more
205240
----------
206241

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.