【问题标题】:How do you allow only admins to read certain fields in a document in Cloud Firestore?如何仅允许管理员读取 Cloud Firestore 中文档中的某些字段?
【发布时间】:2020-04-15 13:42:47
【问题描述】:

我有一组文档,其中有些字段任何人都应该只能读取,有些字段只有管理员才能读取/写入。我的规则看起来像

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /MyCollection/{document=**} {
      allow read: if request.auth.token.admin == true;
      allow write: if request.auth.token.admin == true;
      match /publicField {
        allow read: if true;
      }
    }
  }
}

但是当我尝试阅读文档时,我得到了Missing or insufficient permissions 错误。如何允许任何人阅读任何MyCollection 文档的publicField

【问题讨论】:

    标签: google-cloud-firestore firebase-security


    【解决方案1】:

    无法更改单个文档中各个字段的访问权限。匹配模式只能针对文档路径,对文档字段一无所知。如果任何人都可以阅读文档,那么他们始终可以阅读整个文档。

    如果要更改文档中某些数据的权限,则需要将它们拆分到具有适当安全规则的集合中的另一个文档中。

    您可能有兴趣阅读:The trade-offs between performance, cost, and security with Firestore

    【讨论】:

    • 好文章。如果 Firebase 将来在现场级别实施安全性,将保留此未标记作为答案。
    • 这不会发生。出于各种原因,访问将始终在文档级别。
    • 好的,我相信你
    猜你喜欢
    • 2020-09-22
    • 2018-10-01
    • 2022-10-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2018-04-11
    相关资源
    最近更新 更多