Firestore 的安全规则允许访问基于 UID 的所有子集合

DRi*_*ing 6 firebase-security google-cloud-firestore

因此,尝试设置我的 firestore 数据库,并且我有一个名为 Users 的集合,用于存储用户信息。我还有每个用户的 Towers 子集。我的用户文档有一个用于安全设置的 playerUid 字段。这是我目前的安全规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read: if request.auth.uid != null;
    }
    match /users/{user=**}{
        allow read, create: if request.auth.uid != null;
      allow update: if request.auth.uid == resource.data.playerUid;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这允许用户阅读、创建他们的用户文档和塔文档的子集合,但他们不能编辑子集合。塔文件中没有 playerUid。有没有办法使用用户文档中的 playerUid 来验证更新塔的身份?或者我是否需要在塔文件中添加一个 playerUid 字段以进行身份​​验证

Fra*_*len 5

您可以按照有关访问其他文档的 Firestore 文档中所示的子集合get规则中的用户文档:Towers

allow delete: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true
Run Code Online (Sandbox Code Playgroud)

或者,如果用户位于子集合的文档中,您确实可以包含 UID。这将避免需要在规则中阅读额外的文件。