如何仅授予对Firebase中具有特定值的数据的读取权限?

时间:2015-12-01 17:01:38

标签: firebase firebase-security

以下是Firebase中的数据结构:

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

我想只允许在列表范围内读取状态为0的wall的项目。我的意思是获取wall的项目列表,其中status为0;

所以我的规则是:

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

出了什么问题?

编辑:

我已经使用模拟器检查了,我无法访问列表,但我可以访问项目本身(其中status == 0)

1 个答案:

答案 0 :(得分:3)

您的规则是正确的,但仅针对单个项目。

允许此读取:https://<my-firebase-app>/wall/-cxwcxwcxw 此读取被拒绝:https://<my-firebase-app>/wall/-cxfdsqcxw

enter image description here

但Firebase数据库不会过滤掉无法访问的项目。如果用户无法读取列表中的一个项目,则无法读取整个列表。

解决这个问题的方法是将无法访问的项目(status === 1)移动到它自己的位置。

{
  "validWall": {
    "-cxwcxwcxw" : {
        "name" : "hello",
        "status" : 0
    },
    "-cxwfdscxw" : {
        "name" : "stack",
        "status" : 0
    },
   },
   "invalidWall": {
    "-cxfdsqcxw" : {
        "name" : "overflow",
        "status" : 1
    }
   }
}
相关问题