如何使用EmberFire保护我的Firebase App版本

时间:2016-02-24 13:44:14

标签: ember.js firebase firebase-security emberfire

我抓住了我的头,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中的最低规则,所有用户都可以看到所有对话。

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

编辑:在规则之前添加auth!== null,使错误beginWith()期望字符串参数输出。但这两条规则仍无效。

1 个答案:

答案 0 :(得分: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)"
       }
    }