我只希望那些用戶讀取uid
等於或from
字段message
或郵件的「至」部分設置爲all
的郵件。
這是我的數據庫:我應該使用哪些Firebase安全規則來閱讀郵件?
{
"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"
}
}
}
我會嘗試一些規則,太閱讀火力地堡文檔。但是沒有任何規則幫助我實現我的結果。
的,我試過的規則的一個例子是:
{
"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
}
}
}
}
只是爲了告知,我使用firebaseui在Android應用中讀取數據。請告訴我,如果我對「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',它確實會因這些規則而失敗。這是一個常見的混淆來源,我們在文檔和[這裏的答案中描述爲[規則不是過濾器](https://firebase.google.com/docs/database/security/securing-data#rules_are_not_filters) ](http://stackoverflow.com/a/14298525/209103)。 –
你的代碼在哪裏?你究竟在讀什麼? (提示:上面的puf的評論幾乎肯定是答案,但不能說沒有代碼) – Kato
是puf,我已經把firebaserecycleradapter放在/ messages,我應該在哪裏得到這些消息? –