Symfony FOSUserBundle集团的目的

时间:2016-05-13 09:49:51

标签: symfony security fosuserbundle roles

我正在尝试在我的项目中实现FOSUserBundle。 我刚刚设置了Group功能,创建了一个组并为其添加了一个用户。 让我感到困惑的是,用户并没有像我预期的那样继承组角色。我的期望是,如果用户拥有一个具有角色ROLE_ADMIN的组,那么该用户也将拥有该角色。 像

这样的东西
if (!$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
        throw $this->createAccessDeniedException();
    }

不会抛出异常,但确实如此 对我来说,这与文档在这里所说的相矛盾

  

组的角色将授予属于它的所有用户。

所以我的问题是,如何以正确的方式使用群组? 我是否应该容纳至少一个组中的所有用户,并且从不检查分配给用户的角色,还要检查他们的角色?

1 个答案:

答案 0 :(得分:1)

  • 不推荐使用服务security.context以及上述更改。推荐的    改为使用:     @security.authorization_checker => isGranted() @security.token_storage => getToken() @security.token_storage => setToken()

所以只是:

if($this->isGranted('ROLE_ADMIN'))

参考Symfony组件:

protected function isGranted($attributes, $object = null)
    {
        if (!$this->container->has('security.authorization_checker')) {
            throw new \LogicException('The SecurityBundle is not registered in your application.');
        }

        return $this->container->get('security.authorization_checker')->isGranted($attributes, $object);
    }