Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase delete child with security rule !data.exists on write

I have a security rule for adding new data

 "CATEGORIES": {
      "$CATEGORIES": {
      ".write":   "root.child('USERS/' + auth.uid + '/type').val() == 'admin' && !data.exists() || root.child('USERS/' + auth.uid + '/type').val() == 'admin' && root.child('MODES/delete').val() == 'yes'",
       "$DATA": {
         ".write":   "root.child('USERS/' + auth.uid + '/type').val() == 'admin'",
       }
      }
      }

It's used to prevent duplicates or overwriting of the child node with the !data.exists() rule.

Problems is, it wont let me delete the child with this rule because data exists. To get around this I added the root.child('MODES/delete').val() data to allow me to delete it if I set it to 'yes'.

How can I have it so I don't have to do this work around? So I can have no duplicates but also be able to delete the child if I want to?

like image 955
bdaz Avatar asked Dec 26 '13 04:12

bdaz


1 Answers

To allow creates but no overwriting, !data.exists() works fine. To allow deletes, you can use !newData.exists().

So all together:

// create or delete, but no update/overwrite
".write":  "!data.exists() || !newData.exists()"
like image 81
Kato Avatar answered Oct 19 '22 20:10

Kato