CouchDB ACL交叉引用文档

时间:2011-07-18 18:05:38

标签: reference couchdb acl

假设我有两种类型的文档,其中一种引用另一种,例如: “orders”和“order_replies”后一个具有字段“ref”,其中包含订单文档的ID。

对于我的validate_doc_update函数,我希望用户只有在他是原始订单的作者时才能更改order_reply文档。

如何从函数中的ID为newDoc.ref的订单中获取“author”字段?

到目前为止,这是我的验证功能的一部分:

if(userCtx.roles.indexOf('employee') >= 0) {
  // [...] other doc types
  if (newDoc.type == 'order_reply') {
    //newDoc.ref.author is not the proper approach
    require((userCtx.name == newDoc.ref.author), "this is not your order reply!");
  }
}

1 个答案:

答案 0 :(得分:2)

将作者的ID放在订单的ID中。例如:

  • 订单:{ "_id": "order/jack/1", ... }
  • 订单的回复:{ "ref": "order/jack/1", ... }

所以你可以查看:

if (userCtx.roles.indexOf('employee') !== -1) {
  if (newDoc.type === 'order_reply') {
    require(userCtx.name === newDoc.ref.split('/', 3)[1], "this is not your order reply!");
  }
}

如果您有多位作者,请使用"order/author1/author2/.../authorN/ordernum"作为订单的_id并查看:

var parts = newDoc.ref.split('/'),
    authors = parts.slice(1, -1);
require(authors.indexOf(userCtx.name) !== -1, "this is not your order reply!");

更新:由于错误COUCHDB-1229,在doc._id中使用“/”可能会导致问题。根据您的使用情况,最好使用其他分隔符,例如“:”。