@@ -23,6 +23,7 @@ and ``#[IsGranted()]`` attribute also accept an
2323 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2424 use Symfony\Component\ExpressionLanguage\Expression;
2525 use Symfony\Component\HttpFoundation\Response;
26+ use Symfony\Component\Security\Http\Attribute\IsGranted;
2627
2728 class MyController extends AbstractController
2829 {
@@ -144,6 +145,69 @@ Additionally, you have access to a number of functions inside the expression:
144145 true if the user has actually logged in during this session (i.e. is
145146 full-fledged).
146147
148+ In case of the ``#[IsGranted()] `` attribute, the subject can also be an
149+ :class: `Symfony\\ Component\\ ExpressionLanguage\\ Expression ` object::
150+
151+ // src/Controller/MyController.php
152+ namespace App\Controller;
153+
154+ use App\Entity\Post;
155+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
156+ use Symfony\Component\ExpressionLanguage\Expression;
157+ use Symfony\Component\HttpFoundation\Response;
158+ use Symfony\Component\Security\Http\Attribute\IsGranted;
159+
160+ class MyController extends AbstractController
161+ {
162+ #[IsGranted(
163+ attribute: new Expression('user === subject'),
164+ subject: new Expression('args["post"].getAuthor()'),
165+ )]
166+ public function index(Post $post): Response
167+ {
168+ // ...
169+ }
170+ }
171+
172+ In this example, we fetch the author of the post and use it as the subject. If the subject matches
173+ the current user, then access will be granted.
174+
175+ The subject may also be an array where the key can be used as an alias for the result of an expression::
176+
177+ #[IsGranted(
178+ attribute: new Expression('user === subject["author"] and subject["post"].isPublished()'),
179+ subject: [
180+ 'author' => new Expression('args["post"].getAuthor()'),
181+ 'post',
182+ ],
183+ )]
184+ public function index(Post $post): Response
185+ {
186+ // ...
187+ }
188+
189+ Here, access will be granted if the author matches the current user
190+ and the post's ``isPublished() `` method returns ``true ``.
191+
192+ You can also use the current request as the subject::
193+
194+ #[IsGranted(
195+ attribute: '...',
196+ subject: new Expression('request'),
197+ )]
198+ public function index(): Response
199+ {
200+ // ...
201+ }
202+
203+ Inside the subject's expression, you have access to two variables:
204+
205+ ``request ``
206+ The :ref: `Symfony Request <component-http-foundation-request >` object that
207+ represents the current request.
208+ ``args ``
209+ An array of controller arguments that are passed to the controller.
210+
147211Learn more
148212----------
149213
0 commit comments