Skip to content

Commit 0c6e3ea

Browse 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

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

security/expressions.rst

+35
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)