Firebase 数据库写入权限规则使用 newValue 和 data 访问现有数据

Firebase database write permission rules using newValue and data to access existing data

我在 Firebase 中编写规则时遇到问题

我的数据结构是这样的:

"permisos": {
   "owners" : {
      "$userId" : {
          "$client" : true
       }
   }
 }
 "data" : {
    "promotions" : {
       "$promotion_key" : {
           "property1:" : "some value"
           "client" : "client name"
       }
    }
  }

我正在编写规则以在“data/promotions/$promotion_key下写入。我想验证当前用户是否有权使用相应的[=26编写促销条目=] 客户端值(插入、更新或删除)。到目前为止,我已经尝试了以下规则:

".write" : "root.child('permisos').child('owners').child(auth.uid).child(newData.child('client').val()).exists() || "root.child('permisos').child('owners').child(auth.uid).child(data.child('client').val()).exists()"

规则的第一部分检查插入,第二部分检查删除尝试。

根据 OR 子句的顺序,我可以插入但在尝试删除时失败(权限被拒绝),反之亦然。似乎它没有评估 || 的两个部分。

我已经尝试了 the or individual 的每个子句,它们工作正常。

newData 将不存在删除操作,因此您需要防范这种情况以避免规则的第一部分失败:

(
  newData.exists() &&
  root.child('permisos').child('owners').child(auth.uid).child(newData.child('client').val()).exists()
) ||
root.child('permisos').child('owners').child(auth.uid).child(data.child('client').val()).exists()