- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在开发一个 Symfony2 应用程序,我们想在其中引入 Security Voters . DX 计划(jay!)给我们带来了the simpler version这种机制,所以我想使用它。关于 \Symfony\Component\Security\Core\Authorization\Voter\AbstractVoter
的使用,唯一让我有点困扰的是,当我只实现抽象 isGranted
方法。我想为我们的应用程序更改它,但非常感谢您从安全角度对此事提出的意见。
明确地说,在我们的应用程序中,我们将使用unanimous
访问决策管理器策略 - 简而言之:您至少需要一个 ACCESS_GRANTED
而不需要 ACCESS_DENIED
- 因为我们稍后可能想介绍禁止 IP 地址或其他恶作剧的选民。
如下面的相关代码所示,只要 AbstractVoter
的实现支持某个属性,投票就会默认为 Denied
。
public function vote(TokenInterface $token, $object, array $attributes)
{
if (!$object || !$this->supportsClass(get_class($object))) {
return self::ACCESS_ABSTAIN;
}
// abstain vote by default in case none of the attributes are supported
$vote = self::ACCESS_ABSTAIN;
foreach ($attributes as $attribute) {
if (!$this->supportsAttribute($attribute)) {
continue;
}
// as soon as at least one attribute is supported, default is to deny access
$vote = self::ACCESS_DENIED;
if ($this->isGranted($attribute, $object, $token->getUser())) {
// grant access as soon as at least one voter returns a positive response
return self::ACCESS_GRANTED;
}
}
return $vote;
}
我想做的是用下面的代码覆盖它
public function vote(TokenInterface $token, $object, array $attributes)
{
if (!$object || !$this->supportsClass(get_class($object))) {
return self::ACCESS_ABSTAIN;
}
$vote = self::ACCESS_ABSTAIN;
foreach ($attributes as $attribute) {
if (!$this->supportsAttribute($attribute)) {
continue;
}
// This is where we differ from SymfonyAbstractVoter. Only if there is an outspoken yes or no, a vote is cast
// When returning null, it will still abstain
$grant = $this->isGranted($attribute, $object, $token->getUser());
if ($grant === true) {
return self::ACCESS_GRANTED;
}
if($grant === false) {
return self::ACCESS_DENIED;
}
}
return $vote;
}
你怎么看?当从 isGranted
返回 null 时返回 ACCESS_ABSTAIN
是否安全,并且如果投票者只施放 ACCESS_GRANTED
或 ACCESS_DENIED
返回真还是假?
我为什么要这样做?仍然使用 AbstractVoter
(因为,是的,我是一个懒惰的开发人员)但要分开我的关注点。
假设我为一个帖子设置了一个投票者,PostVoter
,这将允许我编辑我自己的帖子。然后它将转换 ACCESS_GRANTED
。但只要我不拥有该帖子,这个投票者就真的不在乎:它应该只投一个 ACCESS_ABSTAIN
。稍后它可能会遇到 AdminVoter
,它仍然会转换 ACCESS_GRANTED
。但是,当使用 unanimous
访问决策管理器策略时,如果此 PostVoter
已经投了一个 ACCESS_DENIED
,它将永远不会到达任何其他选民。
我现在无法在 AbstractVoter
中转换 ACCESS_ABSTAIN
而不重写 vote
方法以及我脑海中简单选民的全部观点好吧,是为了让它更简单 :)
最佳答案
AbstractVoter
类是大多数用例的简单 Bootstrap 实现,它模拟了 internal 的实现方式。 voters behave ,所以一旦您觉得您的解决方案与基本情况不同,您应该可以随意使用您自己的方法进行重写。
关于php - 只是检查 : abstain vote from Symfony AbstractVoter isGranted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28233178/
假设有某些路由字符串,例如受防火墙保护的“/path/index.html”,如何检查当前用户是否能够访问它? 提前致谢! 对不起,我应该更明确一点:我有一个路由名称数组,我构建了一个菜单。许多具有不
我发现在 Symfony 中我可以使用注释以一种干净的方式向我的 Controller 方法添加额外的功能,这真的很方便。例如: /** * @Route("/{id}") * @IsGrante
任何人都可以解释以下 c# 行为吗?我已经编写了一个小型控制台应用程序来了解 CAS,但我似乎无法理解为什么以下代码行会像它们那样工作: string[] myRoles = new string[]
有时,我有一个页面,我想对不同规则的复杂组合使用react,这些规则总共只是一件事:“当前用户可以执行此操作吗?”。 目前,大多数检查都直接在我的 Controller 内进行,因此我可以: if($
在我的应用程序中测试角色时,我发现了 SecurityContext 的函数 isGranted。它工作得很好,但现在我需要检查不是当前用户的用户的角色,所以 isGranted 对我不起作用。 我一
我目前正在为我的应用程序创建单元测试,但这是我第一次这样做。 我想测试这个功能: /** * This method search if the user is already in a team
我正在开发一个 Symfony2 应用程序,我们想在其中引入 Security Voters . DX 计划(jay!)给我们带来了the simpler version这种机制,所以我想使用它。关于
我正在将 Symfony 从 2.8 升级到 3.4,并且我有一个身份验证监听器。 监听器的构造器 public function __construct(EntityManager $entit
我在 Symfony 2 项目中使用 SonataAdminBundle 和 SonataUserBundle。安装的包是: $ composer show | grep symfony friend
我是一名优秀的程序员,十分优秀!