Skip to content

Commit 7f9bc8c

Browse files
Cydonia7wouterj
authored andcommitted
And now the same for isGranted where it is possible
1 parent 46e2505 commit 7f9bc8c

File tree

3 files changed

+7
-32
lines changed

3 files changed

+7
-32
lines changed

book/security.rst

+5-16
Original file line numberDiff line numberDiff line change
@@ -816,32 +816,17 @@ to users that have a specific role.
816816
Securing Controllers and other Code
817817
...................................
818818

819-
Securing a Controller
820-
~~~~~~~~~~~~~~~~~~~~~
821-
822819
You can easily deny access from inside a controller::
823820

824821
// ...
825822

826823
public function helloAction($name)
827824
{
828-
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
829-
throw $this->createAccessDeniedException('Unable to access this page!');
830-
}
825+
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
831826

832827
// ...
833828
}
834829

835-
.. versionadded:: 2.6
836-
The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior
837-
to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service.
838-
839-
.. versionadded:: 2.6
840-
You can use directly :method:`Symfony\\Bundle\\FrameworkBundle\\Controller::isGranted`
841-
instead of `$this->get('security.context')->isGranted($role)` to check if
842-
a role is granted and :method:`Symfony\\Bundle\\FrameworkBundle\\Controller::denyAccessUnlessGranted`
843-
to throw an exception if the access is not granted (like in the example above).
844-
845830
.. versionadded:: 2.5
846831
The ``createAccessDeniedException`` method was introduced in Symfony 2.5.
847832

@@ -872,6 +857,10 @@ using annotations::
872857
*/
873858
public function helloAction($name)
874859
{
860+
$this->denyAccessUnlessGranted(new Expression(
861+
'"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())'
862+
));
863+
875864
// ...
876865
}
877866

cookbook/security/remember_me.rst

+1-9
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,11 @@ In the following example, the action is only allowed if the user has the
162162
163163
public function editAction()
164164
{
165-
if (false === $this->get('security.authorization_checker')->isGranted(
166-
'IS_AUTHENTICATED_FULLY'
167-
)) {
168-
throw new AccessDeniedException();
169-
}
165+
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
170166
171167
// ...
172168
}
173169
174-
.. versionadded:: 2.6
175-
The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior
176-
to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service.
177-
178170
If your application is based on the Symfony Standard Edition, you can also secure
179171
your controller using annotations:
180172

cookbook/security/securing_services.rst

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,11 @@ and checking the current user's role::
1414

1515
public function helloAction($name)
1616
{
17-
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
18-
throw new AccessDeniedException();
19-
}
17+
$this->denyAccessUnlessGranted('ROLE_ADMIN');
2018

2119
// ...
2220
}
2321

24-
.. versionadded:: 2.6
25-
The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior
26-
to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service.
27-
2822
You can also secure *any* service in a similar way by injecting the ``security.authorization_checker``
2923
service into it. For a general introduction to injecting dependencies into
3024
services see the :doc:`/book/service_container` chapter of the book. For

0 commit comments

Comments
 (0)