【问题标题】:Firebase RTDB rule prevents "set" but still allows "update"Firebase RTDB 规则阻止“设置”但仍允许“更新”
【发布时间】:2019-06-18 05:47:12
【问题描述】:

我试图仅在具有子“A_Key”的情况下才允许对我的 firebase 表进行写入/更新。我设法阻止了 firebase 规则模拟器中的“设置”,但无法阻止“更新”。 “更新”模拟总是成功的。

我已尝试将其写入表格。

{
    "objectId" : "XXaabb",
    "value" : 135
}

这是我的规则

"MyTable": {
    "$uid": {
    ".read": "auth.uid == $uid",
    ".write": "auth.uid == $uid",
    ".validate": "newData.hasChild('A_Key') && newData.child('A_Key').val() === '123456'"
    }
}

模拟集被拒绝。这就是我想要的。 然而 允许模拟更新。这不是我想要的。

我想要的结果是两者都应该被拒绝。

【问题讨论】:

  • 您希望在更新时 A_key 的值也应该发生变化?

标签: firebase rules firebase-security


【解决方案1】:

如果你想在片场应该有 A_key 在数据中,当更新这个数据时应该有 A_key 的值,然后使用以下规则:

"myTable": {
  "$uid": {
    ".write": "(((data.val() == null && (auth.uid == $uid)) && newData.child('A_key').val() != null) || (((data.val() != null && newData.val() != null) && (auth.uid == $uid)) && data.child('A_key').val() != null))",
    ".read": "(auth.uid == $uid)"
  }
}

螺栓等效项:

path /myTable/{uid} {
    read() {isCurrentUser(uid)}
    create() {isCurrentUser(uid) && this.A_key != null}
    update() {isCurrentUser(uid) && prior(this.A_key) != null}
}

isCurrentUser(id) {
  auth.uid == id
}

如果你希望在更新时 A_key 的值不应该改变,那么使用这个:

"myTable": {
      "$uid": {
        ".write": "(((data.val() == null && (auth.uid == $uid)) && newData.child('A_key').val() != null) || (((data.val() != null && newData.val() != null) && (auth.uid == $uid)) && data.child('A_key').val() == newData.child('A_key').val()))",
        ".read": "(auth.uid == $uid)"
      }
    }

等效螺栓:

path /myTable/{uid} {
    read() {isCurrentUser(uid)}
    create() {isCurrentUser(uid) && this.A_key != null}
    update() {isCurrentUser(uid) && prior(this.A_key) == this.A_key}
}

isCurrentUser(id) {
  auth.uid == id
}

https://github.com/FirebaseExtended/bolt/blob/master/docs/guide.md

【讨论】:

    【解决方案2】:

    我做了一个小改动,现在它可以工作了

    newData.hasChild('A_Key')
    

    应该是

    newData.child('A_Key').exists()
    

    【讨论】:

      猜你喜欢
      • 2019-03-29
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 2020-11-13
      相关资源
      最近更新 更多