【发布时间】:2016-06-25 14:06:27
【问题描述】:
我刚刚开始使用 Firebase。
我正在使用其中一个 quickstart-js 模板。
我正在尝试添加 .read 安全规则,以便用户只能阅读创建的帖子。
我有点困惑,因为如果我针对 auth.id 进行测试,规则会通过,但如果我针对 auth.uid 进行测试,则会失败(我知道 auth.uid 是正确的)。
在模拟器中,这条规则失败了:
{
"rules": {
// ".read": "auth != null",
".write": "auth != null",
"posts": {
"$post": {
".read": "data.child('uid').val() === auth.uid",
}
}
}
}
这是数据库结构:
{
"posts" : {
"-KL6ecPO67H5B8SmPHJr" : {
"body" : "Just testing out the firebase system.",
"starCount" : 0,
"title" : "Camp Se has it's first blog post",
"uid" : "vaZrC0sSdYTiou587IuePVw9uRT2"
},
"-KL6fUBOhsc4zosJbwjb" : {
"body" : "Another post ja ja ja\n",
"starCount" : 0,
"title" : "Another post",
"uid" : "vaZrC0sSdYTiou587IuePVw9uRT2"
}
},
"user-posts" : {
"vaZrC0sSdYTiou587IuePVw9uRT2" : {
"-KL6ecPO67H5B8SmPHJr" : {
"body" : "Just testing out the firebase system.",
"starCount" : 0,
"title" : "Camp Se has it's first blog post",
"uid" : "vaZrC0sSdYTiou587IuePVw9uRT2"
},
"-KL6fUBOhsc4zosJbwjb" : {
"body" : "Another post ja ja ja\n",
"starCount" : 0,
"title" : "Another post",
"uid" : "vaZrC0sSdYTiou587IuePVw9uRT2"
}
}
},
"users" : {
"vaZrC0sSdYTiou587IuePVw9uRT2" : {
"email" : "user@gmail.com"
}
}
}
模拟结果详情:
Simulation results
Type read
Location /posts/KL6ecPO67H5B8SmPHJr/
Data null
Auth { "provider": "google", "uid": "vaZrC0sSdYTiou587IuePVw9uRT2" }
Admin false
edit Read denied
close
Line 7 (/posts/KL6ecPO67H5B8SmPHJr)
read: "data.child('uid').val() === auth.uid"
【问题讨论】:
-
您在问题中包含了 JSON 树的图片。请将其替换为实际的 JSON 作为文本,您可以通过单击 Firebase 数据库控制台中的导出按钮轻松获取该文本。将 JSON 作为文本使其可搜索,使我们能够轻松地使用它来测试您的实际数据并在我们的答案中使用它,通常只是一件好事(tm)。
-
当您打开错误消息的详细信息时,它会说什么。作为文本也将比作为屏幕截图更有用。
标签: firebase firebase-realtime-database firebase-security