匹配。
我存储在数据库中的我的模型就像:
{
"ownerUid": <my auth UID>,
"teamName: "Team 1",
"teamDescription: "Team 1 desc"
}
我的初始访问规则就是这样:
service cloud.firestore {
match /databases/{database}/documents {
match /teams/{teamId} {
allow read: if resource.data.ownerUid == request.auth.uid;
allow write: if true; //ignore this
}
}
}
我为获得团队的查询如下:
Query query = FirebaseFirestore.getInstance()
.collection("teams")
.orderBy("teamName")
运行此查询将始终导致"权限拒绝",我完全不确定其原因是什么。
我也尝试了以下规则,用于测试目的:
allow read: if true; //Works
allow read: if request.auth.uid != null; //Works
allow read: if request.auth.uid == <my auth uid>; //Works
allow read: if resource.data.ownerUid != null; //Fails
关于成功的规则,我可以在返回的团队中看到所有者,并且看到它不是零,并且与。
在您的当前代码中,您正在尝试从集合teams
读取。由于您无法访问该完整集合,因此读取被拒绝。
如果要允许用户查询他们创建的文档,则需要做两件事:
- 使用仅检索其创建文档的查询。
- 编写您的安全规则,以便他们允许此查询。
您已经完成了#2,但没有做#1。要使事情正常工作,请使用这样的查询:
String uid = FirbaseAuth.getInstance().getCurrentUser().getUid();
Query query = FirebaseFirestore.getInstance()
.collection("teams")
.whereEqualTo("ownerUid", uid)
.orderBy("teamName")
还请参见基于auth.uid的安全和查询文档的文档部分。