【发布时间】:2019-12-17 20:43:25
【问题描述】:
我想保护数据,同时允许用户获取属于他们在 Firebase 实时数据库中可以访问的“类别”的“注释”。我想避免重复和维护访问条目 - 因此访问仅存储在类别中。只有某个类别的作者才能访问该类别中的“注释”。
数据库结构:
"categories" : {
"category_001": {
"access": {
"author": "user-001"
}
"data" : {
"title": "My first category"
}
}
}
"notes" : {
"note_001": {
"data" : {
"note": "Hello world!",
"categoryId": "category_001"
}
}
}
Firebase 规则:
{
"rules": {
"notes": {
".read": "
auth.uid !== null &&
query.equalTo === auth.uid
",
"data": {
".read": "
root.child('categories').child('access').child('author').val() === auth.uid"
}
}
}
不正确的数据库查询(不检查类别的访问):
database.ref("notes")
.orderByChild("access/author")
.equalTo(`${user.uid}`)
.once('value', (snapshot) => {
snapshot.forEach( (data) => {
console.log("OWNER", data.val().data.title)
})
})
如何编写数据库查询来检查“categories/category001”节点是否可以访问 - 同时读取“notes”/note001”节点?
亲切的问候 /K
【问题讨论】:
标签: javascript firebase firebase-realtime-database firebase-security