1

Here is a data structure in Firebase :

{
  "wall" : {
    "-cxwcxwcxw" : {
        "name" : "hello",
        "status" : 0
    },
    "-cxwfdscxw" : {
        "name" : "stack",
        "status" : 0
    },
    "-cxfdsqcxw" : {
        "name" : "overflow",
        "status" : 1
    }
  }
}

I want to only allow reading wall's item where status is 0, in the list scope. I mean get the list of wall's item that where status is 0;

So my rules are :

{
  "wall": {
    "$wall_item_id": {
       ".read": "data.child('status').val() === 0"
    }
  }
}

What is going wrong ?

EDIT :

I have checked with the Simulator, and I cannot get access to the list, but I can access to the items itself (where status == 0)

Anthony
  • 3,989
  • 2
  • 30
  • 52

1 Answers1

3

Your rules are correct, but only for individual items.

This read is allowed: https://<my-firebase-app>/wall/-cxwcxwcxw This read is denied: https://<my-firebase-app>/wall/-cxfdsqcxw

enter image description here

But the Firebase Database does not filter out the inaccessible items. If one item in the list can't be read by the user, the entire list can't be read.

A way around this is to move the inaccessible items (status === 1) to it's own location.

{
  "validWall": {
    "-cxwcxwcxw" : {
        "name" : "hello",
        "status" : 0
    },
    "-cxwfdscxw" : {
        "name" : "stack",
        "status" : 0
    },
   },
   "invalidWall": {
    "-cxfdsqcxw" : {
        "name" : "overflow",
        "status" : 1
    }
   }
}
David East
  • 31,526
  • 6
  • 67
  • 82
  • Yes your are right, the item can be accessed but not the list of item. I think this will be difficult – Anthony Dec 01 '15 at 18:14
  • Ah I see. Yes, the Firebase Database does not filter out the inaccessible items. If one item in the list can't be read by the user, the entire list can't be read. – David East Dec 01 '15 at 18:19
  • Let me know if my answer works. It's good to keep the unanswered queue clear. – David East Dec 01 '15 at 20:25
  • OK thank, please put the fact that this is not possible to list part of item with security rules, this is the answer here. And I will accept it. – Anthony Dec 02 '15 at 09:02
  • No problem! Just did and also gave an suggestion of how you might be able to get around this. – David East Dec 02 '15 at 12:44