Mirage
Mirage

Reputation: 31548

How to access the role of user in un-authenticated page in symfony2

In my home page template Index.html.twig I have one navigation bar which i only want to show to Admin users if they are logged in , othwise not.

I tried this

{% if is_granted('ROLE_ADMIN') %}
       <div class="navigation">
       </div>
{% endif %}

But i get this error

An exception has been thrown during the rendering of a template ("The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.

Now as this is home page i can't put it behind firewall. Is there any way to do it

Upvotes: 2

Views: 139

Answers (1)

Lusitanian
Lusitanian

Reputation: 11122

Wrap in it an if block like so:

{% if app.user is not null %}
    {% if is_granted('ROLE_ADMIN') %}
        {# your code #}
    {% endif %}
{% endif %}

Upvotes: 2

Related Questions