Skip to content

[Security] add methods argument to #[IsGranted] to restrict access by HTTP method #21301

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2523,6 +2523,29 @@ that is thrown with the ``exceptionCode`` argument::
// ...
}

You can restrict access validation to specific HTTP methods
by using the ``methods`` argument::

// src/Controller/AdminController.php
// ...

use Symfony\Component\Security\Http\Attribute\IsGranted;

#[IsGranted('ROLE_ADMIN', methods: 'POST')]
class AdminController extends AbstractController
{
// You can also specify an array of methods
#[IsGranted('ROLE_SUPER_ADMIN', methods: ['GET', 'PUT'])]
public function adminDashboard(): Response
{
// ...
}
}

.. versionadded:: 7.4

The ``methods`` argument was introduced in Symfony 7.4.

.. _security-template:

Access Control in Templates
Expand Down