32
loading...
This website collects cookies to deliver better user experience
flats
which includes a subcollection called items
.rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /flats/{flatId} {
allow create: if request.auth != null;
allow read, update, delete: if request.auth != null
&& request.auth.uid in resource.data.idsOfUsersWithAccess;
match /items/{itemId} {
allow create: if request.auth != null;
allow read, update, delete: if request.auth != null
&& request.auth.uid in resource.data.idsOfFlatmatesThatShareThis;
}
}
}
}
request.auth != null
) is allow
ed to create
a flat, but they are only allow
ed to read
, update
or delete
a flat if their unique ID (request.auth.uid
) is included in the flat’s property called idsOfUsersWithAccess
.items
collection is a subcollection of the flats
one, I created a nested rule which extends the previous one (read more about rules for hierarchical data in the docs). The rule says that only authenticated users are allow
ed to create
items but in order to read
, update
or delete
an item the user’s unique ID needs to be included in the idsOfFlatmatesThatShareThis
property of the flat.Uncaught (in promise) FirebaseError: Missing or insufficient permissions.
firestore.collection('flats').doc(flat.id)
.collection('items').get()
If the authenticated user queries all flat items, the result will surely only include the ones they have access too, based on the security rules that I created.
Rules are not filters. You cannot write a query for all the documents in a collection and expect Cloud Firestore to return only the documents that the current client has permission to access.
where
clause to match the rule fixed the problem and I could now load the intented items:firestore.collection('flats').doc(flat.id)
.collection('items')
.where('idsOfFlatmatesThatShareThis', 'array-contains', state.user.id)
.get()