在sonata admin的编辑表格中为用户设置角色[英] set role for users in edit form of sonata admin

本文是小编为大家收集整理的关于在sonata admin的编辑表格中为用户设置角色的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用Symfony 2.1进行项目.我使用fosuserbundle来管理用户和索纳塔米邦进行管理.

我对此有一些疑问:

  1. 作为管理员,我想在用户编辑表单中设置用户的角色.如何在role_hierarchy中访问角色?我该如何将它们用作选择字段,以便管理员可以为用户设置角色?

  2. 当我在列表中显示角色时,它以这样的字符串显示:

    [0 => ROLE_SUPER_ADMIN] [1 => ROLE_USER] 
    

    如何将其更改为此?

    ROLE_SUPER_ADMIN, ROLE_USER 
    

    我的意思是,只有数组的值.

推荐答案

我找到了第一个问题的答案!(但是第二个问题尚未回答..) 我在configureFormFields函数中添加了下面的角色:

  protected function configureFormFields(FormMapper $formMapper) {
  //..
 $formMapper
 ->add('roles','choice',array('choices'=>$this->getConfigurationPool()->getContainer()->getParameter('security.role_hierarchy.roles'),'multiple'=>true ));
}

如果有人回答第二个问题,我会很高兴:)

其他推荐答案

基于@parissss的答案,尽管这是错误的,但这是一个工作解决方案.奏鸣曲输入字段将在保存一个角色时显示在给定角色下的所有角色.

另一方面,数据库中只能存储最重要的角色.但这是绝对有意义的.

protected function configureFormFields(FormMapper $formMapper) {
// ..
$container = $this->getConfigurationPool()->getContainer();
$roles = $container->getParameter('security.role_hierarchy.roles');

$rolesChoices = self::flattenRoles($roles);

$formMapper
    //...
    ->add('roles', 'choice', array(
           'choices'  => $rolesChoices,
           'multiple' => true
        )
    );

和另一种方法:

/**
 * Turns the role's array keys into string <ROLES_NAME> keys.
 * @todo Move to convenience or make it recursive ? ;-)
 */
protected static function flattenRoles($rolesHierarchy) 
{
    $flatRoles = array();
    foreach($rolesHierarchy as $roles) {

        if(empty($roles)) {
            continue;
        }

        foreach($roles as $role) {
            if(!isset($flatRoles[$role])) {
                $flatRoles[$role] = $role;
            }
        }
    }

    return $flatRoles;
}

在行动中看到它:

在此处输入图像说明 在此处输入图像说明

其他推荐答案

对于第二个问题,我在用户类中添加了一种看起来像

的方法
/**
 * @return string
 */
 public function getRolesAsString()
 {
     $roles = array();
     foreach ($this->getRoles() as $role) {
        $role = explode('_', $role);
        array_shift($role);
        $roles[] = ucfirst(strtolower(implode(' ', $role)));
     }

     return implode(', ', $roles);
 }

然后您可以在configureListFields函数中声明:

->add('rolesAsString', 'string')

本文地址:https://www.itbaoku.cn/post/2090776.html

问题描述

I'm using Symfony 2.1 for a project. I use the FOSUserBundle for managing users & SonataAdminBundle for administration usage.

I have some questions about that:

  1. As an admin, I want to set roles from users in users edit form. How can I have access to roles in role_hierarchy? And how can I use them as choice fields so the admin can set roles to users?

  2. When I show roles in a list, it is shown as string like this:

    [0 => ROLE_SUPER_ADMIN] [1 => ROLE_USER] 
    

    How can I change it to this?

    ROLE_SUPER_ADMIN, ROLE_USER 
    

    I mean, having just the value of the array.

推荐答案

i found an answer for my first question!(but the second one in not answered yet..) i add the roles like below in configureFormFields function :

  protected function configureFormFields(FormMapper $formMapper) {
  //..
 $formMapper
 ->add('roles','choice',array('choices'=>$this->getConfigurationPool()->getContainer()->getParameter('security.role_hierarchy.roles'),'multiple'=>true ));
}

I would be very happy if anyone answers the second question :)

其他推荐答案

Based on the answer of @parisssss although it was wrong, here is a working solution. The Sonata input field will show all the roles that are under the given role if you save one role.

On the other hand, in the database will be stored only the most important role. But that makes absolute sense in the Sf way.

protected function configureFormFields(FormMapper $formMapper) {
// ..
$container = $this->getConfigurationPool()->getContainer();
$roles = $container->getParameter('security.role_hierarchy.roles');

$rolesChoices = self::flattenRoles($roles);

$formMapper
    //...
    ->add('roles', 'choice', array(
           'choices'  => $rolesChoices,
           'multiple' => true
        )
    );

And in another method:

/**
 * Turns the role's array keys into string <ROLES_NAME> keys.
 * @todo Move to convenience or make it recursive ? ;-)
 */
protected static function flattenRoles($rolesHierarchy) 
{
    $flatRoles = array();
    foreach($rolesHierarchy as $roles) {

        if(empty($roles)) {
            continue;
        }

        foreach($roles as $role) {
            if(!isset($flatRoles[$role])) {
                $flatRoles[$role] = $role;
            }
        }
    }

    return $flatRoles;
}

See it in action:

enter image description here enter image description here

其他推荐答案

As for the second question I added a method in the User class that looks like this

/**
 * @return string
 */
 public function getRolesAsString()
 {
     $roles = array();
     foreach ($this->getRoles() as $role) {
        $role = explode('_', $role);
        array_shift($role);
        $roles[] = ucfirst(strtolower(implode(' ', $role)));
     }

     return implode(', ', $roles);
 }

And then you can declare in your configureListFields function:

->add('rolesAsString', 'string')