I want only those users to read the messages whose uid
is equal to either or both the to
or from
fields of message
or if the 'to' section of the message is set to all
.
This is my database:
{
"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"
}
}
}
I tries several rules, and read the Firebase documentation too. But none of the rules helped me achieve my result.
An example of the rules which I tried is :
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages" : {
"$message": {
".read": "auth.uid !== null",
".write": true
}
}
}
}
whereas the following set of rules works fine, although they don't achieve my desired result but they display all the messages in the app.
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages" : {
".read": "auth.uid !== null",
"$message": {
".write": true
}
}
}
}
Just to inform, I'm using firebaseui in the Android app to read the data. Please tell me if something is wrong in my understanding of 'firebase security rules'.
EDIT : Even the following set of rules are not working:
{
"rules": {
"intents" : {
".read" : "auth.uid === data.child('to').val()",
".write" : true
},
"messages": {
"$message": {
".read" : true,
".write": true
}
}
}
}
The android code that I use to read the data is:
mFirebaseAdapter = new MessageAdapter(FriendlyMessage.class,
R.layout.message_my,
RecyclerView.ViewHolder.class,
mFirebaseDatabaseReference.child("/messages"));