2

我需要创建一个新的 ROLE 作为 ROLE_ERECTA_TASK_ADMIN 但我不知道如何在 Sonata 管理界面中声明它?我使用 Sonata Bundle 来管理我的用户组角色,现在我只有一些角色,但我想创建一些其他形式的我的 Bundles。

我的安全.yml

role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        # PROTEZIONE MODULO TASK
        ROLE_ERECTA_TASK_ADMIN: [ROLE_ERECTA_TASK_USER]
        ROLE_ERECTA_TASK_SA: [ROLE_ERECTA_TASK_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        SONATA:
            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are using acl then this line must be commented

Sonata 管理员用户管理员: 在此处输入图像描述

提前致谢。

4

5 回答 5

2

我建议您像这样在 formMapper 中手动设置角色:

$formMapper->with('Roles')
                ->add('roles', 'choice',
                    array('choices'=>
                    array('ROLE_SUPER_ADMIN' => 'ROLE_SUPER_ADMIN', 'ROLE_...' => 'ROLE_...'),
                        'expanded'=> true,
                        'multiple'=> true))
                ->end();

还要添加ROLE_ADMINROLE_SONATA_ADMIN到您的角色。

于 2013-08-23T08:29:14.793 回答
1

There is another quick workaround to add roles. Just edit security.yml and add roles to ROLE_SUPER_ADMIN.

role_hierarchy:
    ...
    ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH, ROLE_FOO, ROLE_BAR, ...]
于 2013-08-25T19:40:43.417 回答
0

对于更灵活的实现,还可以覆盖 vendor/sonata-project/user-bundle/Security/EditableRolesBuilder.php。

不要直接编辑此文件,而是通过捆绑继承或覆盖服务 sonata.user.editable_role_builder 来注入自定义类。

于 2014-10-29T11:00:47.743 回答
0

我的管理课程中确实有一些自定义操作。我所做的是,只是在管理类中“配置”这些。标准 Sonata\UserBundle\Security\EditableRolesBuilder 调用 Sonata BaseAdmin 类“getSecurityInformation”的公共函数:

foreach ($admin->getSecurityInformation() as $role => $permissions) {
$role = sprintf($baseRole, $role);
if ($isMaster) {
    // if the user has the MASTER permission, allow to grant access the admin roles to other users
    $roles[$role] = $role;
} elseif ($this->securityContext->isGranted($role)) {
    // although the user has no MASTER permission, allow the currently logged in user to view the role
    $rolesReadOnly[$role] = $role;
}

}

这就是我要加入的地方。只需覆盖这个函数自己的 Admin 类(我在从 Sonata\AdminBundle\Admin\Admin 扩展的 BaseAdmin 类中完成了这个)

/**
 * List here the customized roles actions which are used within the Admin class you have extended. (e.g. the
 * CustomerAdmin uses a special function to login as the customer. In this case set the array to array('LOGIN') and
 * use at certain points like ->isGranted('LOGIN'). This is also available in templates like
 * admin.isGranted('LOGIN', object)).
 * The actions you are listing here, will be appended to the standard actions: EDIT, LIST, CREATE, VIEW, DELETE,
 * EXPORT, OPERATOR, MASTER.
 *
 * @see http://sonata-project.org/bundles/admin/master/doc/index.html
 *
 * @var array
 */
protected $customizedRoles = array();

/**
 * {@inheritdoc}
 */
public function getSecurityInformation()
{
    $standardAdminRoles = parent::getSecurityInformation();
    $customizedAdminRoles = $this->getCustomizedAdminRoles();

    $allAdminRoles = array_merge($standardAdminRoles, $customizedAdminRoles);
    ksort($allAdminRoles);

    return $allAdminRoles;
}

/**
 * Get the customized roles set at property of the Admin class 'customizedRoles' prepared to append to the standard
 * roles.
 *
 * @return array
 */
private function getCustomizedAdminRoles()
{
    $customizedRoles = array();

    if (is_array($this->customizedRoles) && !empty($this->customizedRoles)) {
        foreach ($this->customizedRoles as $customizedRole) {
            $customizedRole = strtoupper($customizedRole);
            $customizedRoles[$customizedRole] = $customizedRole;
        }
    }

    return $customizedRoles;
}

只需通过覆盖将这个数组填充到您的 Admin 类中:

/** @{inheritdoc} */
protected $customizedRoles = array('LOGIN');

而已。努力和设计对我来说似乎很公平。:-)

于 2015-01-16T07:48:28.357 回答
-2

我找到了另一种方法,感谢 Rpg600 :)

我在 vendor/bundles/Sonata/UserBundle/Form/Type/SecurityRolesType.php 上写了这段代码

公共函数 getDefaultOptions(array $options) { $options = parent::getDefaultOptions($options);

$roles = array();
//== MY-CODE ============================================================================================================
$Role_to_add= array();
foreach ($this->pool->getContainer()->getParameter('security.role_hierarchy.roles') as $key => $value_roles_group_array)
    if('_ALL'== substr($key,-4,4))
            foreach ($value_roles_group_array as $key => $new_roles_string)
                $roles[$new_roles_string]=$new_roles_string;
//======================================================================================================================                
$rolesReadOnly = array();

...

现在在 app/config/security.yml

role_hierarchy:
    ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
    ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    # PROTEZIONE MODULO TASK
    ROLE_ERECTA_TASK_ALL: [ROLE_ERECTA_TASK_USER, ROLE_ERECTA_TASK_ADMIN, ROLE_ERECTA_TASK_SA]
    ROLE_ERECTA_TASK_ADMIN: [ROLE_ERECTA_TASK_USER]
    ROLE_ERECTA_TASK_SA: [ROLE_ERECTA_TASK_ADMIN, ROLE_ALLOWED_TO_SWITCH]
    SONATA:
        - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are using acl then this line must be commented

当我在层次结构角色中添加一个以“_ALL”结尾的角色时,我的代码会加载所有子元素,在奏鸣曲管理表单用户中显示新角色字符串。

Sonata Form Admin 具有我的代码添加的角色

现在,当我执行登录时,我可以看到我的新角色。

Symfony 系统帐户信息

于 2013-08-24T23:43:49.830 回答