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

后端 未结 7 1886
庸人自扰
庸人自扰 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:03

    security.context Is deprecated since 2.6.

    Use AuthorizationChecker:

    $token = new UsernamePasswordToken(
         $user,
         null,
         'secured_area',
         $user->getRoles()
    );
    $tokenStorage = $this->container->get('security.token_storage');
    $tokenStorage->setToken($token);
    $authorizationChecker = new AuthorizationChecker(
         $tokenStorage,
         $this->container->get('security.authentication.manager'),
         $this->container->get('security.access.decision_manager')
    );
    if (!$authorizationChecker->isGranted('ROLE_ADMIN')) {
        throw new AccessDeniedException();
    }
    

提交回复
热议问题