Skip to content

Commit f9442fd

Browse files
committed
minor #18103 [Security] Add statusCode and message arguments to #[IsGranted] (alexandre-daubois)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [Security] Add `statusCode` and `message` arguments to `#[IsGranted]` Following #18101 (comment) Commits ------- 149d33b [Security] Add `statusCode` and `message` arguments to `#[IsGranted]`
2 parents 91bbff3 + 149d33b commit f9442fd

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

security.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2299,13 +2299,30 @@ the ``ROLE_SUPER_ADMIN`` permission:
22992299
#[IsGranted('ROLE_ADMIN')]
23002300
class AdminController extends AbstractController
23012301
{
2302-
#[IsGranted('ROLE_SUPER_ADMIN')]
2302+
// Optionally, you can set a custom message that will be displayed to the user
2303+
#[IsGranted('ROLE_SUPER_ADMIN', message: 'You are not allowed to access the admin dashboard.')]
23032304
public function adminDashboard(): Response
23042305
{
23052306
// ...
23062307
}
23072308
}
23082309
2310+
If you want to use a custom status code instead of the default one (which
2311+
is 403), this can be done by setting with the ``statusCode`` argument::
2312+
2313+
// src/Controller/AdminController.php
2314+
// ...
2315+
2316+
use Symfony\Component\Security\Http\Attribute\IsGranted;
2317+
2318+
#[IsGranted('ROLE_ADMIN', statusCode: 423)]
2319+
class AdminController extends AbstractController
2320+
{
2321+
// ...
2322+
}
2323+
2324+
The default code is 403.
2325+
23092326
.. versionadded:: 6.2
23102327

23112328
The ``#[IsGranted()]`` attribute was introduced in Symfony 6.2.

0 commit comments

Comments
 (0)