【问题标题】:Firestore Group Query does not work Permission DeniedFirestore 组查询不起作用权限被拒绝
【发布时间】:2020-11-17 23:50:39
【问题描述】:

我的项目中有以下情况。我正在使用 Angular Firestore 来获取用户详细信息的 groupCollection。

我的数据库集合层次结构如下所示。

users/{userId}/personalInfo/{document}

我在 Firestore 中为个人信息创建了组索引。

当我将我的规则重置为默认设置时

match /{document=**} {
  allow read, write: if false;
}

在我的规则结束时,它可以工作,我可以查询每个personalInfo 文档。

但是,一旦我尝试设置任何规则来保护此 groupCollection,我就会收到此“权限被拒绝”消息。

我尝试在规则底部设置的最简单的规则如下所示。

match /users/{userId=**}/personalInfo/{doc} {
  allow read, write: if true;
}

但不幸的是,我收到此权限被拒绝消息,不知道为什么。

在我的 UserService 中,我使用 Angular Firestore 来简单地执行 collectionGroup 查询。

constructor(private afs: AngularFirestore) {}
    
public getUsersDetailedInfo() {
  return this.afs.collectionGroup('personalInfo');
}

【问题讨论】:

  • 你能分享你得到的完整错误跟踪吗?这将有助于更好地了解案件

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


【解决方案1】:

您可能想查看the documentation for security rules for collection group queries。它说:

在您的安全规则中,您必须明确允许集合组 通过为集合组编写规则进行查询:

  • 确保rules_version = '2'; 是您的规则集的第一行。集合组查询需要新的递归通配符 {name=**} 安全规则版本 2 的行为。
  • 使用match /{path=**}/[COLLECTION_ID]/{doc} 为您的集合组编写规则。

您不能在集合组路径段前面使用静态路径段。这成为集合组查询总是尝试考虑具有给定名称的所有集合。您不能将查询或规则限制为仅嵌套在“用户”下的那些子集合。

如果您想允许“personalInfo”的所有集合组查询,则必须是这样的:

match /{path=**}/personalInfo/{doc} {
  allow read, write: if true;
}

请注意,我在这里所做的只是从匹配中删除“用户”路径段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 2020-09-12
    • 2019-02-24
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多