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

Firebase数据库使用newValue和data写入权限规则以访问现有数据,firebase,firebase-realtime-database,firebase-security,rules,Firebase,Firebase Realtime Database,Firebase Security,Rules,我在Firebase中编写规则时遇到问题 我的数据结构如下所示: "permisos": { "owners" : { "$userId" : { "$client" : true } } } "data" : { "promotions" : { "$promotion_key" : { "property1:" : "some value" "client" : "

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

我的数据结构如下所示:

"permisos": {
   "owners" : {
      "$userId" : {
          "$client" : true
       }
   }
 }
 "data" : {
    "promotions" : {
       "$promotion_key" : {
           "property1:" : "some value"
           "client" : "client name"
       }
    }
  }
我正在“data/promotions/$promotion\u key”下编写规则。我想验证当前用户是否有权限使用相应的属性客户端值(insert、update或delete)编写促销条目。到目前为止,我尝试了以下规则:

".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子句的顺序,我可以插入,但在尝试删除或反之亦然时失败(权限被拒绝)。似乎它不会同时计算| |的两个部分


我已经尝试了或单个的每个子句,它们工作正常。

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()

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()