Skip to content

[Security] Explain how to use controller argument(s) in IsGranted attribute #17982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions security/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ Additionally, you have access to a number of functions inside the expression:
true if the user has actually logged in during this session (i.e. is
full-fledged).

If you want to use a controller argument in the expression, you need to pass it
as ``subject``::

#[Route(path: '/{slug}')]
#[IsGranted(new Expression('"Improvement" == subject.getName()'), subject: 'category')]
public function categoryShow(
#[MapEntity()]
NewsCategory $category,
) {

And if you have many arguments::

#[Route(path: '/{slug}/{slug2}')]
#[IsGranted(new Expression('"Improvement" == subject["category"].getName() and "bar" == subject["foobar"].getTitle()'), subject: ['category', 'foobar'])]
public function categoryShow(
#[MapEntity(expr: 'repository.findOneBy({"slug": slug})')]
NewsCategory $category,
#[MapEntity(expr: 'repository.findOneBy({"slug": slug2})')]
Foobar $foobar,
) {

Learn more
----------

Expand Down