3

In my project I have a chat feature to allow users to speak in private message. For now it's only one-to-one, but it could be improved later to allow group discussion.

Currently I'm struggling with custom rules. Indeed, for my projet I need users to have theirs own list of discussion. For example, user A and B talk through private message, but user C, D or whatever shouldn't be able to read the discussion.

Here is how the Database json look like :

{
"room-messages": {
    "-KWgoXt567vzgxZ-1gii": {
        "-KWgoXt567vzgxZ-1gii": {
            "name": "Friendly Chat",
            "sent": 1479294463723,
            "text": "Nice ! You have created a new chat",
            "uid": "user_A_id"
        },
        "-KWh5_W12qsXFaJhyOvx": {
            "name": "Lucien Guimaraes",
            "sent": 1479294463728,
            "text": "A text message",
            "uid": "user_B_id"
        }
    },
    "-KWgoXt567vzgxZ-1git": {
        "-KWgoXt567vzgxZ-1git": {
            "name": "Friendly Chat",
            "sent": 1479294463723,
            "text": "Nice ! You have created a new chat (2)",
            "uid": "user_A_id"
        },
        "-KWh5_W12qsXFaJhyOvz": {
            "name": "Lucien Guimaraes",
            "sent": 1479294463729,
            "text": "Test",
            "uid": "user_C_id"
        }
    }
},
"room-metadata": {
    "-KWgoXt567vzgxZ-1gii": {
        "users": {
            "user_A_id": "Lucien Guimaraes",
            "user_B_id": "Geralt of Rivia"
        }
    },
    "-KWgoXt567vzgxZ-1git": {
        "users": {
            "user_A_id": "Lucien Guimaraes",
            "user_C_id": " Gordon Freeman"
        }
    }
}
}

For your information "user_A_id" or "user_B_id" should be the id provided by Firebase Authentication. In this example I want user A to get all rooms (because he's in both available room). User B should have only the first room and user B only the last room.

Here are my rules :

Firebase rules for my project

I have been able to allow write access for Rooms almost perfectly (the only remaining issue is user who can't delete a message, I don't know why). But for Read I have a huge issue : I can't set a custom rule because the value "$roomId" is unknown inside "room-messages". It's only possible to do this as a child of "$roomId".

Is there any solution for what I want implemented ?

Thanks !

#AskFirebase

Guimareshh
  • 1,214
  • 2
  • 15
  • 26
  • 1
    You're trying to give users access to some rooms, but not to others. Rules cannot be used to filter data in that way. See https://firebase.google.com/docs/database/security/securing-data#rules_are_not_filters and http://stackoverflow.com/a/14298525/209103 – Frank van Puffelen Nov 17 '16 at 14:33
  • Thanks for your reply, I'm going to check the post. I hope I will find a solution ! – Guimareshh Nov 18 '16 at 12:15
  • please see this thread https://gist.github.com/katowulf/4741111 – Nikos Oct 19 '20 at 15:59

0 Answers0