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

Google cloud firestore Firestore允许基于UID访问所有子集合的安全规则,google-cloud-firestore,firebase-security,Google Cloud Firestore,Firebase Security,因此,尝试设置firestore数据库时,我有一个名为Users的集合,用于存储用户信息。我还为每个用户提供了塔的子集合。我的用户文档有一个playerUid字段,用于安全设置。以下是我当前的安全规则: service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read: if request.auth.uid != null; }

因此,尝试设置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来验证塔的更新?或者我是否需要向Towers文档添加playerUid字段以进行身份验证您可以
获取
Towers
子集合规则中的用户文档,如所示:


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

此代码片段可能会对您有所帮助

    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
        }
      }
    }
service cloud.firestore{
匹配/databases/{database}/documents{
匹配/{document=**}{
允许读取:如果request.auth.uid!=null;
}
匹配/users/{user=**}{
允许读取、创建:if request.auth.uid!=null;

允许更新:if request.auth.uid==user;//如果您能解释代码如何/为什么可以帮助OP解决他的问题,那就太好了
    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
        }
      }
    }