4bottiglie
4bottiglie

Reputation: 540

Firebase Realtime database Rules Groups with shared data

I need to create a system where there are many groups.

I have written the following rules, but my problem is that i can't give Group member permissions to read other users data with the or ending part:

root.child('Group').child($group_id).child('Members').hasChild(auth.uid)

simply because it can't work with $group_id .

I did not found any function that give me the possibility to check if there is a uid inside Groups from the UserData group. My constraint is that i already have UserData part and i can't change it, because already populated, but i need to define a Group parts around it. Should i structure this in an other way? Like having an other path that it's defining for every user, the groups that it may be in?

{
  "rules": {

    "Groups": {
      "$group_id": {

        ".read": "data.child('Members').child(auth.uid).exists()",
        ".write": "data.child(auth.uid).child('canChangeRegistry').val() == true",
        "Members": {
          ".read": "data.child(auth.uid).exists()",
          ".write": "data.child(auth.uid).child('canChangeRegistry').val() == true",
          "$uid": {
            ".read": "data.child(auth.uid).exists()",
            ".write": "auth.uid === $uid || data.child('canChangeRegistry').val() == true"
          }
        }
      }
    },

    "UserData": {
      "$uid": {
        ".read": "auth.uid === $uid || root.child('Groups').child($group_id).child('Members').hasChild(auth.uid)",
        ".write": "auth.uid === $uid "
      }
    },

  }
}

Upvotes: 1

Views: 80

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

There is no way to have the rules search through all groups for a membership match, as that wouldn't scale.

The only option I see is to store an additional list for each user with all other users that they share a group with, and then update that whenever you add users to groups and remove them.

With such a list in place, the lookup in security rules only depends on auth.uid and $uid, so can be something like:

root.child('UserBuddies').child(auth.uid).hasChild($uid)

Upvotes: 0

Related Questions