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

How only grant read access to data that own specific value in Firebase?

这是 Firebase 中的一个数据结构:

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

我只想在列表范围内允许阅读墙的状态为 0 的项目。我的意思是获取状态为 0 的墙的项目列表;

所以我的规则是:

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

出了什么问题?

编辑:

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

您的规则是正确的,但仅适用于个别项目。

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

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

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

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