【问题标题】:Firestore rules: How to allow nested document read based on parent propertyFirestore 规则:如何允许基于父属性读取嵌套文档
【发布时间】:2018-12-28 13:14:13
【问题描述】:

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

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

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

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

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

   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;
            }
          }
        }

    }
  }
}

当一个事件文档的可见性是公开的并且活动和流的嵌套集合也是可读的时,我怎样才能使?

【问题讨论】:

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


    【解决方案1】:

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

    添加获取事件数据的函数

     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;
                }
              }
            }
    
        }
      }
    }
    

    【讨论】:

    • 为我工作。我读到 get() 每次都会花费你一读。你还在用这个方法还是在新的安全规则中有更好的方法?
    • 不使用这个atm
    • 在这里向其他人说明一下:通过规则在数据库上完成获取内容时,您不会被收取费用
    • @JimmyKane 不,你肯定在 Firestore 中这样做。 Read firebase.google.com/docs/firestore/security/rules-conditions :“使用这些函数会在您的数据库中执行读取操作,这意味着即使您的规则拒绝请求,您也需要为读取文档付费。请参阅 Cloud Firestore 定价了解更多具体的计费信息。”
    • @JimmyKane:这个答案与 Firebase 实时数据库有关。 Firestore 对安全规则中的 get() 收费。
    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多