引用分布式config.yml中的非参数条目 [英] Referencing non-parameter entries in distributed config.yml

查看:104
本文介绍了引用分布式config.yml中的非参数条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在config.yml中引用非参数条目的后续操作我昨天问的问题。



假设我们有一个更复杂的情况,每个实体都有一个.yml配置文件。在每个目录中,都有security.role_hierarchy设置,其中包含与该实体相关的角色的层次结构。像这样:

 #user.yml 
安全性:
role_hierarchy:& srh
ROLE_USER_SHOW:〜
ROLE_USER_LIST:〜
ROLE_USER_NEW:〜
ROLE_USER_EDIT_OWN:〜
ROLE_USER_EDIT:ROLE_USER_EDIT_OWN
ROLE_USER_SOFTDELETE_OWN:〜
ROLE_USERD_TEE :〜
ROLE_USER_FLAG:〜
ROLE_USER_ALL:
-ROLE_USER_SHOW
-ROLE_USER_LIST
-ROLE_USER_NEW
-ROLE_USER_EDIT
-ROLE_USER_DELETE
- ROLE_USER_SOFTDELETE
-ROLE_USER_FLAG

#group.yml
安全性:
role_hierarchy:& srh
ROLE_GROUP_SHOW:〜
ROLE_GROUP_LIST:〜
ROLE_GROUP_NEW:〜
ROLE_GROUP_EDIT:〜
ROLE_GROUP_DELETE:〜
ROLE_GROUP_ALL:
-ROLE_GROUP_S如何
-ROLE_GROUP_LIST
-ROLE_GROUP_NEW
-ROLE_GROUP_EDIT
-ROLE_GROUP_DELETE

easy_admin:
实体:
组:
形式:
字段:
-
属性:'roles'
类型:选择
type_options:
扩展:true
倍数:true
选项:* srh

但显示的解决方案仅链接选择 group.yml 中的 security.role_hierarchy ,因此它仅引用 ROLE_GROUP _ * 角色。
我希望提供选择,并提供 security.role_hierarchy 的合并值,以便它具有 ROLES_USER _ * ROLES_GROUP _ * 和所有其他定义的角色。



这可能吗?

解决方案

所以我设法自己完成了。
如@Anthon所说,& * 在这里没有用。我们必须通过Symfony做到这一点。方法如下:



我使用 security.role_hierarchy 作为定义安全性层次的可融合点-以及角色列表在应用程序中使用。我将选择字段留给未定义的选择,例如:

 -
属性:'roles'
type :选择
type_options:
扩展:true
倍数:true

然后我在控制器中使用一种方法来设置选择:

  // AppBundle / Controller / AdminController.php 
公共功能indexAction(Request $ request)
{
$ choices = []; $ preferred = [];
$ vals = array_keys($ this->容器-> getParameter(’security.role_hierarchy.roles'));
$ choices = $ vals;
//实际上有一些美化$ choices的方法,但并不是那么重要
//获取$ formBuilder和...
$ formBuilder-> add('roles',ChoiceType ::类,['choices'=> array_combine($ choices,$ vals),'multiple'=> true,'expanded'=> false]);
return $ formBuilder;
}

我放弃了 $ formBuilder 创建,因为我以适合EasyAdmin的方式进行了创建,因此在其他情况下,还有另一种实现方法。



太麻烦了:在控制器中获取 security.role_hierarchy ,根据需要进行处理,然后将其分配给Twig全局变量:

  // AppBundle / Controller / AdminController.php 
公共功能indexAction(Request $ request)
{
$ vals = array_keys($ this ->容器-> getParameter('security.role_hierarchy.roles'));
$ this->容器-> get(’twig’)-> addGlobal( _ security_roles,$ vals);
// ...
}

然后更新适当的Twig模板。 / p>

但我不喜欢不必要的全局变量,因此尚未对其进行广泛的测试。


This is follow-up of Referencing non-parameter entries in config.yml question I asked yesterday.

Suppose we have more complicated case where there is a .yml config file for each entity. In each there is security.role_hierarchy setting with a hierarchy of roles pertaining to that entity. Like so:

#user.yml
security:
    role_hierarchy: &srh
        ROLE_USER_SHOW: ~
        ROLE_USER_LIST: ~
        ROLE_USER_NEW: ~
        ROLE_USER_EDIT_OWN: ~
        ROLE_USER_EDIT: ROLE_USER_EDIT_OWN
        ROLE_USER_SOFTDELETE_OWN: ~
        ROLE_USER_SOFTDELETE: ROLE_USER_SOFTDELETE_OWN
        ROLE_USER_DELETE: ~
        ROLE_USER_FLAG: ~
        ROLE_USER_ALL:
            - ROLE_USER_SHOW
            - ROLE_USER_LIST
            - ROLE_USER_NEW
            - ROLE_USER_EDIT
            - ROLE_USER_DELETE
            - ROLE_USER_SOFTDELETE
            - ROLE_USER_FLAG

#group.yml
security:
    role_hierarchy: &srh
        ROLE_GROUP_SHOW: ~
        ROLE_GROUP_LIST: ~
        ROLE_GROUP_NEW: ~
        ROLE_GROUP_EDIT: ~
        ROLE_GROUP_DELETE: ~
        ROLE_GROUP_ALL:
            - ROLE_GROUP_SHOW
            - ROLE_GROUP_LIST
            - ROLE_GROUP_NEW
            - ROLE_GROUP_EDIT
            - ROLE_GROUP_DELETE

easy_admin:
    entities:
        Group:
            form:
                fields:
                    - 
                      property: 'roles' 
                      type: choice
                      type_options: 
                          expanded: true
                          multiple: true
                          choices: *srh

But presented solution only links choices to security.role_hierarchy in group.yml and so it references only ROLE_GROUP_* roles. I'd like choices to be supplied with merged value of security.role_hierarchy so that it had ROLES_USER_*, ROLES_GROUP_* and all the other defined roles.

Is this possible?

解决方案

So I managed to do it myself. As @Anthon said, & and * are of no use here. We must do it through Symfony. Here is how:

I use security.role_hierarchy as mergable point of defining security hierarchy - and a list of roles used in application. I leave choice field with undefined choices, like so:

            - 
              property: 'roles' 
              type: choice
              type_options: 
                  expanded: true
                  multiple: true

Then I use a method in controller to set up the choices:

// AppBundle/Controller/AdminController.php
public function indexAction(Request $request)
{
    $choices = [];$preferred = [];
    $vals = array_keys($this->container->getParameter('security.role_hierarchy.roles'));
    $choices = $vals;
    // actually there is some beautifying of $choices but it's not that important
    // get $formBuilder and...
    $formBuilder->add('roles', ChoiceType::class, ['choices'=>array_combine($choices, $vals), 'multiple'=>true, 'expanded'=>false]);
    return $formBuilder;
}

I left off $formBuilder creation because I did it in a way suitable for EasyAdmin so in other case there would be another ways to do it.

There could be another way to do it but it's messy: get the security.role_hierarchy in the controller, process it as you like and then assign it to Twig global variable:

// AppBundle/Controller/AdminController.php
public function indexAction(Request $request)
{
    $vals = array_keys($this->container->getParameter('security.role_hierarchy.roles'));
    $this->container->get('twig')->addGlobal("_security_roles", $vals);
    // ...
}

Then update appropriate Twig template.

But I don't like needless globals so haven't tested it extensively.

这篇关于引用分布式config.yml中的非参数条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆