0
votes

Under security.yml I added new role called ROLE_PUBLISHER

access_control:
    - { path: ^/publisher/,           roles: [ ROLE_PUBLISHER ] }

role_hierarchy:
        ROLE_TC_ADMIN:                  [ ROLE_ALLOWED_TO_SWITCH ]
        ROLE_PUBLISHER:                 [ ROLE_PUBLISHER_UNCONFIRMED ]

Role works fine, it is used on production env, but I just noticed that I cannot check in twig if logged user has correct role.

This one works fine, I am getting 'user' word

{% if is_granted('ROLE_USER') %}'user'{% else %}''{% endif %}

This one does not work (always empty string), even I am logged in as publisher

{% if is_granted('ROLE_PUBLISHER') %}'publisher'{% else %}''{% endif %}

Is it possible that user has two roles ROLE_USER and ROLE_PUBLISHER and twig always checking the first one?

1

1 Answers

0
votes

Yep I had right, user has two roles, and twig has been checking the first one. So the solution is:

{% set user_role = '' %}
{% for user in app.user.roles %}
    {% if user == 'ROLE_PUBLISHER' %}
        {% set user_role = 'publisher' %}
    {% endif %}
{% endfor %}