如何只允许公开访问Firestore文档的特定字段 [英] How to allow only particular fields of a firestore document to be accessed publicly

查看:28
本文介绍了如何只允许公开访问Firestore文档的特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在Firestore数据库中具有以下结构:

Suppose I have this structure in a firestore database:

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

并且我有此Firebase规则:

and I have this firebase rule in place:

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

但是此规则在上面的collection中公开了整个document,我想做的是仅公开x字段,这可能吗?谢谢

But this rule exposes the whole document in the above collection, What I want to do is to expose only x field, is it possible? Thanks

推荐答案

您不能.安全规则仅在文档级别起作用,因此您不能将读取权限限制为特定字段.

You can't. Security rules only work on a document-level basis, so you can't restrict read access to specific fields.

如果您要执行建议的操作,则可能需要重组数据,以使非公开数据位于单独的文档中.您很可能希望通过将私有数据放在子集合中来实现此目的.所以您可能最终会遇到这样的事情...

If you want to do something like you're suggesting, you'll probably need to restructure your data so that your non-public data is in a separate document. Most likely you'll want to do this by putting your private data in a subcollection. So you might end up with something like this...

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

然后您将设置安全规则,例如:

And then you'd set up your security rules like:

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

这篇关于如何只允许公开访问Firestore文档的特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆