检查是否为 Symfony2 ACL 中的特定用户授予角色 [英] Check if a role is granted for a specific user in Symfony2 ACL

查看:28
本文介绍了检查是否为 Symfony2 ACL 中的特定用户授予角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否为 Symfony2 中的特定用户(不是登录用户)授予了角色.我知道我可以通过以下方式检查登录用户:

$securityContext = $this->get('security.context');if (false === $securityContext->isGranted('VIEW', $objectIdentity)) {//做任何事情}

但是如果我是登录用户并且我想检查其他用户是否isGranted ??

解决方案

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

这篇关于检查是否为 Symfony2 ACL 中的特定用户授予角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆