【发布时间】:2017-04-01 10:21:17
【问题描述】:
我只希望那些用户阅读 uid 等于或等于 message 的 to 或 from 字段中的一个或两个的消息,或者如果消息的“to”部分设置为 @987654327 @。
这是我的数据库:
{
"intents" : {
"intentFields" : "",
"intentName" : "",
"to" : ""
},
"messages" : {
"-KViVgc7ZG051eMXP0-5" : {
"from" : "Ed",
"name" : "Ed",
"photoUrl" : "https://cdn1.iconfinder.com/data/icons/user-pictures/100/male3-512.png",
"text" : "Hello, I'm Ed",
"timeStamp" : "1476880306",
"to" : "all",
"type" : "text"
},
"-KWmsuvm0uJIf01eHfyN" : {
"from" : "Capyv3mxQsUUn2W1nPOgcJ0Ex9T2",
"name" : "Aakash Bansal",
"photoUrl" : "https://lh5.googleusercontent.com/-oQyA4HXVycc/AAAAAAAAAAI/AAAAAAAAHKo/Ov0A0p0LjiY/s96-c/photo.jpg",
"text" : "ho",
"timeStamp" : "1479396273",
"to" : "Ed",
"type" : "text"
}
}
}
我尝试了几条规则,并阅读了 Firebase 文档。但是没有一条规则帮助我实现了我的结果。
我尝试过的规则示例是:
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages" : {
"$message": {
".read": "auth.uid !== null",
".write": true
}
}
}
}
而以下规则集工作正常,虽然它们没有达到我想要的结果,但它们显示了应用程序中的所有消息。
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages" : {
".read": "auth.uid !== null",
"$message": {
".write": true
}
}
}
}
只是通知一下,我在 Android 应用程序中使用 firebaseui 来读取数据。如果我对“firebase 安全规则”的理解有问题,请告诉我。
编辑:即使以下规则集也不起作用:
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages": {
"$message": {
".read" : true,
".write": true
}
}
}
}
我用来读取数据的android代码是:
mFirebaseAdapter = new MessageAdapter(FriendlyMessage.class,
R.layout.message_my,
RecyclerView.ViewHolder.class,
mFirebaseDatabaseReference.child("/messages"));
【问题讨论】:
-
你如何阅读这些信息?因为如果您尝试阅读
/messages,那么这些规则确实会失败。这是一个常见的混淆来源,我们在文档和this answer here 中将其描述为rules are not filters。 -
你的代码在哪里?你实际上在读什么? (提示:Puf 上面的评论几乎可以肯定是答案,但没有代码就无法判断)
-
是的 puf,我已经将 firebaserecycleradapter 放在 /messages 中,我应该把它放在哪里来获取消息?
标签: firebase firebase-realtime-database firebase-security