【问题标题】:Firestore rules allow subcollectionFirestore 规则允许子集合
【发布时间】:2020-06-27 10:03:19
【问题描述】:

我的数据库结构是这样的:

//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 仅存在于顶层。

有没有办法我可以从 /inventory/{inventoryID}/{document=**} 获取 {inventoryID} 并根据 firebaseID 进行检查,或者以某种方式使用来自 /character/ 的数据

我唯一的选择是将ownerUID 添加到/inventory 的每个子集合吗?

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

    如果您需要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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 2018-07-02
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      相关资源
      最近更新 更多