检查Sonata管理员列表中的权限

时间:2015-12-23 14:07:22

标签: symfony sonata-admin

我需要禁用下载列表,并根据Sonata Admin

中的用户权限自定义查询

这会根据角色

限制列表结果
public function createQuery($context = 'list')
{
    $query = parent::createQuery($context);
    $security_context = $this->getConfigurationPool()->getContainer()->get('security.context');
    $user = $security_context->getToken()->getUser();
    $staff = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository('AppBundle:Staff')->findOneBy(array('user' => $user));
    if ($security_context->isGranted('ROLE_ADMIN')  && !$security_context->isGranted('ROLE_EXECUTIVE_ADMIN')) 
    {
        $query->andWhere($query->getRootAlias().'.store',':store');
        $query->setParameter('store', $staff->getStore());
    }
    return $query;
}

这应该根据权限隐藏下载按钮

protected function configureRoutes(RouteCollection $collection)
{
    $collection->remove('delete')
    ->remove('create');
    $security_context = $this->getConfigurationPool()->getContainer()->get('security.context');
    if ($security_context->isGranted('ROLE_ADMIN')  && !$security_context->isGranted('ROLE_EXECUTIVE_ADMIN')) 
    {
        $collection->remove('export');
    }
}

如何实现预期的目标,因为此实现会返回以下错误:

The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL in . (which is being imported from "E:\www\project\app/config\routing.yml").

1 个答案:

答案 0 :(得分:0)

现在不推荐使用Symfony 2.6 security.context ,您应该使用 security.authorization_checker 服务:http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

现在就像这样:

protected function configureRoutes(RouteCollection $collection)
{
    $collection->remove('delete')
               ->remove('create');
    $authorization_checker = $this->getConfigurationPool()->getContainer()->get('authorization_checker');
    if ($authorization_checker->isGranted('ROLE_ADMIN')  && !$authorization_checker->isGranted('ROLE_EXECUTIVE_ADMIN')) 
    {
        $collection->remove('export');
    }
}