【发布时间】:2017-11-28 14:14:08
【问题描述】:
我有这个 Firebase 数据库,如有必要可以更改:
该数据库的 JSON 是:
{
"groups": {
"1": {
"name": "G1",
"points": {
"1": {
"name": "p1"
}
},
"visits": {
"1": {
"name": "v1"
}
},
"areas": {
"1": {
"name": "a1"
}
},
"waypoints": {
"1": {
"name": "w1"
}
},
"interests": {
"1": {
"name": "i1"
}
}
},
"2": {
"name": "G2",
"points": {
"2": {
"name": "p2"
}
},
"visits": {
"2": {
"name": "v2"
}
},
"areas": {
"2": {
"name": "a2"
}
},
"waypoints": {
"2": {
"name": "w2"
}
},
"interests": {
"2": {
"name": "i2"
}
}
}
},
"users": {
"qdRw1khg1ZO1s52YioYCdM4WrD02": {
"firstName": "AAAA",
"lastName": "BBB",
"email": "sdf@sdfs.com"
},
"h3KYDXkPQrY246w6Y6NXIanVoNS2": {
"firstName": "FF",
"lastName": "RR",
"email": "wwf@jjuzhz.com"
}
},
"userGroups": {
"qdRw1khg1ZO1s52YioYCdM4WrD02": {
"1": "admin",
"2": "readwrite"
},
"h3KYDXkPQrY246w6Y6NXIanVoNS2": {
"1": "admin",
"2": "readonly"
}
}
}
我想定义规则来完成以下任务:
- 每个人都可以创建一个新组
- 只有群组的用户才能读取群组数据
- 只有组的“管理员”可以将数据写入组本身,添加用户和更改组数据的子级别,但
- “readwrite”组用户可以写入子级别“points”和“visits”
- “只读”组用户根本不能写
我有:
"groups": {
"$groupId": {
".read": "root.child('userGroups').child(auth.uid).child($groupId).exists()",
".write": "! root.child('userGroups').child(auth.uid).child($groupId).exists() ||
(data.parent().val() === 'points' && root.child('userGroups').child(auth.uid).child($groupId).val() != 'readonly') ||
(data.parent().val() === 'visits' && root.child('userGroups').child(auth.uid).child($groupId).val() === 'readonly') ||
(data.parent().val() != 'points' && data.parent().val() != 'visits' && root.child('userGroups').child(auth.uid).child($groupId).val() === 'admin')"
}
},
"users": {
"$userId": {
".read": "auth != null",
".write": "auth != null &&
$userId === auth.uid &&
newData.val() != null"
}
},
"userGroups": {
"$userId": {
".read": "auth != null",
".write": "auth != null &&
data.child(auth.uid).val() === 'admin' &&
newData.val() != null"
}
}
但这不起作用,因为
data.parent().val()
不返回父级的名称字符串。所以我做不到
data.parent().val() != 'points'
如何解决这个问题?问题是根据指定的规则将数据写入组。
【问题讨论】:
-
什么具体的要求给了问题?该要求被编码成什么规则?您是否有无法读取/写入的代码示例?此外,您能否将屏幕截图替换为实际的 JSON,以便我可以使用您的数据进行测试。您可以通过单击 Firebase Database console 中的“导出 JSON”链接来获取此信息。
-
一旦您授予用户访问某个级别的数据的权限,您就不能在较低级别取消该权限。这也是 Firebase 文档建议扁平化数据结构的原因之一。在您的场景中,我会将
points和visits分离到一个单独的顶级节点中。 -
最好按照您的建议将数据展平。
-
是的,在您当前的结构中,集团层面的运营总是充满挑战。但是我正在尝试是否可以在儿童级别上使其安全。我将在答案中发布更新,因为它们在 cmets 中不可读。
标签: json firebase firebase-realtime-database firebase-security