0

我为 Firebase 安全工作如我所愿而挠头。我的应用是用 Ember 和 EmberFire 构建的。所以结构是由 EmberFire 决定的。

我的数据库结构如下:

conversations : {
    $conversation_id {
        messages {
            //message data
        }
        users {
            $user_id1 :true
            $user_id2 :true
        }
    }
}

我想要的是只有参与对话的用户才能在此对话中查看和编写消息。我尝试了这条规则但没有成功:

"conversations" : {
    ".indexOn" : "notify",
    ".read" : "auth !== null && root.child('users').hasChild(auth.uid)", 
    ".write": "auth !== null && root.child('users').hasChild(auth.uid)"
}

似乎 auth.uid 不能传递给 hasChild。我还尝试了以下方法,因为我的对话 id 是参与对话的用户 id 的加入:

"conversations" : {
   ".indexOn" : "notify",
   "$conversation" : {
       ".read" : "auth !== null && ($conversation.beginsWith(auth.uid) || $conversation.endsWith(auth.uid))", 
       ".write": "auth !== null && ($conversation.beginsWith(auth.uid) || $conversation.endsWith(auth.uid))"
   }
}

使用此规则,没有人可以看到对话,因为“对话”节点没有 .read 规则。但是,如果我将“.read : true”添加到“对话”节点,由于 Firebase 中的上下规则,所有用户都可以看到所有对话。

编辑:第二条规则与第一条有相同的问题。beginWith() 需要一个字符串参数。而且 auth.uid 不能用来解决我的问题有什么想法吗?

编辑:在规则之前添加 auth !== null ,使得错误 beginWith() 需要一个字符串参数出来。但是这两个规则仍然不起作用。

4

1 回答 1

4

您第一次尝试的问题是您使用的是 root,但应该一直使用 $conversation。Root 是一个规则快照,它引用了 firebase 的根目录,而 $conversation 是一个变量,用于保存您尝试读/写的对话的 id。

"conversations" : {
       ".indexOn" : "notify",
       "$conversation" : {
           ".read" : "auth !== null && root.child('conversations/'+$conversation+'/users').hasChild(auth.uid)", 
           ".write": "auth !== null && root.child('conversations/'+$conversation+'/users').hasChild(auth.uid)"
       }
    }
于 2016-02-24T16:01:12.220 回答