【问题标题】:Preventing child nodes from being removed in Firebase防止在 Firebase 中删除子节点
【发布时间】:2016-01-10 02:18:24
【问题描述】:

我在 Firebase 安全方面遇到了一个痛苦的问题。

我希望经过身份验证的用户在子节点下创建子节点,但不允许删除任何子节点。

请查看“已使用”节点中的 cmets

以下安全规则:

"users": {

      "$userid":{

      ".read": "$userid === auth.uid",  

      ".write":" $userid === auth.uid && newData.exists()",

       //writeable by user
      "qrcodevalue":{}, 
      "datesubscribed":{},

      //not writeable by user
      "confirmed":{".validate":false}, 
      "issubscribed":{".validate":false},
      "periodend":{".validate":false},
      "stripeid":{".validate":false}, 
      "stripesubscription":{".validate":false}, 
      "subscriptionstatus":{".validate":false}, 

      //user should be able to create children under this node but not delete
      "used":{
        "$promotionid":{
          "dateused":{}
        }
      }, 


      }   
    },

任何帮助将不胜感激。

【问题讨论】:

    标签: firebase firebase-security


    【解决方案1】:

    来自Firebase security documentation on new and existing data

    预定义的数据变量用于在写入操作发生之前引用数据。相反,newData 变量包含写入操作成功时将存在的新数据。 newData 表示正在写入的新数据和现有数据的合并结果。

    为了说明,考虑一个允许我们创建新记录或删除现有记录的规则,只要给定路径上不存在数据,但不对数据进行更改:

    // we can write as long as old data or new data does not exist
    // in other words, if this is a delete or a create, but not an update
    ".write": "!data.exists() || !newData.exists()"
    

    所以对你来说,这会转化为这样的东西:

      //user should be able to create children under this node but not delete
      "used":{
        "$promotionid":{
          "dateused":{
            ".write": "newData.exists()"
          }
        }
      }, 
    

    这允许用户将任何数据写入节点,但不能删除它。

    如果您希望他们仅创建更改数据,则变为:

            ".write": "!data.exists() && newData.exists()"
    

    【讨论】:

    • 嗨弗兰克,感谢您抽出宝贵时间回复。我已经尝试过了,但是它不起作用。我可以在“已使用”节点下为孩子运行删除操作。我认为问题是父写规则正在接管。当我删除“$userid === auth.uid && newData.exists()”的父写入规则,然后将您建议的内容“.write”:“newData.exists()”添加到“dateused”时,它会阻止我从运行删除。父规则抛出它,因为我认为新数据的快照有数据(即使一个孩子已被删除)。
    • 啊,是的。我错过了你的那部分规则。确实:一旦您授予了某个级别的权限,就不能在树的较低位置将其删除。这通常意味着您应该以不同的方式构建我们的数据,将used 拉到它自己的“顶级”级别;这样它就可以有自己的一套规则。
    猜你喜欢
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    相关资源
    最近更新 更多