【问题标题】:How to allow only particular fields of a firestore document to be accessed publicly如何仅允许公开访问 Firestore 文档的特定字段
【发布时间】:2018-04-11 05:49:06
【问题描述】:

假设我在 firestore 数据库中有这个结构:

collection[
  document: {
    x: 'x',
    y: 'y'
  }
]

而且我已经制定了这个 firebase 规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /collection/{document} {
      allow read: if true;
    }
  }
}

但是这条规则暴露了上面collection中的整个document,我想做的是只暴露x字段,可以吗?谢谢

【问题讨论】:

标签: google-cloud-firestore firebase-security


【解决方案1】:

你不能。安全规则仅适用于文档级别,因此您不能限制对特定字段的读取权限。

如果您想按照您的建议执行操作,您可能需要重新构建数据结构,以便将非公开数据保存在单独的文档中。您很可能希望通过将您的私人数据放在子集合中来做到这一点。所以你最终可能会得到这样的结果......

collection: [
  document: {
    y: 'y'
    private-collection: [
      document: {
        x: 'x'
      }
    ]
  }
]

然后你会设置你的安全规则,比如:

service cloud.firestore {
  match /databases/{database}/documents {
    match /collection/{document} {
      allow read: if true;
      match /private-collection/{otherdoc} {
        allow read: if someOtherRuleThatYouAddHere();
      }
    }
  }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多