我的 Cloud Firestore 数据库中有以下规则:
service cloud.firestore {
match /databases/{database}/documents {
match /performances/{performanceId} {
allow read, update, delete: if request.auth.uid == resource.data.owner;
allow create: if request.auth.uid != null;
}
}
}
这个想法是,如果您拥有表演,则可以读取和写入表演;如果您已登录,则可以创建表演。
这个查询工作正常:
db.collection("performances").whereEqualTo(FieldPath.of("owner"), user.getUid())
但是,如果我想获取“场景”子集合的内容,则会收到错误:“com.google.firebase.firestore.FirebaseFirestoreException:PERMISSION_DENIED:权限缺失或不足。”
这是通过以下查询:
db.collection("performances")
.document(performanceID)
.collection("scenes");
我假设我需要将查询限制为如下所示,但这不会起作用,因为 whereEqualTo 的输出是查询而不是 CollectionReference,因此我无法访问“文档”:
db.collection("performances")
.whereEqualTo(FieldPath.of("owner"), user.getUid())
.document(performanceID)
.collection("scenes");
那么,有谁知道如果主集合有安全规则,我应该如何访问子集合?
更新1(因为代码没有在下面的注释中格式化)
我想我可能已经想出了一个解决方案。我没有意识到我的安全规则默认会拒绝从子集合中读取,因此将其更改为允许对表演中的场景进行所有读取和写入使其工作正常:
service cloud.firestore {
match /databases/{database}/documents {
match /performances/{performanceId} {
allow read, update, delete: if request.auth.uid == resource.data.owner;
allow create: if request.auth.uid != null;
match /scenes/{sceneId} {
allow read, write: if true
}
}
}
}