Firebase在数据库中确保节点的安全
问题描述:
我使用的是Firebase。这里是我的一个节点看起来像:Firebase在数据库中确保节点的安全
--Users
----RandomUID1
----RandomUID2
----RandomUID3
对于规则,这就是我:
--"users" : {
----".read": "auth != null && !root.child('blockedUsers').hasChild(auth.uid)",
----".write": "auth != null && !root.child('blockedUsers').hasChild(auth.uid)"
--}
我想是从users
节点读取将保持不变,但我想写作只允许users
节点的孩子。这样做的原因是,如果我不小心编写了一个晚上删除整个users
节点的代码,那将是灾难性的。是否有可能制定规则,以便不允许在节点上写入,但允许写入子节点?
有没有办法让自己免受可能因人为错误而发生的事故?
答
可以使用变量,Firebase variable rules
.AT用户节点我没加一个写入规则,即不允许修改数据。
"users":{
".read": "auth != null && !root.child('blockedUsers').hasChild(auth.uid)",
"$uid": { //where $uid is child
".read": "auth!==null && auth.uid == $uid",
".write": ""auth!==null && auth.uid === $uid"
}
}
谢谢!如果我在用户节点上写入“写入”:“false”会怎么样? –
这里没有什么区别,因为默认值是'false'。但为了清晰起见,您可能需要添加它,然后在较低级别上指定更宽松的规则。需要记住的一件重要事情是逆向是不可能的。权限是继承的:因此,一旦您在一个级别上指定了“true”,您就不能在较低级别上获得该权限。 –
很好的答案btw @Fowotade –