Get ROLE of a user not logged in TWIG Symfony2

后端 未结 4 1000
陌清茗
陌清茗 2020-12-14 19:28

I would like to know how can i know if a user is granted when it\'s not the current user in twig.

I use this code for the current user:

{% if is_gran         


        
4条回答
  •  -上瘾入骨i
    2020-12-14 20:15

    i did it this way, have this snippet in the global twig file, in my case layout.html.twig

    {% set is_admin = false %}
    {% if app.security.token.user.roles is iterable %}
        {% for role in app.security.token.user.roles %}
            {% if role == 'ROLE_ADMIN' or role == 'ROLE_SUPER_ADMIN'  %}
                {% set is_admin = true %}
            {% endif %}
        {% endfor %}
    {% endif %}
    

    then i can use anywhere

    {% if is_admin %}....{% endif %}
    

提交回复
热议问题