5

I'm trying to figure out a way to model the following data in Firebase, and I'm not sure if it's possible.

Say I have a list of posts. Some of those I want to flag as private, where the author specifies members of their friends list can view it. Is this possible, and if so is it possible without defining posts and private posts separately.

cactusphone
  • 507
  • 4
  • 15

1 Answers1

5

You can specify which posts may be read and written using security rules. For example, if I have this data set:

{
   "users": {
      "me": {
          "friends" { "jack", "mary" }
      },
   },
   "posts": {
       "post1": {
            "owner": "me",
            ...
       }
   }
}

I could use a security rule like the following:

{
   "posts": {
       "$post_id": {
           // any friend can read my post
           ".read":  "auth.uid === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/friends/'+auth.uid).exists()",
           // only I can write it
           ".write": "auth.uid === data.child('owner').val()"
       }
   }
}

Keep in mind, however, that security rules can't be used as a filter. You can't iterate a list of posts and only expect to get back the ones friends are allowed to see--if it encounters items in the list that aren't readable, then the operation will fail to return results.

Rob DiMarco
  • 13,226
  • 1
  • 43
  • 55
Kato
  • 40,352
  • 6
  • 119
  • 149