Firebase 规则 - 无法访问每个节点

Firebase rules - Cannot access to each node

我正在使用 Firebase 开发应用程序。我在 Firebase 上有以下数据结构:

{
   rules: {
      "version": {
         "threads": {
            "$thread": {
               "noticeList": {
                   ".read": true,
                   ".write": true
               },
               "messages": {
                   ".read": "root.child('version/threads/' + $thread + '/users/' + auth.uid).exists()",
                   ".write": "root.child('version/threads/' + $thread + '/users/' + auth.uid).exists()"
               },
               "$other": {
                    ".read": "auth != null",
                    ".write": "auth != null"
               }
            }
         }
     }
}

每个线程都有子节点/users/,我想制定一个规则,只有包含的用户才能读取和写入该线程中的消息。

添加新消息时,我还必须访问 noticeList 的另一个条件。但是,这些规则不起作用。如果'$thread'中还有其他节点规则,我使用

时无法得到任何响应
firebase.child("version/threads/" + $thread + "/noticeList").once("value", function(snapshot) {});

firebase.child("version/threads").once("value", function(snapshot) {});

我该如何解决?

谢谢

您的查询从这里开始:

firebase.child("version/threads/" + $thread + "/noticeList")

执行此查询时,Firebase 会检查用户是否有权读取该节点。在这种情况下,用户没有,因此查询失败。

这是人们在涉及 Firebase 安全规则时常犯的错误。 Firebase 文档将其解释为“rules are not filters”,这正是您想要做的。

不幸的是,对于您的场景,文档还解释了“rules cascade”。这实质上意味着,如果您授予用户对 noticeList 的读取权限,则无法在较低级别取消读取权限。

实现此功能的最简单方法可能是为每个用户提供他们自己可以访问的通知索引。

version
  threads
    $thread
      userNoticeList
        <userid1>
          <messageid1>: true
          <messageid2>: true
        <userid2>
          <messageid1>: true
          <messageid3>: true

这是一个相当典型的数据反规范化。