【问题标题】:Firestore permissions; checking if a user is in an arrayFirestore 权限;检查用户是否在数组中
【发布时间】:2020-03-27 05:08:37
【问题描述】:

在 Cloud Firestore 中,我有一个文档集合,其中包含一个数组字段,其中包含应该对该文档具有读取权限的所有用户。我简化的安全规则是:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /collection_name/{document=**} {
      allow read: if request.auth.uid in request.resource.data.listOfIds.toSet();
    }
  }
}

一个示例简化文档是:

{
  listOfIds: ["uid1", "uid2"]
}

使用规则模拟器执行获取,读取按预期工作。但是,使用 Android API 执行类似的查询会返回权限被拒绝错误。我想我以与规则相同的方式限制我的查询(这是 Firestore 的 requirement;它不会过滤掉用户无法为您阅读的文档)。

firestoreClient.collection("collection_name")
    .whereArrayContains("listOfIds", firebaseUid)
    .get()
    .addOnCompleteListener(...);

【问题讨论】:

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


    【解决方案1】:

    事实证明,Firestore 认为查询未正确限制为定义的读取权限。是.toSet() 把它扔掉了。删除它会导致我预期的行为。

    为什么我一开始就有.toSet()?我错误地认为将数组转换为集合是使用in 运算符的要求。该运算符works with lists 以及集合。

    【讨论】:

    • 可能值得报告为 Firebase 支持的错误。 support.google.com/firebase/contact/support
    • 举报了,谢谢建议。听起来他们可能会更新 in 运算符的文档。不确定他们是否会更改验证逻辑以实现查询受到适当约束,即使使用 toSet
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 2013-05-04
    • 2016-04-23
    • 2018-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多