firebase - Firestore规则,使用短路来挤出读取

标签 firebase google-cloud-firestore firebase-security

这两个 Firestore 规则在我的配额中花费的读取次数完全不同吗?请注意,isWebAdmin() 会执行 exists(),这会占用我的读取配额。

// example 1
match /companies/{company} {
  // rule 1
  allow list, write: if isWebAdmin();

  // rule 2
  allow get: if isInCompany(company)
    // when isInCompany is true, this is short-circuited away
    || isWebAdmin(); 
}

对比

// example 2
match /companies/{company} {
  // rule 1
  allow read, write: if isWebAdmin();

  // rule 2
  allow get: if isInCompany(company); 
}

这是我的(可能是错误的)推理:对于大多数 get 请求,isInCompany(company) 将为 trueisWebAdmin( ) 将为 false。因此,在示例 2 中,即使用户被授权使用规则 2 来 get,规则 1 也会执行,因为 get 也是一个 read 。因此,在尝试授予管理员访问权限的同时,我为具有访问权限的普通用户花费了更多的阅读时间。

在示例 1 中,我将 getlist 分开并分别处理。在 get 请求中,它根本不会运行规则 1。运行规则 2 时,由于 isInCompany(company) 为 true,因此 isWebAdmin() 由于短路而不会执行。因此,在常见情况下,我通过避免调用 isWebAdmin() 来保存读取。

这是正确的吗?如果是这样,只需授予管理员权限即可为每个用户的常规操作添加获取。我觉得这有点不方便。我想如果情况并非如此,我们应该只按“有效”规则计费,而不是按测试过的所有规则计费。 是这样吗?

最佳答案

使用 Firebase 安全规则, bool 表达式会短路,这是优化规则成本的有效方法。为此,请使用示例 1 中更细化的规则。

关于firebase - Firestore规则,使用短路来挤出读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57274952/

相关文章:

javascript - Angular 火 : undefined is not a function ($on method does not exist)

javascript - 使用 doc.data() 时如何将变量分配为字段名持有者。在 firebase 中检索数据

java - Firestore - 如何从 DocumentSnapshot 中获取集合?

android - 使用 Firestore 和 Google Speech to Text 时出现重复的类错误

javascript - Firebase 权限被拒绝

javascript - 无法使用reactFire从数据库检索firebase json

firebase - Flutter - 从 firestore 循环获取数据

javascript - 如何在 Firestore Web 上创建子文件夹和文档?

带有通配符的 Firebase 安全规则

firebase - 在 Firestore 安全规则中创建新文档时如何验证另一个文档引用