EDIT: Seems to be an open issue in Firestore. Also see this post.
In Google Cloud Firestore, I want to model a collection of group
s. Each group contains a name
, the list of it's user
s and some secretGroupData
. For me, the natural way to do this would be:
/groups
/group1 {
name: "Group 1"
users: { //object can be queried, simple array not
"user1": true,
"user5": true
}
secretGroupData: ...
}
/group2 { ... }
Given a user like user1
, I want to query all groups he is member of. This query works fine:
groupsRef.where("users.user1", "==", true)
However, I want to secure the group data. This query only works, when all groups are readable for all users. When I protect the group to be readable only by the group members, by the rule
match /groups/{groupId} {
allow read: if resource.data.users[request.auth.uid] == true;
}
the above query does not work any more, because as soon as it sees a group where the current user is not a member of, read access is denied and the whole query fails.
What is the best solution for this problem in Firestore? Should I
- tell Firestore to return only the allowed
group
s and ignore the other ones, instead of throwing an error? If so, how can I achieve this? - make the
group
s readable for alluser
s and move thesecretGroupData
into subcollections, where I can then restrict the access to just the group members - add redundancy by adding the IDs of all groups of a user into the user's profile document (
/users/user1/groupIds: ["group1"]
), so I know the groups beforehand and can query them by ID - use a totally different solution?
Thank you very much for your ideas.