且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查是否为 Symfony2 ACL 中的特定用户授予角色

更新时间:2023-02-26 08:58:17

VIEW"是权限,不是角色.

检查用户是否拥有权利(无论是角色还是权限)的***方法是访问 AccessDecisionManager.类似的东西:

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

在此处查看原始答案:https://***.com/a/22380765/971254了解详情.>

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:

$securityContext = $this->get('security.context');

if (false === $securityContext->isGranted('VIEW', $objectIdentity)) {
        //do anything
}

but if I'm the logged user and I wand to check other user if isGranted ??

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://***.com/a/22380765/971254 for details.