【问题标题】:How do I prevent access to changes after creating a node in Firebase Database?在 Firebase 数据库中创建节点后,如何防止访问更改?
【发布时间】:2017-05-23 04:52:48
【问题描述】:

我有以下情况,如果节点已经存在,如何拒绝更新(覆盖)节点?比如加好友的请求,我想要那个,这个功能执行了一次,因为它在数据库中设置了规则,但是它们不起作用。如何解决这个问题?

规则

// friends 
      "friends": {
        "$ownerID": {
          "friendIncomingRequests": {
            "$secondUserID": {
              ".write": "!data.exists()" // Only allow new nominations to be created
            }
          },
          "friendOutgoingRequests": {
             "$secondUserID": {
              ".write": "!data.exists()" // Only allow new nominations to be created
            }
          }
        }
      }

数据

  "friends" : {
    "8OdvaGQfMVdJrlCxdc5pOaj09hy2" : {
      "friendOutgoingRequests" : {
        "mp9pfsfVQKavwYddjYYPC5Ja9N93" : {
          "timeStamp" : 1.495514876872129E9,
          "userID" : "mp9pfsfVQKavwYddjYYPC5Ja9N93",
          "userName" : "Tim C."
        }
      }
    },
    "mp9pfsfVQKavwYddjYYPC5Ja9N93" : {
      "friendIncomingRequests" : {
        "8OdvaGQfMVdJrlCxdc5pOaj09hy2" : {
          "senderID" : "8OdvaGQfMVdJrlCxdc5pOaj09hy2",
          "senderName" : "Alexsander K.",
          "timeStamp" : 1.495514876872129E9
        }
      }
    }
  },

更新 我认为问题出在这段代码中,因为我在规则中也有这段代码。但是我该如何解决呢?

"rules": {
    ".read": "auth != null",
    ".write": "auth != null",
 }

更新 1:这是所有规则。我只需要在朋友中制定特定的写入规则(更新)。我看到了针对其规则的每个单独分支的示例,但是如果我需要为一个分支执行一些特定规则,而对于数据库的其余部分,您需要标准规则,我应该如何做得更好?

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",



      // card location
    "cardLocation": {
      // Allow anyone to read the GeoFire index
      //".read": true,
      // Index each location's geohash for faster querying
         ".indexOn": "g",

     },

      "cards": {
        ".indexOn": "ownerID"
      },

      "userListEvents": {
          "$uid": {
            ".indexOn": "isConfirmed"
          }
      },

      "userImages": {
        "$uid": {
          "userProfileImages": {
                            ".indexOn": "isoDate"
          }
        }
      },

      // tags 
      "userTags": {
        "$uid": {
          ".indexOn": "isSelected"
        }
      },


        // people search
        // 
        "userLocations": {
          ".indexOn": "g"
        },


      // friends 
      "friends": {
        "$ownerID": {
          "friendIncomingRequests": {
            "$secondUserID": {
              ".write": "!data.exists()" 
            }
          },
          "friendOutgoingRequests": {
             "$secondUserID": {
              ".write": "!data.exists()" 
            }
          }
        }
      }

  }
}

【问题讨论】:

  • 我很欣赏这次更新,但我有点困惑。您能否将附加规则添加到顶部的原始规则中,以便我可以准确地看到它的外观?我想我可能会为您提供解决方案,但澄清会有所帮助。
  • @JenPerson 你好!我写了所有的数据库规则。请看一看。非常感谢!
  • 谢谢!哦,还有一件事:当你说它不起作用时,你的意思是在测试中 .write 总是被允许的,还是永远不允许的?
  • @JenPerson 我的意思是,它总是被允许的。我想为特定的分支设置特定的规则。 (例如对朋友的请求)。谢谢。

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


【解决方案1】:

我认为问题出在这段代码中,因为我在规则中也有这段代码。但是我该如何解决呢?

"rules": {
    ".read": "auth != null",
    ".write": "auth != null",
 }

是的,你的想法是正确的。 Firebase .read.write 规则级联。因此,您应该将每个 .read.write 放在数据结构的每个子节点上。类似的东西:

{
  "rules": {
  //skip this
    "someNode": {
        ".read": "auth != null",
        ".write": "auth != null"
    },
    // friends 
    "friends": {
      "$ownerID": {
        "friendIncomingRequests": {
          "$secondUserID": {
           ".write": "!data.exists()" 
        }
      },
        "friendOutgoingRequests": {
          "$secondUserID": {
            ".write": "!data.exists()" 
          }
        }
      }
    }
    //...
  }
}

【讨论】:

  • 是的,我这样做了,我只是认为可能有更好的解决方案可以将一个分支设为例外。
  • 孩子也不例外:D
猜你喜欢
  • 1970-01-01
  • 2021-05-15
  • 2017-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-11
  • 1970-01-01
  • 2020-12-06
相关资源
最近更新 更多