Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit4ddcc08

Browse files
[Security] Add support for closures in theIsGranted attribute
1 parent8b5773b commit4ddcc08

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎security/expressions.rst

Lines 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)

[8]ページ先頭

©2009-2025 Movatter.jp