Symfony Voters 对象集合

标签 symfony security

我正在使用 Voters 来管理我的应用程序的权限,并且对于单个对象来说一切正常。

我似乎无法做的是将投票者应用于对象集合。例如,我有一个端点/persons ,它将返回完整的人员列表,但应该根据每个用户的权限进行过滤(部门经理应该只看到自己部门中的人员)。有什么办法可以让选民做到这一点吗?

最佳答案

这可以使用授权检查器来完成:

// your controller

private $authorizationChecker;

public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
    $this->authorizationChecker = $authorizationChecker;
}

// in your action

return array_filter($userCollection, function (User $user) {
    return $this->authorizationChecker->isGranted('VOTER_NAME', $user);
});

或者如果你是 Controller ,则扩展 Symfony 的 Controller 类:

return array_filter($userCollection, function (User $user) {
    return $this->isGranted('VOTER_NAME', $user);
});

关于Symfony Voters 对象集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50872773/

相关文章:

javascript - 如何使用 Symfony 和 Jquery 发出 POST Ajax 请求

symfony - 覆盖 FOSUserBundle 登录表单

Symfony2 : Set label of checkbox from Class Field

xml - 如何在 iOS 应用程序中验证 XML 签名?

php - Symfony2 - Doctrine - 有没有办法在一行中保存实体?

symfony - 如何在 Dql 中使用 Char_Length

c# - 我应该如何阻止用户修改我的数据文件?

android - 为什么 getHttpSocketFactory 已被弃用,什么替代了它?

security - 更改预先存在的数据库上的散列函数

java - jarsigner 错误 : java. lang.RuntimeException : keystore load: Keystore was tampered with, 或密码不正确