Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display items on dashboard according to user role

I'm new to Symfony2, and for the needs of my company I'm using Sonata Admin and User bundles.

I would like to know if it is possible to hide or show items on the dashboard according to the role of a logged user.

For example, I have several entities which are managed by Sonata AdminBundle (user, company, mission, site, etc ... management) and I want to display the user management only to the super Admin, company and site management to another role (SITE_ADMIN for example) and missions to a third role.

Have you ever faced this situation ? Could you give me the solution or clues to go directly to the solution ?

Many thanks,

Enes

like image 826
Enessari Avatar asked Jul 08 '13 09:07

Enessari


2 Answers

Use Role handler and enable User related rights only to, for example, Admins group (create it). Then assign admin users to Admins group. Sonata Admin will pick up restrictions and will only display dashboard items to Admins.

like image 148
TautrimasPajarskas Avatar answered Sep 28 '22 00:09

TautrimasPajarskas


In addition you can hide and display blocks in the userboard this way:

# app/config/config.yml
sonata_admin:
dashboard:
    groups:
        acme.admin.group.content:
            label: acme.admin.group.content
            label_catalogue: AcmeDemoBundle
            items:
                - sonata.admin.post
        acme.admin.group.blog:
            items: ~
            item_adds:
                - sonata.admin.page
            roles: [ ROLE_ONE, ROLE_TWO ]
        acme.admin.group.misc: ~

As mentioned in the docs.

Please note that this only changes the visibility of a block, not the accessability. If there are no restrictions people could still open the link in the browser if they know it.

If you use the previous answer implementing the sonata.admin.security.handler.role the block should be automatically hidden if the user does not have the correct role.

like image 33
webDEVILopers Avatar answered Sep 28 '22 00:09

webDEVILopers