Firestore 查询管道中的执行顺序 - 为什么缺少权限?
Order of execution in Firestore query pipeline - why missing permissions?
我的 Firestore 集合包含每个用户一个文档,用户 ID 是文档的 ID。
我有以下规则:
match /mycollection/{userId} {
allow read, update, delete: if userId == request.auth.token.email;
allow create, update: if request.auth.uid != null && userId == request.auth.token.email;
}
这保证
- 每个用户都可以阅读、更新和删除“他们的”文档,
- 新用户可以创建“他们的”文档。
这在我“按 ID”加载用户文档时效果很好。
但是,当通过查询加载文档时它不起作用:
const query = db.collection("mycollection")
.where("user", "==", userId)
.where("writeDate", ">", writeDate)
.get()
如您所见,用户仅加载“他们自己的文档”,因此在我看来,该请求满足访问规则。但是,无论如何,我都会收到“缺少或权限不足”错误。
为什么?
我想这与 Firestore 上的查询和规则的执行顺序有关,但我在文档中没有找到任何关于此的内容。只要我只访问我有阅读权限的文档,应该就没问题吧?
如果您想知道:我需要查询以仅在文档发生更改时下载文档(因此使用“date”where 子句)。我知道我可以通过实时更新(使用 onSnapshot
或类似的)来做到这一点。我不想知道其他方法。
找到了。以下规则不起作用:
match /mycollection/{userId} {
allow read, update, delete: if userId == request.auth.token.email;
allow create, update: ...
}
这些规则确实有效:
match /mycollection/{userId} {
allow read, update, delete: if resource.data.user == request.auth.token.email;
allow create, update: ...
}
userId
只有在通过其 ID 访问文档时才有效。
如果规则是基于文档的某个字段(此处:user
),则需要使用该字段作为resource.data.user
.
我的 Firestore 集合包含每个用户一个文档,用户 ID 是文档的 ID。
我有以下规则:
match /mycollection/{userId} {
allow read, update, delete: if userId == request.auth.token.email;
allow create, update: if request.auth.uid != null && userId == request.auth.token.email;
}
这保证
- 每个用户都可以阅读、更新和删除“他们的”文档,
- 新用户可以创建“他们的”文档。
这在我“按 ID”加载用户文档时效果很好。
但是,当通过查询加载文档时它不起作用:
const query = db.collection("mycollection")
.where("user", "==", userId)
.where("writeDate", ">", writeDate)
.get()
如您所见,用户仅加载“他们自己的文档”,因此在我看来,该请求满足访问规则。但是,无论如何,我都会收到“缺少或权限不足”错误。
为什么?
我想这与 Firestore 上的查询和规则的执行顺序有关,但我在文档中没有找到任何关于此的内容。只要我只访问我有阅读权限的文档,应该就没问题吧?
如果您想知道:我需要查询以仅在文档发生更改时下载文档(因此使用“date”where 子句)。我知道我可以通过实时更新(使用 onSnapshot
或类似的)来做到这一点。我不想知道其他方法。
找到了。以下规则不起作用:
match /mycollection/{userId} {
allow read, update, delete: if userId == request.auth.token.email;
allow create, update: ...
}
这些规则确实有效:
match /mycollection/{userId} {
allow read, update, delete: if resource.data.user == request.auth.token.email;
allow create, update: ...
}
userId
只有在通过其 ID 访问文档时才有效。
如果规则是基于文档的某个字段(此处:user
),则需要使用该字段作为resource.data.user
.