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

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

高层次,软件是供合作者和审计人员在项目中工作/查看任务的。

MongoDB中的“Project”实体/表用于pipe理ACL组。 每个项目都有一个用户指针的“Collaborator”数组和一个用户指针的“Auditor”数组。 Collaborators数组中的用户被添加到具有读/写访问权限的项目“Collaborator”ACL组中,Auditors数组中的用户被添加到只读的项目auditor acl组中。

在MongoDB中还有另一个名为“Task”的实体,每个任务都绑定到一个,只有一个项目。 一个项目可以有多个任务。 任务获取的项目添加到它的协作ACL组和审计员ACL组。

所有这一切都很好。 现在,用户Bob是项目A上的协作者,项目B上是审计者。如果Bob在数据库中查询任务,他将返回他可以读写的任务(项目A)以及他只能读取的任务(项目B)。 但是,前端如何知道哪些任务他有写权限,哪些只读? 因为前端仅需要在用户具有写入权限的任务旁边显示“编辑”button。

我在ACL权限中看到,我可以执行一个调用来检查用户是否对单个对象具有写权限,但是这是针对每个对象的,并且对于每个对象执行额外的调用将会影响性能,即使它是在服务器上完成之前发送初始查询响应。

我可以用“AND当前用户权限包含写入”这样的filter来查询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}] } 

然后当调用对象 – 服务器端代码需要应用“有效的权限”

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

只读 – 如果我们在acl列表中有条目,将被作为快速检查进行调用

在聚合下方列出ACL列表给定用户的所有项目 – 可以使用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]) 

输出:

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