How to use the AccessDecisionManager in Symfony2 for authorization of arbitrary users?

后端 未结 7 1893
庸人自扰
庸人自扰 2020-12-14 21:14

I\'d like to be able to verify whether or not attributes (roles) are granted to any arbitrary object implementing UserInterface in Symfony2. Is this possible?

7条回答
  •  爱一瞬间的悲伤
    2020-12-14 22:01

    Maybe you can instantiate a new securityContext instance and use it to check if user is granted :

    $securityContext = new \Symfony\Component\Security\Core\SecurityContext($this->get('security.authentication.manager'), $this->get('security.access.decision_manager'));
    $token           = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($user, null, $this->container->getParameter('fos_user.firewall_name'), $user->getRoles());
    $securityContext->setToken($token);
    if ($securityContext->isGranted('ROLE_ADMIN')) {
        // some stuff to do
    }
    

提交回复
热议问题