@@ -802,33 +802,18 @@ the ``^``) would match ``/admin/foo`` but would also match URLs like ``/foo/admi
802
802
803
803
To learn about all of this, see :doc: `/cookbook/security/access_control `.
804
804
805
- Securing other Services
806
- ~~~~~~~~~~~~~~~~~~~~~~~
807
-
808
- In fact, anything in Symfony can be protected using a strategy similar to
809
- the one seen in the previous section. For example, suppose you have a service
810
- (i.e. a PHP class) whose job is to send emails from one user to another.
811
- You can restrict use of this class - no matter where it's being used from -
812
- to users that have a specific role.
813
-
814
805
.. _`book-security-securing-controller` :
815
806
816
807
Securing Controllers and other Code
817
808
...................................
818
809
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
810
+ You can easily deny access from inside a controller::
827
811
828
812
// ...
829
813
830
814
public function helloAction($name)
831
815
{
816
+ // The second parameter is used to specify on what object the role is tested.
832
817
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
833
818
834
819
// Old way :
@@ -839,6 +824,11 @@ You can easily deny access from inside a controller:
839
824
// ...
840
825
}
841
826
827
+ .. versionadded :: 2.6
828
+ The ``denyAccessUnlessGranted() `` method was introduced in Symfony 2.6. Previously (and
829
+ still now), you could check access directly and throw the ``AccessDeniedException `` as shown
830
+ in the example above).
831
+
842
832
In both cases, a special
843
833
:class: `Symfony\\ Component\\ Security\\ Core\\ Exception\\ AccessDeniedException `
844
834
is thrown, which ultimately triggers a 403 HTTP response inside Symfony.
@@ -860,10 +850,6 @@ using annotations::
860
850
*/
861
851
public function helloAction($name)
862
852
{
863
- $this->denyAccessUnlessGranted(new Expression(
864
- '"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())'
865
- ));
866
-
867
853
// ...
868
854
}
869
855
0 commit comments