10

I'd like to create a simple chat app, but I don't want any user to be able to edit the chat-entry list - but I do want any user to be able to append to it. Is this possible?

2 Answers 2

16

You can utilize exists in your write rule:

"chat_list": {
   "$chat_entry": {
       ".write": " !data.exists() "
   }
}

This will make it possible to append but not to write to a record which already exists.

7
  • See edits above by Kato. The key is to assign the write rule to the children of the list.
    – Andrew Lee
    Commented Jul 22, 2013 at 16:36
  • Sorry for the delay, I found a solution (having my own service do the append, and giving the clients read-only access otherwise) and totally forgot about this question...
    – Chris2048
    Commented Jul 31, 2013 at 21:41
  • Yeah, I just don't see that @nathancahill. !data.exists() means you can only write if the data does not exist. Enough said. You probably made a mistake implementing if you saw differently.
    – Kato
    Commented Feb 6, 2017 at 23:53
  • Sorry @Kato, you're correct. I'm not sure why I was seeing that behavior with my rules. Just tested in a clean project and your answer works as described. Deleting my answer and upvoting yours. Commented Feb 7, 2017 at 13:14
  • Cheers, @nathancahill. Thanks for contributing to the convo and for adding your thoughts. I love how interactive the Firebase community is.
    – Kato
    Commented Feb 7, 2017 at 23:26
0

over a decade late to the party, but maybe this

// Only allow to APPEND to myArray
allow update: 
  if request.resource.data.keys().hasOnly(['myArray'])
  && request.resource.data.myArray is list
  && request.resource.data.myArray.size() == resource.data.myArray.size() + 1
  && request.resource.data.myArray.diff(resource.data.myArray).affectedKeys().hasOnly([resource.data.myArray.size()]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.