Firebase数据库安全规则,仅使用Android

时间:2017-01-05 14:39:03

标签: android firebase firebase-realtime-database

我想创建一个聊天支持应用模型。我只有1个代理和许多请求用户询问查询。每个用户可以与代理进行多次对话(假设它像一个只有2个成员的whatsapp组,用户和该代理)。

所以我创建了一个数据结构,如:

Conversation
-uId
-status
-conversationTitle

和消息,

Message
-conversationId
-message
-from //0 mean it from me to my friend and 1 for a message from my friend to me
-timestamp

基本结构如下:

Conversation1:{ "uId": "1", "status":0, "conversationTitle": "First conversation"},
Conversation2:{ "uId": "2", "status":0, "conversationTitle": "Second conversation"},
Conversation1:{ "uId": "1", "status":0, "conversationTitle": "Third conversation"}

和消息:

    Message1: {"conversationId": "Conversation1", "message": "Hello there 1!", "from":0, "timestamp":******** },
    Message2: {"conversationId": "Conversation1", "message": "Hello there 2!", "from":0, "timestamp":******** },
    Message3: {"conversationId": "Conversation2", "message": "Hello there 3!", "from":0, "timestamp":******** },
    Message4: {"conversationId": "Conversation3", "message": "Hello there 4!", "from":0, "timestamp":******** },

正如您所见,消息1和2属于会话1,而会话1又属于具有uID 1的用户1。

我希望用户1(uId:1)和用户2(uID:2)发生的所有对话都不能看到1的对话。

我写的安全性为:

"conversations":{
  "$uid":{
    ".read": "data.child('uId').val() == auth.uid",
    ".write":"auth != null && newData.child('uId').val() == auth.uid"
  }
}

然后在Android中:

FirebaseDatabase database = FirebaseDatabase.getInstance();
Query conversationsRef = database.getReference("conversations");

FirebaseRecyclerAdapter<Conversation, ConversationHolder> binderData = new FirebaseRecyclerAdapter<Conversation, ConversationHolder>(Conversation.class, R.layout.item_butler_converastions, ConversationHolder.class, conversationsRef) {
                    @Override
                    public void populateViewHolder(ConversationHolder chatMessageViewHolder, Conversation chatMessage, int position) {
                        chatMessageViewHolder.setName(chatMessage.getTitle());
                    }

                    @Override
                    protected void onCancelled(DatabaseError error) {
                        super.onCancelled(error);
                    }
                };

当我这样做时,我得到了许可错误。我想检索属于用户1的所有会话。

我也尝试过使用:

Query conversationsRef = database.getReference("conversations").orderByChild("uId").equalTo(mAuth.getCurrentUser().getUid());

但没有奏效。提前谢谢。

0 个答案:

没有答案