Skip to content

Commit 7961840

Browse files
[Security] Add statusCode and message arguments to #[IsGranted]
1 parent 9f38a74 commit 7961840

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

security.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2299,13 +2299,27 @@ the ``ROLE_SUPER_ADMIN`` permission:
22992299
#[IsGranted('ROLE_ADMIN')]
23002300
class AdminController extends AbstractController
23012301
{
2302-
#[IsGranted('ROLE_SUPER_ADMIN')]
2302+
#[IsGranted('ROLE_SUPER_ADMIN', message: 'You are not allowed to access the admin dashboard.')]
23032303
public function adminDashboard(): Response
23042304
{
23052305
// ...
23062306
}
23072307
}
23082308
2309+
A custom status code can be set with the ``statusCode`` argument. This
2310+
code will be used in the HTTP response::
2311+
2312+
// src/Controller/AdminController.php
2313+
// ...
2314+
2315+
use Symfony\Component\Security\Http\Attribute\IsGranted;
2316+
2317+
#[IsGranted('ROLE_ADMIN', statusCode: 403)]
2318+
class AdminController extends AbstractController
2319+
{
2320+
// ...
2321+
}
2322+
23092323
.. versionadded:: 6.2
23102324

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

0 commit comments

Comments
 (0)