1

我正在为一个项目使用 CouchDB 和 PouchDB,在该项目中,用户拥有自己的数据库,并且可以相互添加以查看或编辑他们的文档。

目标是具有不同级别的可访问性:根据文档本身,非 docs.owner 的其他用户将具有有限的写入/更新权限。数据库所有者/管理员授予这些权限。

我不确定如何正确实施。

目前我的解决方案是让数据库所有者“成为”其他用户,并将他们作为成员添加到 db/_security,同时使用 _design 文档限制写作权限,如下所述:https ://github.com/pouchdb -community/pouchdb-authentication/blob/master/docs/recipes.md

但我需要用户特定权限和数据库特定权限的混合。所以我的策略是让用户/所有者除了默认的“members”和“admins”之外添加特殊角色到 db/_security。

例子:

用户paula拥有数据库paulas_DB并希望授予用户 jan 更改每个文档的属性“位置”的权利。因此,Paula 将jan添加到_security 中的members.names中,并在 _security 中添加了一个名为“movers”新列表:

curl -X PUT $HOST/paulas_DB/_security -d '{"members":{"names":["admin","paula","jan"],"roles":[]},"admins":{"names":["admin","paula"]},"movers":["jan"]}'

paulas_DB中的文档结构如下:

{
  "_id": "y",
  "_rev": "7-x",
  "owner": "paula",
  "location": "somewhere",
  "name":"thing"
}

现在在她的数据库中有一个设计文档,检查任何想要更改文档的人至少是一个成员,然后检查他们是否想像这样更改位置:

function (newDoc, oldDoc, userCtx, secObj) {

// only admins owners or friends of the owner (aka users in the _security members.names list) can edit
if (userCtx.roles.indexOf('_admin') === -1 && oldDoc.owner !== userCtx.name && secObj.members.names.indexOf(userCtx.name) === -1) 
 {
    // next step: add special fields to be either editable or not
    throw({forbidden : "sorry. you are not the owner of this document"});
 }

// only owners users who are also listed within _security.movers can change the location
if (oldDoc.location !== newDoc.location && oldDoc.owner !== userCtx.name && secObj.movers.indexOf(userCtx.name) === -1)
 {
    throw({forbidden : "you are not allowed to change the location of an item, dummie!"})
 }
}

这种方法似乎有效并且相当简单,但是将非标准属性添加到 _security 中感觉有些不对劲。

是否有另一种适当的方法来做同样的事情?或者对于文档/用户特定的权限系统来说,这是一个可接受的设计吗?

4

0 回答 0