Skip to content

[Security] Add support for closures in the IsGranted attribute #20669

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

Merged
merged 1 commit into from
Mar 11, 2025
Merged
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
35 changes: 35 additions & 0 deletions security/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,41 @@
``args``
An array of controller arguments that are passed to the controller.

Additionally to expressions, the ``#[IsGranted]`` attribute also accepts
closures that return a boolean value. The subject can also be a closure that
returns an array of values that will be injected into the closure::

// src/Controller/MyController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Security\Http\Attribute\IsGrantedContext;

Check failure on line 214 in security/expressions.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Security\Http\Attribute\IsGrantedContext" does not exist

class MyController extends AbstractController
{
#[IsGranted(static function (
IsGrantedContext $context,
mixed $subject,
) {
return $context->user === $subject['post']->getAuthor();
}, subject: static function (array $args) {
return [
'post' => $args['post'],
];
})]
public function index($post): Response
{
// ...
}
}

.. versionadded:: 7.3

The support for closures in the ``#[IsGranted]`` attribute was introduced
in Symfony 7.3 and requires PHP 8.5.

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

Expand Down
Loading