简体   繁体   中英

Symfony2 - Showing dynamic selectable roles

I'm using Symfony2 with Sonata.
I have a list of sections on my application to which I'd like to give access to my users.

Example:

  • John should have access to section1 and section3
  • Bob should have access to section2 and section3

So I created 3 roles, that I can attach to users/groups (through sonata)

  • ROLE_SECTION1
  • ROLE_SECTION2
  • ROLE_SECTION3

But as I create new sections, I'd like to have the related roles automatically listed in the admin
(without having to declare them each time in my security.yml )

I saw sonata-admin-user does that when using the role security handler,
but I don't know how to do the same.

Finally the question is simple and is the following:

How can I add some dynamic selectable roles into my Sonata user admin bundle editor list ?

First you can load user and groups from your database. See Symfony2 cookbook Load user from database . Then in the controller of every section:

if ($this->securityContext->isGranted('Role_section1') === false) {
    echo 'forbidden';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM