我的数据库结构是这样的:
//Sub collections
/inventory/{inventoryId}/armor/chest/
/inventory/{inventoryId}/armor/head/
...
// Document
/inventory/{inventoryId}.ownerUID // ownerUID = firebaseID
/inventory/{inventoryId}.charName // Character name that owns this inventory, each user can own multiple characters, each character has one inventory linked to it
可能不相关:
/characters/{charName}.ownerUID
/characters/{charName}.charName
/characters/{charName}.inventoryID
我正在尝试编写规则,以便每个用户只能读/写属于他的清单,对于清单中的顶级文档,我只能编写以下内容:
match /inventory/{inventoryID}/{document=**} {
allow read,write: if request.auth != null && resource.data.ownerUID == request.auth.uid
}
但是,这对于嵌套集合将失败,因为resource.data.ownerUID仅存在于顶层。
有没有办法我可以从{inventoryID}
获取/inventory/{inventoryID}/{document=**}
并对照firebaseID进行检查,或者可以某种方式使用/character/
中的数据
我唯一的选择是将ownerUID
添加到/inventory
的每个子集合中吗?
答案 0 :(得分:2)
如果您需要use fields from other documents而不是与match
模式匹配的文件,则可以使用get()
来读取该文档并使用其字段。例如:
match /inventory/{inventoryID}/{document=**} {
allow read, write: if
get(/databases/$(database)/documents/inventory/$(inventoryID)).data.ownerUID
== request.auth.uid;
}