Symfony FOSUserBundle 组目的

Symfony FOSUserBundle Group purpose

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

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

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

The roles of a group will be granted to all users belonging to it.

所以我的问题是,如何正确使用网上论坛? 我是否应该将所有用户至少放在一个组中,从不检查分配给用户的角色,而是检查他们的角色?

  • 服务 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);
    }