Skip to content

Commit 1361715

Browse files
committed
Removed the comments about the is_granted() issues in non-secure pages
1 parent 0061abe commit 1361715

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

Diff for: book/security.rst

+7-9
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ Access Control in Templates
884884
...........................
885885

886886
If you want to check if the current user has a role inside a template, use
887-
the built-in helper function:
887+
the built-in ``is_granted()`` helper function:
888888

889889
.. configuration-block::
890890

@@ -900,20 +900,18 @@ the built-in helper function:
900900
<a href="...">Delete</a>
901901
<?php endif ?>
902902

903-
If you use this function and you are *not* behind a firewall, an exception will
904-
be thrown. Again, it's almost always a good idea to have a main firewall that
905-
covers all URLs (as shown before in this chapter).
906-
907-
.. caution::
903+
.. note::
908904

909-
Be careful with this in your base layout or on your error pages! Because of
910-
some internal Symfony details, to avoid broken error pages in the ``prod``
911-
environment, wrap calls in these templates with a check for ``app.user``:
905+
In Symfony versions previous to 2.8, using the ``is_granted()`` function
906+
in a page that wasn't behind a firewall resulted in an exception. That's why
907+
you also needed to check first for the existence of the user:
912908

913909
.. code-block:: html+twig
914910

915911
{% if app.user and is_granted('ROLE_ADMIN') %}
916912

913+
Starting from Symfony 2.8, the ``app.user and ...`` check is no longer needed.
914+
917915
Securing other Services
918916
.......................
919917

Diff for: cookbook/controller/error_pages.rst

+1-19
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ To override the 404 error template for HTML pages, create a new
9696
<h1>Page not found</h1>
9797

9898
{# example security usage, see below #}
99-
{% if app.user and is_granted('IS_AUTHENTICATED_FULLY') %}
99+
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
100100
{# ... #}
101101
{% endif %}
102102

@@ -124,24 +124,6 @@ store the HTTP status code and message respectively.
124124
for the standard HTML exception page or ``exception.json.twig`` for the JSON
125125
exception page.
126126

127-
Avoiding Exceptions when Using Security Functions in Error Templates
128-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129-
130-
One of the common pitfalls when designing custom error pages is to use the
131-
``is_granted()`` function in the error template (or in any parent template
132-
inherited by the error template). If you do that, you'll see an exception thrown
133-
by Symfony.
134-
135-
The cause of this problem is that routing is done before security. If a 404 error
136-
occurs, the security layer isn't loaded and thus, the ``is_granted()`` function
137-
is undefined. The solution is to add the following check before using this function:
138-
139-
.. code-block:: twig
140-
141-
{% if app.user and is_granted('...') %}
142-
{# ... #}
143-
{% endif %}
144-
145127
.. _testing-error-pages:
146128

147129
Testing Error Pages during Development

0 commit comments

Comments
 (0)