I am trying to restrict writing to two related firebase nodes. I have a the following data structure on firebase:
"mainsibblings" : {
"-L9ygIWI-TKeNZvQ-TmP" : {
"MACaddress" : "111111111111",
},
},
"slavesibbling" : {
"111111111111" : {
"onMainSibling" : "-L9ygIWI-TKeNZvQ-TmP",
}
}
The slavesibbling pushkey is a unique MAC Address (freely created and updated by the user).
Conditions:
1) user should have permission to write a new mainsibbling and slavesibbling
2) shouldn't have permission to overwrite neither (mainsibbling & slavesibbling) if slavesibbling key (in this case: "111111111111") already exists.
3) the "owner" should be able to update both node even after he creates them
So if someone types a MAC Address that is registered already, the child nodes can't be updated, but if the user (referenced in ownerID) want to update a MAC Address he has created he should be able to do it.
How do I write the firebase database rules to control this? I am trying this to prevent duplicates but it's not working:
"mainsibblings": {
".read": true,
"$pushKey": {
".write": "data.child('MACaddress').val() != newData.child('MACaddress').val()",
}
},
"slavesibbling": {
"$MACaddress":{
".read": true,
".write": "!data.hasChild(newData.val())",
}
}
This is the write operation I want to allow/disallow (I have replaced the MACaddress variable for a hardcoded MACaddress):
function AddSibblings(newMainSib, newSlaveSib) {
var MACaddress = "111111111111";
var ownerID = "QXXds7d33ceyecc4inoe33p_3";
mainRef.child(MACaddress).set(this.newMainSib);
otherRef.child(MACaddress).set(this.newSlaveSib)
mainRef.child(MACaddress).child('ownerID').set(ownerID);
otherRef.child(MACaddress).child('ownerID').set(ownerID);
otherRef.child(MACaddress).child('onMainSibbling').set(MACaddress);
}