gpt4 book ai didi

java - Firebase 数据库规则 parent 跟踪 child

转载 作者:行者123 更新时间:2023-12-02 11:41:36 25 4
gpt4 key购买 nike

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

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

然后:

-成员
--0
---uid0="true"
---uid1="true"
--1
---uid2="true"
---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 上时才能够读取聊天节点。我尝试了多种方法来制定规则,但都没有成功。有人能指出我正确的方向吗?谢谢

最佳答案

Firebase user security文档提供了一个很好的指导起点,有助于理解如何构建不同的规则来保护指定路径上的数据。在您的情况下,您希望根据登录用户的 uid 是否存在于 /members/$chat_id/$user_id 中来读取 /chats/$chat_id 路径。为了实现这一点,您的安全规则应该是这样的:

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

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

关于java - Firebase 数据库规则 parent 跟踪 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com