我将 SonataAdminBundle 与 Symfony 2.2 一起使用,并希望根据登录的用户显示仪表板块。
例如
- 具有组超级管理员的用户将看到块“用户管理”和“消息”
- 拥有 Group Staff 的用户只会看到块“消息”
我阅读了整个文档,尤其是安全文档,但没有找到有关如何限制仪表板视图的信息。我已经实现了一个过滤器,如果用户的权限不够,它将在实体类的列表视图中不显示任何条目。
但最好根本不给他看积木。
关于如何做到这一点的任何想法?
我将 SonataAdminBundle 与 Symfony 2.2 一起使用,并希望根据登录的用户显示仪表板块。
例如
我阅读了整个文档,尤其是安全文档,但没有找到有关如何限制仪表板视图的信息。我已经实现了一个过滤器,如果用户的权限不够,它将在实体类的列表视图中不显示任何条目。
但最好根本不给他看积木。
关于如何做到这一点的任何想法?
好吧,对于遇到此问题的任何人,我只需在 execute() 中返回一个空响应即可解决它。我的用例是我想向具有特定角色的用户显示一个块:首先我定义了使用 security.context 服务的服务,如下所示:
sonata.block.service.foo:
class: Acme\DemoBundle\Block\FooBlockService
arguments: [ "sonata.block.service.foo", "@templating", "@doctrine", "@security.context" ]
tags:
- { name: sonata.block }
然后我将块服务 __construct() 定义为:
//Acme\DemoBundle\Block\FooBlockService
public function __construct($name, EngineInterface $templating, $doctrine, $securityContext)
{
parent::__construct($name, $templating);
$this->doctrine = $doctrine;
$this->securityContext = $securityContext;
}
最后,在执行函数中,我做的第一件事是检查用户的特定角色,如下所示:
//Acme\DemoBundle\Block\FooBlockService
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
if( $this->securityContext->isGranted("ROLE_ACME_FOO") === false )
{
return new Response();
}
//... more code
这行得通,但感觉就像一个黑客。主要是因为 Block 经历了它的所有阶段,并且只有在输出上它什么都不返回,这意味着开销。更好的解决方案是根据一些自定义代码以某种方式阻止整个 Block 被加载。
总之,这不是最好的方法,但它对我有用!
您可以使用角色限制访问以阻止,如下所示:
sonata_admin:
dashboard:
blocks:
- { position: top, type: your_service_block_name, class: 'col-xs-4', roles: [ROLE_SOME_NAME_HERE_, ROLE_SOME_NAME_HERE_2] }
您可以检查它是SonataAdminBundle:Core:dashboard.html.twig
如何roles
工作的:
{% if block.roles|length == 0 or is_granted(block.roles) %}
<div class="{{ block.class }}">
{{ sonata_block_render({ 'type': block.type, 'settings': block.settings}) }}
</div>{% endif %}