firebase - Firestore 规则 : How to allow nested document read based on parent property

标签 firebase google-cloud-firestore firebase-security

我的 firstore 数据库包含如下结构的集合和文档:

  • 用户 -> 事件 -> 事件 -> 流

我希望每个人都能够阅读事件集合中的文档及其子集合文档(事件+流),如果事件集合文档具有属性,例如对字符串“public”的可见性

因此,如果 Events 集合上的文档对公众具有字段可见性,则任何用户都应该能够阅读该文档及其子集合。

到目前为止,我设法通过以下方式仅使事件集合中的文档可读:

   service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userID} {
      allow read, update, delete: if request.auth.uid == userID;
      allow create: if request.auth.uid != null;
        match /events/{eventID} {
          allow read: if resource.data.visibility == 'public';
          allow read, write, create, update, delete: if request.auth.uid == userID;
          match /activities/{activitytID} {
            allow read, write, create, update, delete: if request.auth.uid == userID;
            match /streams/{streamID} {
              allow read, write, create, update, delete: if request.auth.uid == userID;
            }
          }
        }

    }
  }
}

当一个事件文档的可见性是公开的时,如何使事件和流的嵌套集合也可读?

最佳答案

我通过以下方式解决了这个问题:

添加获取事件数据的函数

 function eventData() {
            return get(/databases/$(database)/documents/users/$(userID)/events/$(eventID)).data
          }

完整规则:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userID} {
      allow read, update, delete: if request.auth.uid == userID;
      allow create: if request.auth.uid != null;
        match /events/{eventID} {
          allow read: if resource.data.visibility == 'public';
          allow read, write, create, update, delete: if request.auth.uid == userID;
          function eventData() {
            return get(/databases/$(database)/documents/users/$(userID)/events/$(eventID)).data
          }
          match /activities/{activityID} {
            allow read: if eventData().visibility == 'public'
            allow read, write, create, update, delete: if request.auth.uid == userID;
            match /streams/{streamID} {
              allow read: if eventData().visibility == 'public'
              allow read, write, create, update, delete: if request.auth.uid == userID;
            }
          }
        }

    }
  }
}

关于firebase - Firestore 规则 : How to allow nested document read based on parent property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53959184/

相关文章:

firebase - 如何在Flutter中将Firebase Auth与Firestore结合使用

firebase - 有测试 firebase 存储安全规则的好方法吗?

android - 带有 ref 的 Firebase toObject

android - 添加 snapshotListener 时禁用第一个查询快照

firebase - documentSnapshot.data() 不传递给构建器 : Flutter

Firebase删除具有安全规则!data.exist的 child

javascript - 我需要什么规则才能允许访问 Firestore 中的用户数据?

firebase - Firestore 文档最大写入速率说明

Angular Firebase ngIf 用户经过身份验证监听器在页面重新加载时不起作用

java - Firebase 内容 - 如何从 onSuccess() 方法外部访问 taskSnapshot.getDownloadUrl()