【问题标题】:How to have nested logical and/or in Firebase database rules如何嵌套逻辑和/或 Firebase 数据库规则
【发布时间】:2018-02-02 22:33:37
【问题描述】:

我有一个要阻止的 firebase 数据库规则:

  • 未经身份验证的用户
  • 试图为其他用户创建数据的用户
  • 我还想阻止更新数据。为此我需要此代码:

    !data.exists() || !newData.exists()
    

这是目前的样子:

"rules": {
"orders": {
  "$order":{
    ".read": "auth != null && auth.token.admin === true",
    ".write": "auth != null && auth.uid === newData.child('userID').val()" // check the incoming data's userID value here if sender is same. or someone can send orders with other peoples ids. also anyone can send an order with id 123 but only if it doesnt exist. so they cannot update anyones order. is this safe enough?
  },
  ".indexOn": "userID"
}

}

  1. 我应该如何包含这个“||” “.write”规则的逻辑表达式中的运算符?

  2. 我无法删除数据。是不是因为我在安全规则中检查了 newData 的子节点,而在应用程序中使用“.remove()”时没有 newData?如果是这样,在同一行中写入和删除规则如何工作?

【问题讨论】:

  • 文档建议您可以同时使用 && 和 ||。你有什么具体问题?您是否正在使用身份验证模拟器来找出为什么您的规则不能按您期望的方式工作?
  • 请将规则图片替换为文字。将规则作为文本使其可搜索,使我们能够轻松地使用它来测试您的实际规则并在我们的答案中使用它,总的来说这只是一件好事。
  • @DougStevenson 但我需要以某种方式在括号或其他内容中包含“!data.exists() || !newData.exists()”,并使用逻辑和添加到表达式中。如果我只是将它放在这个逻辑表达式附近,“!newData.exists()”将允许访问所有数据,因为它位于 || 之后

标签: firebase firebase-realtime-database firebase-security


【解决方案1】:

我遇到了一个奇怪的问题,我一开始无法使用括号,但现在可以使用了。我创建了一个 ||两个表达式之间

第一个表达式是写:

(auth != null && newData.exists() && auth.uid === newData.child('userID').val() && !data.exists())

表达式中的“newData.exists()”告诉我们有一个传入数据(写入/更新操作),“!data.exists()”阻止更新数据。

第二个表达式用于删除规则:

(!newData.exists() && auth != null && auth.uid === data.child('userID').val()

这里“!newData.exists()”表示没有新数据所以这是一个删除操作,下一部分确保每个人只能删除自己的数据。

所以现在代码如下所示:

"rules": {
"orders": {
  "$order":{
    ".read": "auth != null && auth.token.admin === true",
    ".write": "(auth != null && newData.exists() && auth.uid === newData.child('userID').val() && !data.exists()) || (!newData.exists() && auth != null && auth.uid === data.child('userID').val())"
  },
  ".indexOn": "userID"
},

}

【讨论】:

    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2017-07-15
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多