I am using firbase database and I am trying to set up rules to limit write access to users who's uid matches the userId node of the object they are trying to write and still allow write to create a new object such as a new post if it doesn't exist. So new posts are allowed and editing your own post is allowed, but write is denied if post exists, but doesn't belong to user.
Something like:
"posts": {
".write": "auth !== null && posts.post.userId.val() === auth.uid
}
or maybe something like
//where posts.post represents objectRef.objectUpdating
"rules":{
".write": " auth !== null && posts.post.userId.val() === auth.uid
}
posts.postid object is updated like this:
firebase.database().ref('posts').child(id).update(updates)
Here is a sample of how posts look. Threads and forums follow same structure.
"posts": {
"-KsjWehQ--apjDBwSBCZ": {
"edited": {
"at": 1504037546,
"by": "ALXhxjwgY9PinwNGHpfai6OWyDu2"
},
"publishedAt": 1504035908,
"text": "some post text",
"threadId": "-KsjWehQ--apjDBwSBCY",
"userId": "ALXhxjwgY9PinwNGHpfai6OWyDu2"
},
...{post2},
...{etc}
}
Hope that makes sense, any help is appreciated.
EDIT: I got it so when the update or create method is called it checks to see if data.userId is the same as the auth.uid. Updating existing data now works.
The create method is not working though, I am not sure how to allow update() to create new posts or threads like before without updating if userId does not match auth.uid.
Rules below:
{
"rules": {
".read": true,
"users": {
".indexOn": ["usernameLower", "email"],
"$user": {
".write": "auth !== null && $user === auth.uid"
}
},
"forums": {
".write": false
},
"categories": {
".write":false
},
"$resource": {
"$child": {
".write": "(auth !== null && auth.uid === data.child('userId').val()) || data.val() === null"
}
}
}
}