Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 roles/groups - is_granted is not detecting roles that the user has
    text
    copied!<p>I have followed the 'How to load Security Users from the Database (the Entity Provider)' recipe from the Symfony2 cook book (http://symfony.com/doc/current/cookbook/security/entity_provider.html), except I am not using the Custom Entity Provider - which means my User class is using lazy loading for the roles.</p> <p>The firewall/access control in the security all works fine. I have some routes that only ROLE_ADMIN users can access and some that ROLE_USER users can access - these work fine.</p> <p>The problem is that in my base template, I have a bar that is displayed like:</p> <pre><code>&lt;p&gt;Logged in as: {{ app.user.username }} {% if is_granted('ROLE_ADMIN') %}| &lt;a href="{{ path('bassettprovidentia_skeleton_admindashboard') }}"&gt;Admin area&lt;/a&gt; {% endif %}| &lt;a href="#"&gt;Settings&lt;/a&gt; | &lt;a href="{{ path('bassettprovidentia_skeleton_logout') }}"&gt;Log out&lt;/a&gt;&lt;/p&gt; </code></pre> <p>Even though my user has the ROLE_ADMIN role in the database (and can access URLs that are restricted to that role), the "Admin area" link is not being displayed!</p> <p>In the same form, I have this:</p> <pre><code>&lt;p&gt;Roles: {% for role in app.user.roles %}{{ role.name }} [{{ role.role }}]{% if not loop.last %}, {% endif %}{% endfor %}&lt;/p&gt; </code></pre> <p>It works fine! All of the roles which the user has are displayed!</p> <p>What am I doing wrong?</p> <p>Is the lazy-loading to blame?</p> <p>It isn't causing problem's elsewhere.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload