如何使用安全规则过滤文档中的字段
How to filter fields in documents with security rules
我正在试验 Cloud Firestore 安全规则。是否可以过滤文档字段?
例如,如果您有一个文档
{
name: "John Doe",
email: "doe@example.com"
}
然后某些用户不允许使用电子邮件地址获取文档。他们的应用程序请求文档
firebase.firestore.doc('users/doe-uid')
并获取此文档
{
name: "John Doe",
}
如果是,怎么样?
我觉得应该可以,因为Cloud Firestore Security Rules Reference第一句话说(重点是我的):
Cloud Firestore Security Rules are used to determine who has read and write access to collections and documents stored in Cloud Firestore, as well as how documents are structured and what fields and values they contain.
但是我在参考中找不到任何告诉我如何过滤字段的内容。
Firestore 规则不是过滤器,它们是文档查询的服务器端验证,这意味着您访问(或不访问)整个文档,而不是特定字段。
您提到的文档意味着您可以对字段进行数据验证。
这是一个基本的规则示例,在写入查询时验证数据(通过 request.resource.data
):
match /users/{userId} {
allow write: if request.resource.data.age is int;
}
这是另一个使用现有字段验证读取查询的基本示例(通过 resource.data
):
match /articles/{articleId} {
allow read: if resource.data.isPublished == true;
}
要过滤字段,您可以在查询后在客户端进行。
现在,如果您想保护对某些字段的访问,您必须使用一组不同的规则创建另一个集合(查看 subcollections),并进行另一个匹配这些规则的查询。
我正在试验 Cloud Firestore 安全规则。是否可以过滤文档字段?
例如,如果您有一个文档
{
name: "John Doe",
email: "doe@example.com"
}
然后某些用户不允许使用电子邮件地址获取文档。他们的应用程序请求文档
firebase.firestore.doc('users/doe-uid')
并获取此文档
{
name: "John Doe",
}
如果是,怎么样?
我觉得应该可以,因为Cloud Firestore Security Rules Reference第一句话说(重点是我的):
Cloud Firestore Security Rules are used to determine who has read and write access to collections and documents stored in Cloud Firestore, as well as how documents are structured and what fields and values they contain.
但是我在参考中找不到任何告诉我如何过滤字段的内容。
Firestore 规则不是过滤器,它们是文档查询的服务器端验证,这意味着您访问(或不访问)整个文档,而不是特定字段。
您提到的文档意味着您可以对字段进行数据验证。
这是一个基本的规则示例,在写入查询时验证数据(通过 request.resource.data
):
match /users/{userId} {
allow write: if request.resource.data.age is int;
}
这是另一个使用现有字段验证读取查询的基本示例(通过 resource.data
):
match /articles/{articleId} {
allow read: if resource.data.isPublished == true;
}
要过滤字段,您可以在查询后在客户端进行。
现在,如果您想保护对某些字段的访问,您必须使用一组不同的规则创建另一个集合(查看 subcollections),并进行另一个匹配这些规则的查询。