【问题标题】:Firestore Where Statement with Rules?带有规则的 Firestore Where 语句?
【发布时间】:2019-08-22 20:52:49
【问题描述】:

我有一个 Firestore 查询:

firebase.firestore()
    .collection('files')
    .where("design_id", "==", design.id)
    .get();

获取文件列表,其中数据看起来像这样

{
design_id: "P6oShXH081Mbssxs2zcp",
name: "all-teachers-like-brains.png",
type: "image",
user_id: "1uD82gAXORYsyimX5Dw23DDAimx1"
}

我要做的是确保只有可以访问它们的用户是 uid 与文档中的 user_id 匹配的用户。

为此,我有以下规则(第 2 版)

{
  match /files/{document=**} {
      allow read, delete: if fileBelongsToUser();
  }

function fileBelongsToUser() {
      return get(/databases/$(database)/documents/files/{document=**}).data.user_id == request.auth.uid;
    }
}

尽管如此,我仍然被拒绝许可,我不知道为什么。我的规则是否阻止了 where 语句?

【问题讨论】:

  • 如果你使用模拟器,它是否通过规则?

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


【解决方案1】:

Firebase security rules are not filters。您不能通过应用规则来限制查询访问的文档。如果规则不允许查询中的任何文档,则整个查询将失败。因此,查询只需要请求规则允许的文档。

因此,如果您的规则规定 uid 必须等于文档中的字段 user_id,则您的查询必须使用该条件过滤文档:

firebase.firestore()
    .collection('files')
    .where("design_id", "==", design.id)
    .where("user_id", "==", uid)
    .get();

上面的uid 是当前经过身份验证的用户的UID。

【讨论】:

    猜你喜欢
    • 2019-04-10
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多