Firestore collectionGroup 安全规则,验证父文档值

时间:2021-03-04 14:20:21

标签: google-cloud-firestore firebase-security

我正在尝试的是检查父帖子可见性的值是否为“公开”以拒绝读取:

match /{postId=**}/subPostsById/{subPostId} {
    allow get, read: if 
      get(/databases/$(database)/documents/postsById/$(postId)).data.visibility == 'public';
}

(当我将 if 设置为 'true' 时它有效,所以路径是正确的)

但它一直让我拒绝访问。

对于父帖子,它的工作方式如下:

  match /postsById/{postId} {
      allow get, read: if 
        resource.data.visibility == 'public'
  }

有谁知道这是否可能?或者有什么问题? 我想避免每次将父帖子设置为“公开”时都必须遍历每个子帖子的 visibility 属性

编辑:此规则适用于单独获取 subPost 时,那么为什么不适用于 subCollection?:

match /subPostsById/{subPostId} {
   match /postsById/{postId} {
     allow get, read: if 
       get(/databases/$(database)/documents/postsById/$(postId)).data.visibility == 'public';        
   }
}

Edit2:这是最小的 firestore.rules 示例,仍然没有按预期工作:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

     match /{postId=**}/subPostsById/{subPostId} {
       allow get, read, list: if
        get(/databases/$(database)/documents/postsById/$(postId)).data.visibility == 'public';
     }

  }
}

(当我做 allow get, read, list: true 时它会正确读取,所以路径是正确的,规则在这里不起作用)

我正在做的查询:

      const fbPath = firestore
        .collectionGroup('subPostsById')
        .where('title', '>=', searchQuery)

      fbPath.get().then(() => {
         // etc... "FirebaseError: Missing or insufficient permissions."
      })

0 个答案:

没有答案