I am trying to create a security rule that allows any user in a group to read the information of any other user in the same group. In other words a user should be able to read the user information of any user that belongs to a common group.
This is what I have:
{
"rules": {
"users": {
"$user_id": {
// Any user beloging to at least one group in common should be able to read
".read": "$user_id === auth.uid || root.child('users/' + $user_id + '/groups').hasAny(root.child('users/' + auth.uid + '/groups'))",
".write": "$user_id === auth.uid",
"groups": {
"$group_id": {
".validate": "root.child('groups/' + $group_id).exists() && newData.isBoolean()"
}
}
}
},
"groups": {
"$group_id": {
"name": { ".validate": "newData.isString() && newData.val().length > 0 && newData.val().length < 50" }
}
},
"members": {
"$group_id": {
".read": "root.child('members/' + $group_id + '/' + auth.uid).exists()",
".validate": "root.child('groups/' + $group_id).exists()",
"$user_id": {
".write": true, // Skipped for brevity
".validate": "root.child('users/' + $user_id).exists() && newData.isBoolean()"
}
}
},
}
}
}
Of course, the hasAny
function is not part of the API. Is there any way to do this with the existing API? Are there any plans to add something like this?