You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写Firestore安全规则:允许pst为true或不存在时读取

Firestore规则解决方案

要实现pst字段为"true"或不存在时允许读取,仅当pst为"false"时拒绝的需求,你需要调整规则的判断逻辑,把「字段不存在」的情况纳入允许条件中。

直接编写规则

allow read: if !request.auth.token.hasOwnProperty('pst') || request.auth.token.pst == "true";

封装为辅助函数(更易维护)

function canRead() {
  // 字段不存在 或者 pst值为"true"时返回true
  return !request.auth.token.hasOwnProperty('pst') || request.auth.token.pst == "true";
}

allow read: if canRead();

逻辑说明

  • 当令牌中没有pst字段时,request.auth.token.hasOwnProperty('pst')返回false,取反后为true,满足允许条件。
  • 当pst字段存在且值为"true"时,第二个条件成立,同样允许读取。
  • 只有当pst字段存在且值为"false"时,两个条件都不成立,规则会拒绝读取请求。

内容的提问来源于stack exchange,提问作者Vishwesh Jainkuniya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 07:42:03