Can I use the equivalent of array_contains
in Firebase Security Rules?
I have an Event
class (corresponding to Event
documents in the events
collection). Each document has a subscribers
list/array that contains the UIDs of all the users who should be able to query for this event:
{
.
"subscribers" : ["uidA", "uidB", "uidC"],
.
}
More explicitly, a user should be able to run the query:
db().collection("events").whereArrayContains("subscribers", "uidC");
How do I write this security rule?
I tried:
match/events/{eventId} {
allow list: if request.auth.uid in resource.data.subscribers;
}
and the simulator tells me that access would be denied.
Using functions like this also reports access denied:
function isSubscriber() {
return resource.data.subscribers.includes(request.auth.uid);
}
match/events/{eventId} {
allow list: if isSubscriber();
}
if request.auth.uid in resource.data.subscribers;
actually does work in practice. However, thein
operator is undocumented (found it through another SO question). Anywhere I can report this? – markvgtiin
operator is documented, as are all list operations: firebase.google.com/docs/reference/rules/rules.List – Doug Stevenson