【问题标题】:How to allow a node to be created, updated and deleted by the creator but not by other users in Firebase?如何允许创建者创建、更新和删除节点,而不是 Firebase 中的其他用户?
【发布时间】:2018-04-16 19:47:56
【问题描述】:

我的应用内容是由用户生成的。我希望每个用户都能访问和编辑他自己的数据(他创建的节点)。因此,如果节点为空,则用户创建“剪辑”节点和“剪辑所有者”节点,并且如果他是所有者,他还应该能够更新这些节点和子节点中的任何一个。如果节点 pushKey(即“$11111111”)已被占用,则其他用户不应该能够创建新节点。 这是我的数据库结构:

                 clips {
 variable Key >>>   "111111111111" : {
                      "MACaddress" : "111111111111",
                      "comments" : "",
                      "created" : "Mon Apr 16 2018 12:40:13 GMT+0100 (BST)",
                      "inRoom" : "-LADDm48Uqabm1bcQGOw",
                      "ins" : {
                         "1523878813443" : true
                      },
                      "name" : "1",
                      "outs" : {
                         "1523878813443" : true
                      },
                      "ownerID" : "QpMHsVHHRrMvk92rbSQvcYEv4en1"
                   },
                   "222222222222" : {
                      "MACaddress" : "222222222222",
                      "comments" : "",
                      "created" : "Mon Apr 16 2018 12:40:13 GMT+0100 (BST)",
                      "inRoom" : "-LADDm48Uqabm1bcQGOw",
                      "ins" : {
                         "1523878813443" : true
                      },
                      "name" : "1",
                      "outs" : {
                         "1523878813443" : true
                      },
                      "ownerID" : "QpMHsVHHRrMvk92rbSQvcYEv4en1"
                   }
                },


                 "clipOwners" : {
                     "111111111111": "QpMHsVHHRrMvk92rbSQvcYEv4en1"           
                     "222222222222": "QpMHsVHHRrMvk92rbSQvcYEv4en1", 
                 }

我正在尝试这个,但是如果另一个用户尝试写入相同的 $MACaddress,“ownerID”子节点会继续被另一个用户更新:

    "clips": {  
        ".read": "true",
        ".write": "!data.exists() || newData.exists()",
        "$MACaddress":{
          "ownerID": {
              ".validate": "!data.exists() || newData.val() === root.child('clipOwner').child($MACaddress).val()",

            }
        }
    },
    "clipOwners": {
      ".read": true,
      ".write": "newData.exists()",
      "$MACaddress": {

      },

为什么会这样? 关于如何锁定这个东西的任何想法?

【问题讨论】:

  • 是否可以选择更改数据结构?
  • 是的@AndréKool。前提是 $MACaddress 是剪辑的密钥并且规则有效
  • 这仅仅是关于用户无法写入其他用户数据还是关于无法拥有重复密钥? (在最后一种情况下,密钥是如何生成的?)
  • 两者兼而有之。用户在输入字段中键入键

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


【解决方案1】:

在您当前的写入规则中,您只检查数据(不)是否存在。在这个答案中,我将只关注写入规则,以确保您不能有重复的键(请参阅 clipOwners 规则)并且您只能写入自己的数据(请参阅剪辑规则):

"clipOwners": {
  ".read": true,
  "$MACaddress": {
      //Only create or delete are possible and value is the user uid
      ".write": "(!data.exists() || !newData.exists()) && (newData.val() == auth.uid || data.val() == auth.uid)"
  }
},
"clips": {  
    ".read": "true",
    "$MACaddress":{
      //The $MACaddress has to exist in the clipOwners node and its value has to be the user uid
      ".write": "root.child('clipOwners/'+$MACaddress).exists() && root.child('clipOwners/'+$MACaddress).val() == auth.uid"
    }
}

写入时,您首先必须在 clipOwners 节点中写入 $MACaddress,因为这将用于检查用户是否可以写入 clips 节点。

您可以查看these docs 的类似案例。

【讨论】:

  • @JoaoAlvesMarrucho 您能否使用数据库中的实际数据以及您正在使用的应该失败/成功的编写代码来更新您的问题?
  • 您能否添加您用来编写问题的代码?根据您的 cmets,我很难理解我的答案是如何失败的。并且可能会清理一些多余的 cmets 以保持井井有条。
  • @JoaoAlvesMarrucho 要删除数据,您必须确保首先删除剪辑中的数据,然后删除剪辑所有者中的数据,因为剪辑所有者中的数据用于确定用户是否可以写入(或删除)到剪辑节点
  • 做到了!谢谢您的帮助! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
相关资源
最近更新 更多