Reputation: 428
I have two collections: projects and sprints. Inside projects I have a members array with objects. And using Firebase security rules I want to check if a value exists inside one of those objects.
Project example
Sprint example
What I want to do is, when a user updates or creates a sprint, I want to check if the logged in user (request.auth.uid)
matches with one of the userIds inside of the members array of the project.
But I just can't figure out how to accomplish this.
This is what I currently have, which does not work obviously.
match /sprints/{document} {
allow read: if request.auth.uid != null
allow update, create: if get(/databases/$(database)/documents/projects/$(resource.data.project)).data.members == request.auth.uid
}
Does someone maybe know how to do this?
Thanks in advance
Upvotes: 0
Views: 1539
Reputation: 3642
You wont be able to do that with this data structure: you would need to loop through the members and you cant do that in the rules.
But what would work is to use a map instead of an array with the uid as the key.
{
archived: false
id: "asdasd"
members: {
firstuid: { name: "Bob", role: "Prog"}
seconduid: { name: "Alice", role": "PM"}
}
}
Then the following rules would do the job:
match /sprints/{document} {
allow read: if request.auth.uid != null
allow update, create: if get(/databases/$(database)/documents/projects/$(resource.data.project)).data.members.get(request.auth.uid, null) != null
}
Upvotes: 3