引用分布式 config.yml 中的非参数条目
Referencing non-parameter entries in distributed config.yml
这是我昨天问的 问题的后续。
假设我们有更复杂的情况,每个实体都有一个 .yml 配置文件。每个都有 security.role_hierarchy 设置,其中包含与该实体相关的角色层次结构。像这样:
#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
但提供的解决方案仅将 choices
链接到 group.yml
中的 security.role_hierarchy
,因此它仅引用 ROLE_GROUP_*
角色。
我希望为 choices
提供 security.role_hierarchy
的合并值,以便它具有 ROLES_USER_*
、ROLES_GROUP_*
和所有其他定义的角色。
这可能吗?
所以我设法自己做了。
正如@Anthon 所说, &
和 *
在这里没有用。我们必须通过 Symfony 来完成。方法如下:
我使用 security.role_hierarchy
作为定义安全层次结构的可合并点 - 以及应用程序中使用的角色列表。我将选择字段保留为未定义的选项,如下所示:
-
property: 'roles'
type: choice
type_options:
expanded: true
multiple: true
然后我在控制器中使用一个方法来设置选项:
// 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;
}
我停止了 $formBuilder
创作,因为我以适合 EasyAdmin 的方式进行创作,所以在其他情况下会有其他方式来创作。
可能还有另一种方法,但很麻烦:在控制器中获取 security.role_hierarchy
,根据需要进行处理,然后将其分配给 Twig 全局变量:
// 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);
// ...
}
然后更新适当的 Twig 模板。
但我不喜欢不必要的全局变量,所以没有对其进行广泛测试。
这是我昨天问的
假设我们有更复杂的情况,每个实体都有一个 .yml 配置文件。每个都有 security.role_hierarchy 设置,其中包含与该实体相关的角色层次结构。像这样:
#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
但提供的解决方案仅将 choices
链接到 group.yml
中的 security.role_hierarchy
,因此它仅引用 ROLE_GROUP_*
角色。
我希望为 choices
提供 security.role_hierarchy
的合并值,以便它具有 ROLES_USER_*
、ROLES_GROUP_*
和所有其他定义的角色。
这可能吗?
所以我设法自己做了。
正如@Anthon 所说, &
和 *
在这里没有用。我们必须通过 Symfony 来完成。方法如下:
我使用 security.role_hierarchy
作为定义安全层次结构的可合并点 - 以及应用程序中使用的角色列表。我将选择字段保留为未定义的选项,如下所示:
-
property: 'roles'
type: choice
type_options:
expanded: true
multiple: true
然后我在控制器中使用一个方法来设置选项:
// 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;
}
我停止了 $formBuilder
创作,因为我以适合 EasyAdmin 的方式进行创作,所以在其他情况下会有其他方式来创作。
可能还有另一种方法,但很麻烦:在控制器中获取 security.role_hierarchy
,根据需要进行处理,然后将其分配给 Twig 全局变量:
// 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);
// ...
}
然后更新适当的 Twig 模板。
但我不喜欢不必要的全局变量,所以没有对其进行广泛测试。