在公司的FireStore安全规则,是有可能使用list
查询时,检查某些文档字段?
采用了棱角分明,我想用它userprofiles
属性如下检索来自username
收集单个文档:
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
。而在这种情况下,这些规则不允许查询。我越来越Error: Missing or insufficient permissions.
我记得读沿着对于列表查询线的东西,规则必须允许全部或任何文件,这在我的情况不适用。所以,我有点明白为什么它不工作。
我的问题是,我可以改变的东西,使之成为我的查询工作?或者,这是不可能的?任何想法解决方法? (除了明显的“由文档ID查询”或“使用户名的文档ID”)
为了让“名单”进行查询,查询参数必须匹配的安全规则。请参考the documentation.
您的查询需要包括发布的==真实的陈述:
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;
}
注意查询将只列出发表的UserProfiles。我不认为这将有可能查询“发表== true”或一次匹配的UID状态,您的代码将需要查询的两倍。
我迟到了,但这样的说法在我的情况下工作
match /userprofiles/{userprofileId} {
allow read: if resource.data.published==true;
}
}
match /userprofiles/{userprofileId} {
allow read: if resource.data.uid==request.auth.uid;
}
}