如何授予对Firebase中拥有特定价值的数据的读取权限? [英] How only grant read access to data that own specific value in Firebase?

查看:319
本文介绍了如何授予对Firebase中拥有特定价值的数据的读取权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Firebase中的数据结构:

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



code
$ b我只想允许阅读在列表范围内状态为0的墙的项目。我的意思是获得状态为0的墙的物品列表;



所以我的规则是:

<$ p $ ($'$'$'$'$'$'$'$'$'$'$'$'$'$'$'$' === 0
}
}
}

什么是错误的?

编辑:

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

解决方案

您的规则是正确的,但只限于个人项目。



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





但Firebase Databas e不会过滤不可访问的项目。如果列表中的一个项目不能被用户读取,则不能读取整个列表。

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

  {
validWall:{
-cxwcxwcxw:{
name :hello,
status:0
},
-cxwfdscxw:{
name:stack,
status :
},
},
invalidWall:{
-cxfdsqcxw:{
name:overflow,
状态: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)

解决方案

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

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
    }
   }
}

这篇关于如何授予对Firebase中拥有特定价值的数据的读取权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆