如何设置真正的防火基地;时间数据库规则根据数据



我想将我的聊天数据设为私有数据,并且只能由登录用户访问。

我不知道该制定防火区规则。我的数据结构如下所示。

{
"chat" : {
"-L1223434cI8qG1eLQUyj" : {
"User" : {
"_id" : "66b2-4bac-abe1-fe89a0e29a28",
"name" : "aaaabbbccc"
},
"friendID" : "8bcd-4b62-bb4e-25b7c1df4ca2",
"text" : "hello",
"timestamp" : 1573113592492
}

我想将_id与我在应用程序下拥有的所有经过身份验证的用户进行比较,并希望为只使用他/她的id的用户提供访问权限。

您可以使用以下安全规则来实现所需的结果,这些规则利用内置的auth变量来保存当前登录用户的信息:

{
"rules": {
"chat": {
"$msg_id": {
// grants write access to the owner of this message
// whose uid must exactly match the value of _id
".write": "data.child("User/_id").val() === auth.uid",
// grants read access to the owner or recipient of this
// message whose uid must exactly match the saved uids
".read": "data.child("User/_id").val() === auth.uid || data.child("friendID").val() === auth.uid"
}
}
}
}

我建议阅读"保护数据安全"one_answers"基于用户的安全"文档,以获得更多详细信息,这些详细信息以比在这里重复更好的方式解释这些概念。

最新更新