gpt4 book ai didi

angular - `list` 请求的 Firestore 安全规则

转载 作者:太空狗 更新时间:2023-10-29 18:02:40 24 4
gpt4 key购买 nike

在 Firestore 安全规则中,是否可以在使用 list 查询时检查某些文档字段?

使用 Angular,我想使用 username 属性从 userprofiles 集合中检索单个文档,如下所示:

let userprofile = this.afs.collection( 'userprofiles', ref => 
ref.where('username', '==', username ).limit(1);

如果出现以下任一情况,我希望 Firestore 安全规则允许此查询:

  • 用户资料已发布,或者
  • 用户配置文件未发布,但相应用户已登录

这是我的 Firestore 安全规则:

match /userprofiles/{userprofileId} {

allow list: if( resource.data.published==true || (
resource.data.published==false &&
resource.data.uid==request.auth.uid )
);
}
}

对于上下文,我使用完全相同的规则来允许 get 请求,效果很好。但是,上面示例中的查询会导致 list 请求,而不是 get。在那种情况下,这些规则不允许查询。我收到 错误:权限缺失或不足。

我记得读过一些关于列表查询的内容,规则必须允许所有文档或不允许任何文档,这在我的情况下不适用。所以我有点理解为什么它不起作用。

我的问题是,我可以更改某些内容以使其适用于我的查询吗?或者这是不可能的?任何解决方法的想法? (除了明显的“按文档 ID 查询”或“使用户名成为文档 ID”之外)

最佳答案

为了允许“列表”查询,查询参数必须符合安全规则。请引用the documentation.

您的查询需要包含 published==true 语句:

let userprofile = this.afs.collection( 'userprofiles', ref => 
ref.where('published', '==', true).where('username', '==', username ).limit(1);

您的规则可以简化为:

match /userprofiles/{userprofileId} {

allow list: if resource.data.published==true ||
resource.data.uid==request.auth.uid;
}

请注意,查询只会列出已发布的用户配置文件。我认为不可能一次查询“published==true”或匹配 uid 条件,您的代码需要查询两次。

关于angular - `list` 请求的 Firestore 安全规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49950468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com