How can I check other users or role permissions in the template? symfony2

后端 未结 2 662
星月不相逢
星月不相逢 2021-02-02 03:04

I\'m building this user manager, where admins can change permission of a group or user. I don\'t want to use the FOS user bundle, because I want to customize alot.

I fou

2条回答
  •  星月不相逢
    2021-02-02 03:29

    You can check if the current user has a role by twig by using the function is_granted

    {% if is_granted('ROLE_USER') %}
      {{ app.user.username }}
    {% endif %}
    

    Getting the current users roles array in twig:

    {{ app.user.roles }}
    

    If you are wanting to display from a collection of users, you can do something like this (assuming collection passed as users)

    {% for user in users %}
      

    {{ user.username }}: {% for role in user.roles %} {{ role }} {% endfor %}

    {% endfor %}

提交回复
热议问题