简体   繁体   English

Symfony2 - 显示动态可选角色

[英]Symfony2 - Showing dynamic selectable roles

I'm using Symfony2 with Sonata. 我正在使用Symfony2和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 John应该有权访问section1和section3
  • Bob should have access to section2 and section3 Bob应该可以访问section2和section3

So I created 3 roles, that I can attach to users/groups (through sonata) 所以我创建了3个角色,我可以附加到用户/组(通过奏鸣曲)

  • ROLE_SECTION1 ROLE_SECTION1
  • ROLE_SECTION2 ROLE_SECTION2
  • ROLE_SECTION3 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 ) (无需每次都在我的security.yml声明它们)

I saw sonata-admin-user does that when using the role security handler, 我看到sonata-admin-user在使用角色安全处理程序时这样做了
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 ? 如何在Sonata用户管理包编辑器列表中添加一些动态可选角色?

First you can load user and groups from your database. 首先,您可以从数据库加载用户和组。 See Symfony2 cookbook Load user from database . 请参阅Symfony2 cookbook从数据库加载用户 Then in the controller of every section: 然后在每个部分的控制器中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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