I have an app where I have data objects and these objects can be read/write either by single users or by group of users. I'm trying to figure out what is the best approach to write a security rules for such a logic and best way to structure my real time DB.
I have tried to look for a solution but I have never found my exact use case. Here is example of my DB:
{
"data": {
"data1": {
"id": "data1",
"name": "Some data",
"content": "Some content...",
"visibility": "private",
"owner": "user1"
},
"data2": ...
},
"dataPermissions": {
"data1": {
"user1": "read",
"user2": "write",
"group1": "write"
},
"data2": {...}
},
"groupMembers": {
"group1": {
"user3": true,
"user4": true
}
},
"userGroups": {
"user4": {
"group1": true
}
...
}
}
My question is how to setup security rules to check whether user can read/write to data node? I know how to check for a single user. But I can't figure out how to check for the group. How to check in security rules that eg. user4 can read/write to data1 node? Since I can have several groups per user and several groups per data nodes I can't really address it with dynamic variable $uid.
Please note that I'm open to changes in db structure. I'm more curious how to deal with this problem in general without to much of duplicating datas (like saving all users from a group to dataPermissions under group id node) in order to keep DB maintenance sane.
Here is how I check for user permissions in security rules:
"data": {
"$dataId": {
".read": "data.child('owner').val() == auth.uid || data.child('visibility').val() == 'public' || root.child('dataPermissions/' + $dataId + '/' + auth.uid).exists()",
".write": "false"
}
},
Thanks for help.