Reputation: 333
Testing Collection Group following the documentation (https://firebase.google.com/docs/firestore/security/rules-query#collection_group_queries_and_security_rules)
Firestore Security Rule
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /forums/{forumid}/posts/{post} {
allow read: if true;
}
}
}
Query On Android App (Using Firestore 19.0.1)
FirebaseFirestore.getInstance().collectionGroup("posts").get().addOnSuccessListener { queryDocumentSnapshots ->
Log.d(TAG, "queryDocumentSnapshots " + queryDocumentSnapshots.size())
}.addOnFailureListener {
Log.d(TAG, "exception" + it)
}
getting exception PERMISSION_DENIED: Missing or insufficient permissions.
Upvotes: 6
Views: 2099
Reputation: 8035
You need to add a wild card variable in the path to make it work for collection group queries. Reference to documentation https://firebase.google.com/docs/firestore/security/rules-query#secure_and_query_documents_based_on_collection_groups
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{path=**}/posts/{post} {
allow read: if true;
}
}
}
Upvotes: 21