我已经接管了一个项目,其中有以下数据模型在firestore:
/聊天/{聊天ID}/消息/{消息ID}
/Chat/{chatId}
{
users: ["24","51"]
messages: [ //collection
{message:"...", sender: 24, time:"...", users: ["24","51"]},
]
}
通过flutter我读聊天通过:
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection("Chat")
.doc(chatID.toString())
.collection("messages")
.orderBy("time", descending: false)
.snapshots(),
我需要确保只有参与聊天的两个用户才能阅读消息,但我很难做到这一点。
我当前的规则如下所示:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /Chat/{chatId}{
allow read: if "24" in resource.data.users //24 is being replaced with request.auth.uid when it works
allow write
match /messages/{messageId}{
allow read: if "24" in resource.data.users //24 is being replaced with request.auth.uid when it works
allow write
}
}
}
}
与上述,我得到一个权限错误时,试图通过flutter列出聊天,它给出了错误:
Chat/1103/messages failed: Missing or insufficient permissions.
如何编写一个规则,使只有两个用户能够读写邮件?
1条答案
按热度按时间ykejflvf1#
安全规则不会过滤数据,而只是确保代码不会尝试读取超过授权的数据。
由于您的规则规定代码只能读取用户是协作者的消息,因此您的代码也应该限制其读取此类消息。向查询添加条件:
有了这个附加条件,规则就可以看出您没有尝试读取超过允许的数据。