处理多层次的角色

问题描述 投票:0回答:2

我有一个与经过身份验证的用户一起使用的多个应用程序。
所有这些应用程序可以针对不同的客户端一起部署,但具有相同的用户数据库。
例如连锁酒店。
用户的角色信息可在每个请求的标头中找到。

例如,经理在自己的酒店拥有完全访问权限,但在另一家酒店只能读取访问权限。
例如:

{
    ["organization":"paris","roles":[ADMIN,ROOT]],
    ["organization":"london","roles":[READ]]
}

我如何处理组织中的多个级别的角色?
我在 symfony 中读到了一些有关选民和角色的文档,但没有读到有关某种群体中角色的信息。

symfony authorization symfony-voter
2个回答
2
投票

选民是出路

// src/Security/PostVoter.php
namespace App\Security;

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

class OrganisationVoter extends Voter
{
    // these strings are just invented: you can use anything
    const READ= 'READ';
    const EDIT = 'EDIT ';

    protected function supports($attribute, $subject); bool //todo
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
    {
        // [...] check class like documentation
        $organisation= $subject;

        switch ($attribute) {
            case self::READ:
                return $this->canView($organisation, $user);
            case self::EDIT:
                return $this->canEdit($organisation, $user);
        }
    }

    private function canView(Organisation $organisation, User $user)
    {
        //here your logic if your user has the same organisation
    }

    private function canEdit(Organisation $organisation, User $user)
    {
        //here your logic if your user has the same organisation than the one in parameter and the good level of right (admin, root)
    }
}

然后在你的控制器(或树枝或任何地方)

    if ($this->security->isGranted(OrganisationVoter::EDIT, $organisation)) {
        return true;
    }

0
投票

您所描述的内容完全是“基于属性的访问控制”。 帮助您将授权与您想要保护的应用程序/API 外部化/解耦。这意味着您可以独立于授权逻辑来开发功能。

有几个标准 - 即 XACML 和 ALFA(授权缩写语言)。

这就是架构的样子:

XACML Architecture

  • 策略执行点 (PEP) 拦截业务流并创建授权请求,并将其发送给 PDP
  • 策略决策点 (PDP) 根据配置的策略评估传入请求。它最终将决定返回给 PEP
  • PDP 可以使用策略信息点 (PIP) 来检索丢失的元数据(用户的部门、角色、位置;资源的部门、所有者......)

之前的答案迫使您实施投票者。这是非常脆弱的,需要随着需求的变化定期编码和更新。在 ALFA 中,您不需要这样做。您只需用简单的古英语编写策略,使用您感兴趣的属性。例如:

  • 角色==“经理”的用户可以对类型==“酒店”的对象执行操作==“查看”
  • 角色==“经理”的用户可以对类型==“酒店”的对象执行操作==“编辑”,如果hotel.owner == user.name
© www.soinside.com 2019 - 2024. All rights reserved.