【问题标题】:Firebase database rules parents shadowing childrenFirebase 数据库规则父母跟踪孩子
【发布时间】:2018-01-29 22:53:33
【问题描述】:

我遇到了问题,我尝试了所有方法,但找不到解决方案。我有一个这样的firebase数据库。聊天:

-聊天
--0
---title=""
---lastmsg=""
---timestamp=""
--1
---title=""
---lastmsg=""
---timestamp=""

然后:

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

现在,我有这个 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));
        }

现在,我希望用户只有在用户 ID 位于节点成员/聊天编号/用户 ID 上时才能读取聊天节点。我尝试了几种方法来制定规则,但没有成功。谁能指出我正确的方向?谢谢

【问题讨论】:

    标签: java android firebase firebase-realtime-database firebase-security


    【解决方案1】:

    Firebase user security 文档提供了一个很好的指导起点,可帮助您了解应如何构建不同规则以保护指定路径上的数据。在您的情况下,您希望 /chats/$chat_id 路径可读,基于登录用户的 uid 是否存在于/members/$chat_id/$user_id。为此,您的安全规则应如下所示:

    {
      "rules": {
        "chat": {
          "$chat_id": {
            ".read": "root.child('members/' + $chat_id + '/' + auth.uid).exists()",
            ".write": false
          }
        }
      }
    }
    

    您可能希望 .write 规则为 false,以防您有 Cloud function 通过 Admin SDK 写入该数据库路径,因此您可以根据何时发送新聊天消息更新为适当的值.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      • 2016-11-16
      • 2021-11-09
      相关资源
      最近更新 更多