Skip to content
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