我在为 Firestore 项目设置规则时遇到一些问题。我尝试学习数据库设置,但找不到任何解决方案。因此,当我尝试从我的收藏“列表”中获取文档时,没有任何问题。但是当我尝试获取集合“列表”中的所有文档时,xcode 告诉我“权限缺失或不足”。 我的目标是让用户能够在集合“列表”中创建文档,但他们只能读取出现在文档数组“成员”中的“列表”中的文档。
现在我可以毫无问题地在集合(“列表”)中添加文档,但我无法阅读它们。我只能从 xcode 中针对特定目标一一读取它们。
有什么建议或想法吗?
service cloud.firestore {
match /databases/{database}/documents {
match /{documentId} {
allow read: if request.auth.uid != null;
}
match /lists/{docId} {
allow write: if request.auth.uid != null;
allow read: if request.auth.uid in resource.data.members
}
}
}
Xcode。
//working
let docRef = db.collection("lists").document("j0hHA5TLPETf6JRMbC1s")
docRef.getDocument { ...
//not working due to permission failed
db.collection("lists").getDocuments() { ...
最佳答案
您的规则不起作用,因为它假设该规则将过滤掉所有与该规则不匹配的文档。这不是安全规则的运作方式。 Security rules are not filters.从链接的文档:
When writing queries to retrieve documents, keep in mind that security rules are not filters—queries are all or nothing. To save you time and resources, Cloud Firestore evaluates a query against its potential result set instead of the actual field values for all of your documents. If a query could potentially return documents that the client does not have permission to read, the entire request fails.
客户只能请求满足规则的文件。现在,查询要求列表集合中的所有文档,无论客户端是否有权访问它们。这将使安全规则失败,因为它试图读取它无权访问的文档。
您需要做的是让您的查询仅请求用户可读的文档。这意味着您可能应该使用 array-contains filter在客户端上仅匹配规则允许其读取的文档。
关于google-cloud-firestore - 是否有读取集合中所有文档的规则功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55690278/