Skip to content

Commit e03c13c

Browse files
committed
minor #21838 [Security] Document this variable in #[IsGranted] subject expression (lacatoire)
This PR was merged into the 8.1 branch. Discussion ---------- [Security] Document `this` variable in `#[IsGranted]` subject expression This PR documents the new `this` variable available in `#[IsGranted]` subject expressions, introduced in Symfony 8.1. This allows accessing the controller/component instance directly, which is particularly useful with Live Components. Fixes #21836 Commits ------- 7690891 [Security] Document `this` variable in #[IsGranted] subject expression
2 parents 66972a3 + 7690891 commit e03c13c

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

security/expressions.rst

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,37 @@ You can also use the current request as the subject::
193193
// ...
194194
}
195195

196-
Inside the subject's expression, you have access to two variables:
196+
Inside the subject's expression, you have access to these variables:
197197

198198
``request``
199199
The :ref:`Symfony Request <component-http-foundation-request>` object that
200200
represents the current request.
201201
``args``
202202
An array of controller arguments that are passed to the controller.
203+
``this``
204+
The controller instance itself, allowing access to its properties and methods.
205+
206+
.. versionadded:: 8.1
207+
208+
The ``this`` variable was introduced in Symfony 8.1.
209+
210+
This is useful when you need to access controller properties in the subject
211+
expression::
212+
213+
use Symfony\Component\ExpressionLanguage\Expression;
214+
use Symfony\Component\HttpFoundation\Response;
215+
use Symfony\Component\Security\Http\Attribute\IsGranted;
216+
217+
class PostController
218+
{
219+
public string $defaultCategory = 'blog';
220+
221+
#[IsGranted('CATEGORY_ACCESS', subject: new Expression('this.defaultCategory'))]
222+
public function index(): Response
223+
{
224+
// ...
225+
}
226+
}
203227

204228
Additionally to expressions, the ``#[IsGranted]`` attribute also accepts
205229
closures that return a boolean value. The subject can also be a closure that

0 commit comments

Comments
 (0)