Check if a role is granted for a specific user in Symfony2 ACL

后端 未结 3 1423
忘了有多久
忘了有多久 2020-12-10 19:03

I want to check if a role is granted for a specific user in Symfony2 (not the logged user). I know that I can check it for the logged user by:

$securityCont         


        
3条回答
  •  庸人自扰
    2020-12-10 19:50

    The "VIEW" is a permission, not a role.

    The best way to check if a user has a right (be it a role or permission) would be to access the AccessDecisionManager. Something like:

    $token = new UsernamePasswordToken($user, 'none', 'none', $user->getRoles());
    $attributes = is_array($attributes) ? $attributes : array($attributes);
    $this->get('security.access.decision_manager')->decide($token, $attributes, $object);
    

    See original answer here: https://stackoverflow.com/a/22380765/971254 for details.

提交回复
热议问题