【问题标题】:How to forbid rewriting data if it exists如果存在,如何禁止重写数据
【发布时间】:2017-04-25 11:52:58
【问题描述】:

授权后我在“用户”表中添加用户。此外,我将向该用户添加一些我不想被覆盖的值。

ref.child("users").child((FIRAuth.auth()?.currentUser?.uid)!).setValue(["username": "MyName"])

规则

{
    "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
    "users": {
      ".write": "!root.child('users/'+auth.uid).exists()",
      ".read":true,
      "positive": {
        ".read": true,
          ".write":false,
      },
      "negative": {
        ".read": true,
          ".write":false,
      },
    }
  }
}

删除旧数据并放入新数据。

我想在服务器端编写规则,如果设置值已经存在,它将忽略设置值。

【问题讨论】:

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


    【解决方案1】:

    如果您调用updateChildValues(),您指定的属性将被更新:

    ref.child("users")
       .child((FIRAuth.auth()?.currentUser?.uid)!)
       .updateChildValues(["username": "MyName"])
    

    请参阅Firebase documentation on updating specific fields

    您的规则将不起作用,因为您无法取消您在树的更高级别上授予的权限。请参阅Firebase documentation on the cascading of permissions

    为确保用户始终具有某些属性,请在用户节点上使用规则:

    "users": {
      "$uid": {
          ".write": "newData.hasChildren(['certain', 'properties])",
    

    【讨论】:

    • 这会阻止数据被写入数据已经存在的节点吗?
    • 不。那将是!data.exists()
    • 很奇怪我们不能控制服务器端的数据。是的,您的解决方案有效。不是我想要的,顺其自然吧。
    • 弗兰克,请更正这一行“newData.hasChildren(['certain', 'properties'])”
    • 完成。请注意,这里有一个编辑链接,任何人都可以修正此类错别字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多