【发布时间】:2017-09-11 17:03:47
【问题描述】:
我的数据库是这样的:
user123 是管理员。因此,他应该能够遍历 entries 中的所有节点。除非 entryID 的 uid 是 auth.uid
,否则其他人无法看到 entries 的子项我该如何为此制定规则?如果没有可能的方法,任何更改数据库的建议:)
【问题讨论】:
标签: firebase firebase-realtime-database firebase-security
我的数据库是这样的:
user123 是管理员。因此,他应该能够遍历 entries 中的所有节点。除非 entryID 的 uid 是 auth.uid
,否则其他人无法看到 entries 的子项我该如何为此制定规则?如果没有可能的方法,任何更改数据库的建议:)
【问题讨论】:
标签: firebase firebase-realtime-database firebase-security
如果您已经知道 admin 是,在您的问题 user123 中。那么你的数据库规则应该是这样的
"entities": {
"$entryId":{
// you don't what others to see other to see teh data
".read": "auth.uid == 'user123'"
// any one who is logged in should write to the /entries node
".write": "auth.uid != null"
}
}
如果你想让规则更加动态,那么你可以做
"entities": {
"$entityId":{
// you don't what others to see other to see teh data
".read": "root.child('users').child(auth.uid).child('isAdmin').val() == true || root.child('entities').child($entityId).child('uid').val() == auth.uid"
// any one who is logged in should write to the /entries node
".write": "auth.uid != null"
}
}
您可以从这里获得更多信息https://firebase.google.com/docs/reference/security/database/
或者,您可以将条目模型更改为用户特定的
{
"entities" :{
"user465": {
"entry456": {
"text" : "Some sample text"
}
}
}
}
在这种情况下,你写你的规则
"entities": {
"$userId":{
// you don't what others to see other to see teh data
".read": "root.child('users').child(auth.uid).child('isAdmin').val() == true || $userId == auth.uid"
// any one who is logged in should write to the /entries node
".write": "auth.uid == $userId"
}
}
【讨论】: