【发布时间】:2016-03-12 05:11:22
【问题描述】:
我在使用 firebase 的安全规则时遇到了问题,而且我不是 100% 会出错。我在想也许我的数据结构有误:
{
"users": {
"uid": {
"displayName": "Name";
}
},
"modules": {
"id": {
"title": "buttons",
"uid": "(user id string)"
},
"id": {
"title": "navbars",
"uid": "(user id string)"
}
},
"snippets": {
"id = moduleID": {
"id (of snippet)": "(id string)" {
"uid (user ID)": "(string)",
"body": {
"css": "(some code)",
"html": "(Some code)",
"name": "(string)",
"description": "(string)"
}
}
}
}
应用程序中的一切工作正常,但是当我开始添加安全规则时,我收到了拒绝访问错误。我只是想知道我的数据结构一开始是正确的还是安全规则完全错误?
安全规则:
{
"rules": {
"users": {
"$uid": {
// grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth != null && auth.uid == $uid",
".read": "auth != null && auth.uid == $uid"
}
},
"snippets": {
"$uid": {
// grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth != null && auth.uid == $uid",
".read": "auth != null && auth.uid == $uid"
}
},
"modules": {
"$uid": {
// grants write and read access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth != null && auth.uid == $uid",
".read": "auth != null && auth.uid == $uid"
}
}
}
任何建议将不胜感激。
【问题讨论】:
-
您在哪里看到拒绝访问错误?你能发布一个拒绝读/写的代码的sn-p吗?
-
该应用程序使用正常的基本规则,但当使用新规则更新时,它会完全中断。控制台给出此错误错误:permission_denied:客户端没有访问所需数据的权限。在错误(本机)
-
尝试应用仪表板中的模拟器选项卡;这使您可以分析规则如何应用于任何读/写操作,而无需进行身份验证。
-
auth != null &&很有价值,但在这里技术上是多余的。auth.uid === $uid检查将短路并且仍然评估为假。在这种情况下,这只是一个细微差别,但却是一个需要理解的重要概念,因为当您尝试在具有||的复杂规则中使用auth.uid时,它可能会咬到您,它可能会短路并避免评估 ||标准。 -
@kato 感谢您的意见,我会注意的。我完全迷失了这一点,刚刚开始再次阅读文档并阅读 NoSql。我认为我需要更好地理解这些概念,而不是试图强迫它发挥作用。干杯:)
标签: firebase firebase-security firebase-realtime-database