确定查询中每个对象的用户权限?

Determine users permissions per object in query?

我们正在设计一个协作软件数据库结构并使用MEAN堆栈,我缺乏ACL权限经验提示了这个问题。

高级别,该软件供协作者和审计员在项目中执行 work/view 任务。

MongoDB中的'Project'entity/table用于管理ACL组。每个项目都有一个 'Collaborator' 用户指针数组和一个 'Auditor' 用户指针数组。 Collaborators 数组中的用户被添加到具有 read/write 访问权限的 projects 'Collaborator' ACL 组,Auditors 数组中的用户被添加到只读的 projects auditors acl 组。

MongoDB 中还有另一个名为 'Task' 的实体,每个任务都与一个项目相关联,而且只有一个项目。一个项目可以有多个任务。任务获取项目 Collaborate ACL 组和 Auditor ACL 组添加到它。

所以一切都很好。现在,用户 Bob 是项目 A 的协作者和项目 B 的审核员。如果 Bob 查询数据库中的任务,他将取回他可以 read/write 在(项目 A)上的任务和他只能读取的任务(项目 B)。但是前端怎么知道哪些任务他有写权限,哪些任务他只读呢?因为前端只需要在用户具有写入权限的任务旁边显示 'edit' 按钮。

我在 ACL 权限中看到,我可以进行调用以检查用户是否对单个对象具有写权限,但这是针对每个对象的,如果对每个对象进行额外的调用,即使它是在发送初始查询响应之前在服务器上完成。

我可以使用 'AND current user permissions contain Write' 之类的过滤器查询 mongo 中的任务实体组吗?或者应该如何处理?

有这样的项目ACL:

projectAcl{
    id:1,
    projectId:1,        

    // those subDocuments can be also a groupId and reference for exteral document
    auditors:[{userNAme:"John", userId:2},{userNAme:"Marry", userId:12}], 
    colaborators:[{userNAme:"Anna", userId:3},{userNAme:"Eric", userId:4}]
}

然后在调用对象时 - 服务器端代码需要应用 "effective permissions"

task{
    id:23,
    projectId:1,
    /*
    all fields needed here
    */
    // and here we have fields aded in serwerSide code
    readOnly:null //
}

readOnly - 如果我们在 acl 列表中有一个条目,将被称为快速检查

下方汇总列出给定用户的所有项目,其中包含 ACL 列表 - 这可能有助于设置 task/projects 权限或使用 CQRS 模式添加额外的安全层

var givenUserId = 4;
var matchArraysByUser = {
    $match : {
        $or : [{
                "auditors.userId" : givenUserId
            }, {
                "colaborators.userId" : givenUserId
            }
        ]
    }
}

var filterArrysByUser = {
    $project : {
        _id : 0,
        projectId : 1, //all needed fields set to 1
        colaborator : {
            $filter : {
                input : "$colaborators",
                as : "colaborator",
                cond : {
                    $eq : ["$$colaborator.userId", givenUserId]
                }
            }
        },

        auditor : {
            $filter : {
                input : "$auditors",
                as : "auditor",
                cond : {
                    $eq : ["$$auditor.userId", givenUserId]
                }
            }
        },
    }
}

var group = {
    $group : {
        _id : givenUserId,
        "auditor" : {
            $addToSet : {
                $cond : {
                    if  : {
                        $ne : ["$auditor", []]
                    },
                then : "$projectId",
                else  : null
            }
        }
    },
    "colaborator" : {
        $addToSet : {
            $cond : {
                if  : {
                    $ne : ["$colaborator", []]
                },
            then : "$projectId",
            else  : null
        }
    }
}}}

db.projAcl.aggregate([matchArraysByUser, filterArrysByUser, group])

output:

{
    "_id" : 4.0,
    "auditor" : [ 
        null, 
        2.0
    ],
    "colaborator" : [ 
        1.0, 
        null
    ]
}