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

标签 angular firebase google-cloud-firestore firebase-security

在 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/

相关文章:

javascript - 无法使用 FileReader 读取拖放文件

angular - 寻找使用 ngrx 8 沉浸式制作的示例

node.js - 部署 Firebase 和 Node.js 应用程序导致错误 : Error: Parse Error in remoteconfig. template.json: Unexpected token 'i' at 1:1

ios - Swift Firebase metaData!.downloadURL()!.absoluteString

python - 将 Python 日期时间转换为 firestore 时间戳格式

firebase - 错误: Could not load the default credentials (Firebase function to firestore)

Angular 和麦克风录音机到 mp3 : Lame is not defined

angular - 如何在angular2中将HTML转换为pdf?

firebase - ML Kit for Firebase 可以用于手写文本吗?

javascript - 写入 Firestore 数据库会导致 Firestore net::ERR_BLOCKED_BY_CLIENT 错误