6

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

Enessari
  • 125
  • 1
  • 5

2 Answers2

6

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.

TautrimasPajarskas
  • 2,686
  • 1
  • 32
  • 40
  • 2
    Yeah you have to set `sonata_admin: security: handler: sonata.admin.security.handler.role` in your config. Then you'll be able to set the required roles to access the different parts of admin. – Pierre de LESPINAY Jul 11 '13 at 11:39
  • Thanks a lot for your answers, a little bit late but everything is fine for me now :) – Enessari Jul 30 '13 at 10:32
2

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.

webDEVILopers
  • 1,886
  • 1
  • 21
  • 35