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

时间:2018-03-22 03:02:54

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

因此,尝试设置我的firestore数据库,我有一个名为Users的集合,用于存储用户信息。我还为每个用户提供了塔的子集。我的用户文档有一个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;
    }
  }
}

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

2 个答案:

答案 0 :(得分:2)

您可get Towers子集合规则中的allow delete: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true 用户文档,如Firestore documentation on accessing other documents所示:

open('file','w+')

或者,如果用户在子集合的文档中,您确实可以包含UID。这样可以防止在规则中读取额外的文档。

答案 1 :(得分:0)

此代码段可能会为您提供帮助

    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 == user; // <== THIS LINE
        }
      }
    }

这是否将“用户”路径与“ uid”匹配? 有空的时候我会尝试测试一下。 如果可以,它将保存一个get呼叫。

相关问题