【发布时间】:2017-06-13 12:42:54
【问题描述】:
我想创建一个 firebase 规则,如果该子项的属性具有特定值,则该规则允许用户读取和写入子项。假设我的数据库如下所示:
"tasks": {
"SDkh7s62jnd23d9": {
"uid": "someuserid",
"other": "datagoes here"
}
}
这是我当前的安全规则:
"tasks": {
".indexOn": "_state",
"$registerQueueKey": {
".read": "data.child('uid').val() == auth.uid",
".write": "newData.child('uid').val() == auth.uid"
}
}
此规则限制用户写入权限,但它绝不允许用户读取,即使他们尝试读取的子级具有等于 auth.id 的 uid 属性。
然后我测试了以下规则集:
"tasks": {
".indexOn": "_state",
"$registerQueueKey": {
".read": true,
".write": "newData.child('uid').val() == auth.uid"
}
}
尽管将.read 权限设置为永久true 值,用户仍然无法读取$registerQueueKey 子级。
然后我测试了以下规则集:
"tasks": {
".indexOn": "_state",
".read": true,
"$registerQueueKey": {
".write": "newData.child('uid').val() == auth.uid"
}
}
现在我可以很好地阅读孩子,所以我尝试了这个最终的安全规则:
"tasks": {
".indexOn": "_state",
".read": "data.child($registerQueueKey").child('uid').val() == auth.uid",
"$registerQueueKey": {
".write": "newData.child('uid').val() == auth.uid"
}
}
但此规则会引发错误,因为变量 $registerQueueKey 在它正在使用的范围内未定义。
如何完成这样的规则?
【问题讨论】:
-
Firebase 在您附加侦听器的位置检查读取授权。因此,您要么可以从该位置读取(从而读取其下的所有内容),要么您无法从该位置读取。这意味着您不能使用安全规则来过滤您在此处尝试的内容。这在文档中称为rules are not filters,在this answer 和these other questions 中。
-
@FrankvanPuffelen 那么我是否需要重组我的数据库,以便我尝试检查的值是那个孩子的关键?
-
那将是一种确实可行的模型。
标签: firebase firebase-realtime-database firebase-security