Java Firebase数据库规则父项跟踪子项

Java Firebase数据库规则父项跟踪子项,java,android,firebase,firebase-realtime-database,firebase-security,Java,Android,Firebase,Firebase Realtime Database,Firebase Security,我遇到了一个问题,我什么都试过了,但我找不到解决办法。我有一个像这样的firebase数据库。聊天: -聊天 -0 --头衔= --lastmsg= --时间戳= -1 --头衔= --lastmsg= --时间戳= 然后: -成员 -0 --uid0=真 --uid1=真 -1 --uid2=真 --uid3=真 现在,我有了这个java代码来处理这个列表 FirebaseDatabase database = FirebaseDatabase.getInstance(

我遇到了一个问题,我什么都试过了,但我找不到解决办法。我有一个像这样的firebase数据库。聊天:

-聊天 -0 --头衔= --lastmsg= --时间戳= -1 --头衔= --lastmsg= --时间戳=

然后:

-成员 -0 --uid0=真 --uid1=真 -1 --uid2=真 --uid3=真

现在,我有了这个java代码来处理这个列表

            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("chats");
             myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.

            Iterable<DataSnapshot> children = dataSnapshot.getChildren();

            for (DataSnapshot child : children) {
                String titolo = (String) child.child("titolo").getValue();
                String ultimomsg = (String) child.child("ultimomsg").getValue();
                Long timestamp = (Long) child.child("timestamp").getValue();
                Log.w(TAG, "Title is: "+ titolo);
                CHATITEMS.add(new DummyItem(child.getKey(), titolo, ultimomsg, timestamp));
            }
            RecyclerView recyclerView = (RecyclerView) view;
            recyclerView.setAdapter(new MyPersonRecyclerViewAdapter(CHATITEMS, mListener));
        }
现在,我希望用户仅当userid位于节点成员/chatnumber/userid上时才能够读取chats节点。我尝试了几种方法来制定规则,但都没有成功。谁能给我指出正确的方向吗?谢谢

这些文档提供了一个很好的指导起点,有助于理解应如何构造不同的规则以保护指定路径上的数据。在您的情况下,您希望根据/members/$chat\u id/$user\u id是否存在登录用户的uid来读取/chats/$chat\u id路径。为此,您的安全规则应如下所示:

{
  "rules": {
    "chat": {
      "$chat_id": {
        ".read": "root.child('members/' + $chat_id + '/' + auth.uid).exists()",
        ".write": false
      }
    }
  }
}
您可能希望.write规则为false,以防通过写入该数据库路径,因此您可以根据发送新聊天信息的时间更新为相应的值