【问题标题】:Collection group queries getting denied by firebase rules集合组查询被 Firebase 规则拒绝
【发布时间】:2019-06-20 22:56:51
【问题描述】:

用户、项目作为顶级集合,任务作为项目的子集合 以下是安全规则,我在允许对任务进行集合组查询时遇到了麻烦。即使我删除了对 createdBy 的检查,似乎也不起作用,当我为任务资源运行模拟器时,我的规则有效

match /projects/{projectID} {
  allow read, delete, update: if request.auth.uid == resource.data.createdBy;
  allow create: if request.auth != null;
}

match /users/{userID} {
    allow read, delete, update: if request.auth.uid == userID;
  allow create: if request.auth != null;
}

match /projects/{projectID}/tasks/{taskID} {
  allow read, delete, update: if request.auth.uid == resource.data.createdBy;
  allow create: if request.auth != null;
}

这是我的收藏组查询

_firestore
    .collectionGroup('tasks')
    .where('dueDate', isEqualTo: DateTimeHelper.today)
    .where('createdBy', isEqualTo: user.id)
    .snapshots()
    .map((list) => list.documents.map((doc) {
          String projectId = doc.reference.parent().parent().documentID;
          String taskId = doc.documentID;
          return Task.fromDocument(doc, taskId, projectId);
        }).toList());

【问题讨论】:

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


    【解决方案1】:

    您的所有规则均不适用于集合组查询。您应该查看documentation on rules for collection groups。从该页面:

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

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

    所以你的规则看起来更像这样:

    rules_version = '2';  // at the very top of your rules
    
    match /{path=**}/tasks/{taskID} {
      allow read, delete, update: if request.auth.uid == resource.data.createdBy;
      allow create: if request.auth != null;
    }
    

    【讨论】:

    • 您可以使用“taskID”变量与安全规则中的内容进行比较吗?每次我尝试它都会出错......即taskID == resource.data.oldTaskID
    • @Leon 看来我们做不到。当我尝试使用 taskID 时,我得到了和你一样的行为是错误了
    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多