Search code examples
symfonysymfony4easyadmin

EasyAdminBundle - List of users by role and group


I am trying to use EasyAdminBundle (https://github.com/EasyCorp/EasyAdminBundle) to manage the admin part of my project.

I am currently doing the list of users, it works very well. I get the list of all existing users

However, users are all assigned in groups and in these groups there are "supervisors". What I would like to do is that these "ROLE_SUPERVISOR", is access to the users administration page but that they can only see the users of the same group.

To summarize what I want: - Me (ROLE_SUPER_ADMIN): I want to see all existing users - ROLE_SUPERVISOR: See all users in the same group as them.

Is this possible?

My EasyAdminBundle configuration file :

easy_admin:
  site_name: '<img height="65px" src="../img/logo.png" />'
  user:
    display_name: true
    name_property_path: 'username'
    display_avatar: false
  design:
    brand_color: '#7C1AE8'
    menu:
      - { entity: 'User', css_class: 'menu--user', icon: 'user', role: [ROLE_SUPERVISEUR, ROLE_SUPER_ADMIN] }
  entities:
    User:
      class: App\Entity\User
      label: 'Utilisateurs'
      list:
        title: "Liste des utilisateurs"
        fields:
          - email
          - { property: 'username', label: "Nom d'utilisateur" }
          - { property: 'group.name', label: "Groupe" }
          - { property: 'lastLogin', label: "Dernière connexion", format: 'd/m/Y H:i:s', type: 'datetime' }
        sort: 'group.name'

Solution

  • There is item_permission for that:

    easy_admin:
        ...
        entities:
            Product:
                list:
                    # set this option to an empty string or array to unset the global permission for this entity
                    item_permission: ''
            Employees:
                list:
                    # this completely overrides the global option (both options are not merged)
                    item_permission: ['ROLE_SUPER_ADMIN', 'ROLE_HUMAN_RESOURCES']
    

    see documentation https://symfony.com/doc/master/bundles/EasyAdminBundle/book/list-search-show-configuration.html#security-and-permissions