[英]Firebase Realtime Database newData Rules
I'm creating some rules for a couple of tables which reflect a n2n relation.我正在为反映 n2n 关系的几个表创建一些规则。 Below is the code I use to update the database:下面是我用来更新数据库的代码:
// Prepara alterações no BD
HashMap<String, Object> dadosUpdate = new HashMap<>();
String idCurrUser = userManager.getCurrentUser().getId();
// Obtem nova chave para registros novos
if (isNew) {
String newCondominioId = FirebaseDBManager.getNewId("condominio");
condominioSel.setId(newCondominioId);
}
// Usuarios_Condominios
dadosUpdate.put("/usuarios_condominios/" + idCurrUser + "/" + condominioSel.getId() + "/ehAdmin/", true);
dadosUpdate.put("/usuarios_condominios/" + idCurrUser + "/" + condominioSel.getId() + "/ehSindico/", true);
dadosUpdate.put("/usuarios_condominios/" + idCurrUser + "/" + condominioSel.getId() + "/ehColaborador/", true);
// Condominios_Usuarios
dadosUpdate.put("/condominios_usuarios/" + condominioSel.getId() + "/" + idCurrUser + "/ehAdmin/", true);
dadosUpdate.put("/condominios_usuarios/" + condominioSel.getId() + "/" + idCurrUser + "/ehSindico/", true);
dadosUpdate.put("/condominios_usuarios/" + condominioSel.getId() + "/" + idCurrUser + "/ehColaborador/", true);
return fbDB.updateChildren(dadosUpdate);
And here are the rules I've setup at Firebase:这是我在 Firebase 设置的规则:
{
"rules": {
"usuarios_condominios": {
".read" : "auth.uid != null",
"$idUsuario": {
".write": "(auth.uid === $idUsuario) || (root.child('usuarios').child(auth.uid).child('ehAdmin').val() == true)"
}
},
"condominios_usuarios": {
".read" : "auth.uid != null",
"$idCondominio": {
".write": "(auth.uid != null && newData.child('condominios_usuarios').child($idCondominio).child(auth.uid).child('ehAdmin').exists())"
}
}
}
}
For some reason I could not find, the last rule is preventing me to save the data:出于某种原因我找不到,最后一条规则阻止我保存数据:
"newData.child('condominios_usuarios').child($idCondominio).child(auth.uid).child('ehAdmin').exists()"
Please help me understand what am I doing wrong.请帮助我了解我做错了什么。
The newData
variable contains the value for the node on which the rules is defined as it will exist after the write operation, if that write operation succeeds/is permitted. newData
变量包含在其上定义规则的节点的值,因为如果写入操作成功/被允许,它将在写入操作之后存在。
So, you don't need to build a path from the root yourself, newData
already points to the current node.所以,你不需要自己从根开始建立路径, newData
已经指向了当前节点。 I think that means you'll need:我认为这意味着你需要:
newData.child(auth.uid).child('ehAdmin').exists()
If in the future you want to inspect a node in a different branch from the root, you will have to call parent()
to get from the current node to the root of the tree, and then use child
calls from there.如果将来您想检查与根不同的分支中的节点,则必须调用parent()
以从当前节点到达树的根,然后从那里使用child
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.