@@ -816,29 +816,32 @@ to users that have a specific role.
816
816
Securing Controllers and other Code
817
817
...................................
818
818
819
- You can easily deny access from inside a controller::
819
+ You can easily deny access from inside a controller:
820
+
821
+ .. versionadded :: 2.6
822
+ The ``denyAccessUnlessGranted() `` method was introduced in Symfony 2.6. Previously (and
823
+ still now), you could check access directly and throw the ``AccessDeniedException `` as shown
824
+ in the example below).
825
+
826
+ .. code-block :: php
820
827
821
828
// ...
822
829
823
830
public function helloAction($name)
824
831
{
825
832
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
826
833
834
+ // Old way :
835
+ // if (false === $this->isGranted('ROLE_ADMIN')) {
836
+ // throw $this->createAccessDeniedException('Unable to access this page!');
837
+ // }
838
+
827
839
// ...
828
840
}
829
841
830
- .. versionadded :: 2.5
831
- The ``createAccessDeniedException `` method was introduced in Symfony 2.5.
832
-
833
- The :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::createAccessDeniedException `
834
- method creates a special :class: `Symfony\\ Component\\ Security\\ Core\\ Exception\\ AccessDeniedException `
835
- object, which ultimately triggers a 403 HTTP response inside Symfony.
836
-
837
- .. versionadded :: 2.6
838
- You can use directly `$this->isGranted($role) ` instead of
839
- `$this->get('security.context')->isGranted($role) ` to check if
840
- a role is granted and `denyAccessUnlessGranted ` to throw an exception
841
- if the access is not granted (like in the example above).
842
+ In both cases, a special
843
+ :class: `Symfony\\ Component\\ Security\\ Core\\ Exception\\ AccessDeniedException `
844
+ is thrown, which ultimately triggers a 403 HTTP response inside Symfony.
842
845
843
846
That's it! If the user isn't logged in yet, they will be asked to login (e.g.
844
847
redirected to the login page). If they *are * logged in, they'll be shown
0 commit comments