Kable
Kable

Reputation: 117

Symfony FOSUserBundle Group purpose

I am trying to implement the FOSUserBundle in my project. I have just set up the Group functionality, created a group and added a User to it. What really confuses me is that the user doesn't inherit the group roles like I expected. My expectation was that If a user has a group that has the role ROLE_ADMIN for example, that the user also will have that role. So something like

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

would not throw the exception, but it does To me that contradicts what the docs say here

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

So my question is, how do I use Groups the right way? Am I supposed to house all users in at least one group, and never check for the roles assigned to the user, but check their roles?

Upvotes: 0

Views: 172

Answers (1)

Aastal
Aastal

Reputation: 350

  • The service security.context is deprecated along with the above change. Recommended to use instead: @security.authorization_checker => isGranted() @security.token_storage => getToken() @security.token_storage => setToken()

So just :

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

Referre to Symfony Component:

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);
    }

Upvotes: 1

Related Questions