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

时间:2015-07-06 15:38:25

标签: firebase firebase-security

我正在使用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) {});

我该如何解决?

由于

1 个答案:

答案 0 :(得分:2)

您的查询从此处开始:

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

这是一种相当典型的数据非规范化。