首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >处理多级角色

处理多级角色
EN

Stack Overflow用户
提问于 2019-03-15 03:37:34
回答 1查看 161关注 0票数 0

我有一个与认证用户工作的多个应用程序。

所有这些应用程序都可以为不同的客户端一起部署,但使用相同的用户数据库。

例如,一家连锁酒店。

用户的角色信息在每个请求的标头中可用。

例如,经理在自己的酒店中具有完全访问权限,但在另一个酒店中仅具有读取访问权限。

例如:

代码语言:javascript
复制
{
    ["organization":"paris","roles":[ADMIN,ROOT]],
    ["organization":"london","roles":[READ]]
}

如何按组织处理多个级别的角色?

我在symfony上读到了一些关于投票者和角色的文档,但没有任何关于某种群体中角色的文档。

EN

回答 1

Stack Overflow用户

发布于 2019-03-15 04:25:45

投票者才是前进的方向

代码语言:javascript
复制
// 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)
    }
}

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

代码语言:javascript
复制
    if ($this->security->isGranted(OrganisationVoter::EDIT, $organisation)) {
        return true;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55170728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档