Firebase 安全规则不适用于 CollectionGroup

时间:2021-02-10 12:47:52

标签: flutter google-cloud-firestore firebase-security

我有这个安全规则代码

  match /{path=**}/favorited/{userUID} {
  allow create, update, read: if request.auth.uid == userUID;
} 

这是我收到的错误信息

<块引用>

W/Firestore(6236):(21.3.0) [Firestore]:侦听查询(collectionGroup=favorited where userUID == abcdefghijklmnopqrstuvwxyz and favorite == true order by -updatedAt, -name ) 失败:状态{代码=PERMISSION_DENIED,描述=权限缺失或不足。,原因=空}

我使用的查询是

  Firestore.instance.collectionGroup('favorited')
    .where('userUID', isEqualTo: user.uid)
    .where('favorite', isEqualTo: true)
    .orderBy('updatedAt', descending: true).snapshots()

我无法弄清楚 collectionGroup 安全规则的哪一部分出错了。 当没有安全规则时,查询代码工作正常。 我在这里错过了什么?

1 个答案:

答案 0 :(得分:0)

您的查询:

Firestore.instance.collectionGroup('favorited')
    .where('userUID', isEqualTo: user.uid)
    .where('favorite', isEqualTo: true)
    .orderBy('updatedAt', descending: true).snapshots()

...正在寻找具有 field {userUID: value} 其中值等于 user.uid 的任何文档(未知 ID),但您的安全规则:

match /{path=**}/favorited/{userUID} {
  allow create, update, read: if request.auth.uid == userUID;

专门寻找具有 request.auth.uid 的 Id 的文档。

请记住,安全规则不是过滤器,您是否创建了文档,使得每个带有 Id {value} 的文档也有一个 field {userUID: value} ?

query 必须进行过滤;安全规则确保只有结果ALL通过规则的查询才会成功。如果查询可以导致其 Id 与字段 {userUID: value} 不同,它将被拒绝。