我需要在 symfony 2 中添加新的自定义角色来管理 SONATA Bundle 中的权限

问题描述 投票:0回答:5

我需要创建一个新的角色作为 ROLE_ERECTA_TASK_ADMIN 但我不知道如何,我必须在哪里声明才能在 Sonata 管理界面中设置它? 我使用 Sonata Bundle 来管理我的用户组角色,现在我只有一些角色,但我想创建一些其他形式的捆绑包。

我的安全.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 Admin 用户管理员: enter image description here

提前致谢。

php symfony security sonata-admin symfony-sonata
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_ADMIN
ROLE_SONATA_ADMIN
添加到您的角色中。


1
投票

还有另一种快速解决方法来添加角色。只需编辑

security.yml
并将角色添加到 ROLE_SUPER_ADMIN。

role_hierarchy:
    ...
    ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH, ROLE_FOO, ROLE_BAR, ...]

0
投票

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

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


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 类(我已经在我的 BaseAdmin 类中完成了此操作,该类扩展自 Sonata\AdminBundle\Admin\Admin)

/**
 * 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');

就是这样。努力和设计对我来说似乎很公平。 :-)


-2
投票

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

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

公共函数 getDefaultOptions(array $options) { $options = 父级::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 with the roles added by my code

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

Symfony System account info

© www.soinside.com 2019 - 2024. All rights reserved.