2011-07-18 27 views
3

比方說,我有兩種類型的文檔,一種引用另一種文檔,例如, 「orders」和「order_replies」具有包含訂單文檔的ID的字段「ref」的後者。CouchDB ACL交叉引用文檔

對於我的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!"); 
    } 
} 
+0

請注意我已經添加在[我的迴應]警告(http://stackoverflow.com/questions/6737285/couchdb-acl-cross-referencing-documents/6788113#6788113)。 –

回答

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中使用「/」可能會導致問題。根據你的使用情況,最好使用另一個分隔符,例如「:」。

+0

可悲的是這是行不通的,因爲可能有多個作者,所以他們的名字不能是ID的一部分。 – degeeman

+0

@ vc666我編輯了允許多個作者的答案。沒時間測試代碼,抱歉...希望它有幫助。 –

+1

謝謝,這將不得不做。但我不明白爲什麼CouchDB不提供一種方法來檢索函數中的文檔... – degeeman