【问题标题】:How to allow all users to read only certain documents with a field " friend : true "如何允许所有用户仅阅读带有“朋友:真”字段的某些文档
【发布时间】:2021-03-20 14:03:55
【问题描述】:

在 Firestore 中,我将集合设为“用户”,文档 ID 是随机数。

现在我需要允许用户阅读包含字段 friend : true

的文档

我尝试了以下规则:

match /users/{document=**}
{
allow read : if request.resource.data.friend = true;
}


match /users/{document=**}
        {
        allow read : if resource.data.friend = true;
        }

但是我无法阅读包含friend = true的文档。

请帮帮我

【问题讨论】:

  • 安全规则本身不做任何事情。它们仅在与触发它们的查询配对时才会生效。请编辑问题以显示与这些规则不符合您期望的应用代码。

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


【解决方案1】:

我的第一个猜测是您没有通过读取操作传递相同的条件。请记住Firebase security rules do not filter data。相反,他们“仅仅”确保允许读取操作。所以你的 sn-p 中的第二条规则允许这个查询:

firebase.firestore().collection("users").where("friend", "=", true)

如果您没有通过where 条件,读取将被拒绝,因为您尝试读取所有文档,而安全规则不允许这样做。只有当查询和规则匹配时,才会允许读取。


您的第一个规则片段在任何情况下都不起作用,因为request.resource 仅在写入操作的上下文中有意义。来自docs

request.resource

新资源值,仅在写入请求时出现。

【讨论】:

  • 哇,你是对的。它按我的需要工作。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多