Sonata Admin:isGranted() 在 Admin class 中始终 returns 为真,但在模板中为正确的布尔值

Sonata Admin: isGranted() always returns true in Admin class, but correct boolean in template

我在 Symfony 2 项目中使用 SonataAdminBundleSonataUserBundle。安装的软件包是:

$ composer show | grep symfony
friendsofsymfony/rest-bundle             1.7.7              This Bundle provides various tools to rapidly develop RESTful API's with Symfony
friendsofsymfony/user-bundle             v1.3.6             Symfony FOSUserBundle
symfony/assetic-bundle                   v2.8.0             Integrates Assetic into Symfony2
symfony/css-selector                     v2.8.6             Symfony CssSelector Component
symfony/dom-crawler                      v2.8.6             Symfony DomCrawler Component
symfony/monolog-bundle                   2.11.1             Symfony MonologBundle
symfony/polyfill-apcu                    v1.1.1             Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-mbstring                v1.1.1             Symfony polyfill for the Mbstring extension
symfony/swiftmailer-bundle               v2.3.11            Symfony SwiftmailerBundle
symfony/symfony                          v2.7.13            The Symfony PHP framework

$ composer show | grep sonata
sonata-project/admin-bundle              2.3.10             Symfony SonataAdminBundle
sonata-project/block-bundle              2.2.15             Symfony SonataBlockBundle
sonata-project/cache                     1.0.7              Cache library
sonata-project/core-bundle               2.3.11             Symfony SonataCoreBundle
sonata-project/doctrine-extensions       1.0.2              Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle 2.3.4              Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.1.10             Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.4.1              Lightweight Exporter library
sonata-project/google-authenticator      1.0.2              Library to integrate Google Authenticator into a PHP project
sonata-project/user-bundle               2.2.5              Symfony SonataUserBundle

security.yml 配置文件中的角色:

role_hierarchy:
    ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
    ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

使用只有 ROLE_ADMIN 的用户登录,以下是我的 UserAdmin class 的转储:

dump($this->isGranted('ROLE_ALLOWED_TO_SWITCH'));
dump($this->isGranted('ROLE_BLA_BLA_BLA'));
dump($this->isGranted('ROLE_USER'));

在 Symfony 工具栏中打印(在 dev 环境中)

true
true
true

虽然如果我将转储放在一个覆盖的 Sonata 模板中,例如 app/Resources/SonataAdminBundle/views/CRUD/[anytemplate].html.twig

{{ dump(is_granted('ROLE_ALLOWED_TO_SWITCH')) }}
{{ dump(is_granted('ROLE_BLA_BLA_BLA')) }}
{{ dump(is_granted('ROLE_USER')) }}

正确的值是 returned。

false
false
true

我进入这个是因为这里的这一行,在 SonataUserBundle 中没有效果:https://github.com/sonata-project/SonataUserBundle/blob/3.x/Admin/Model/UserAdmin.php#L95

isGranted() 用法描述如下:http://symfony.com/doc/current/bundles/SonataAdminBundle/reference/security.html#usage

我是不是做错了什么或者这是一个错误?

编辑:

感谢@mickadoo 的评论,我注意到我有默认处理程序 sonata.admin.security.handler.noop,据说它总是 return true,不管那是什么意思。我用 sonata.admin.security.handler.role 设置它并创建了一些角色(ROLE_SONATA_USER_ADMIN_USER_LISTROLE_SONATA_USER_ADMIN_USER_VIEW),现在它 return 是 $this->isGranted('LIST') 或 [=34= 的正确值],但对于 $this->isGranted('ROLE_USER')$this->isGranted('ROLE_ALLOWED_TO_SWITCH'),总是 returns false

如何查看这个角色?

通用、非实体角色,如ROLE_USERROLE_ADMINROLE_SUPER_ADMINROLE_{CUSTOM_STRING}应该使用默认的 Symfony 安全上下文。

Admin class:

$securityContext = $this->getConfigurationPool()->getContainer()->get('security.context');

if ($securityContext->isGranted('ROLE_USER')) {
    // Your PHP code here
}

Twig 模板中:

{% if is_granted('ROLE_USER') %}
    Your HTML/Twig content here.
{% endif %}

实体操作角色,如 ROLE_SONATA_USER_ADMIN_USER_LISTROLE_SONATA_USER_ADMIN_USER_VIEWROLE_{CUSTOM_SONATA_ADMIN_SERVICE_NAME}_{ACTION} 可以使用 Sonata Admin 助手或 Symfony 安全上下文进行检查。

Admin class:

// Using Symfony security context
$securityContext = $this->getConfigurationPool()->getContainer()->get('security.context');

if ($securityContext->isGranted('ROLE_SONATA_USER_ADMIN_USER_LIST')) {
    // your code here
}

// Using Sonata helper for shorter syntax
if ($this->isGranted('LIST')) {
    // your code here
}

Twig 模板中:

<!-- Using Symfony security context -->
{% if is_granted('ROLE_SONATA_USER_ADMIN_USER_LIST') %}
    Your HTML/Twig content here.
{% endif %}

<!-- Using Sonata helper -->
{% if admin.isGranted('LIST') %}
    Your HTML/Twig content here.
{% endif %}

我现在正在做一个项目,似乎 isGranted 正在 Admin classes 中做这件事。 我在管理员 class:

中找到的解决方法
if ($this->getContainer()->get('security.token_storage')->getToken()->getUser()->getRoles()[0] == 'ROLE_EMPLOYEE') {}

它 returns 您在创建用户时分配的角色(无继承)。 您当然需要满足继承的要求,因此您必须列出您想要允许或禁止的所有角色,以较少列出的为准。 检查 getUser() 不为空并且 getRoles() returns 一个数组。

这在 sonata admin 和 Symfony 4 中对我有用:

/**
 * @param string $role
 * @return bool
 */
protected function checkUserHasRole(string $role): bool
{

    $securityContext = $this->getConfigurationPool()->getContainer()->get('security.authorization_checker');
    try {
        return $securityContext->isGranted($role);
    } catch (AuthenticationCredentialsNotFoundException $e) {
        return false;
    }
}