繁体   English   中英

Firebase 实时数据库规则防止节点删除

[英]Firebase realtime database rules prevent node deletion

我的用户数据库如下所示:

root
  users
   453453452
       name: user1
       email: email@example.com
       permissions: [1,2,3]
   457575684
       name: user2
       email: email2@example.com
       permissions: [1,2,3]
   234235346
   123245346
   436286785
   474574725

我要做的是允许写入/users和用户节点/users/453453452 ,但防止删除 users 表或任何用户节点。

我尝试了什么:

{
    "rules": {
        "users": {
            ".read": false,
            ".write": "newData.exists() && newData.val() != null",
            "$user": {
                ".read": true,
                ".write": "newData.exists() && newData.val() != null",
            }
        }
    }
}

如果我向/users/453453452.json发送 DELETE 请求,我仍然可以删除任何节点。

请问我能得到一些帮助吗?

编辑:1-成功购买后将用户添加到数据库,其中用户 ID 是用户的userID地址的散列唯一标识符。

fetch(myAPI + "/" + userID + ".json", {
    method: 'PUT',
    headers: {'Accept': 'application/json, application/xml, text/plain, text/html, *.*',
        'Content-Type': 'application/json; charset=utf-8'},
    body: JSON.stringify({"name": userName,"email": userEmail, "order_id": orderID})
});

我可以使用 POSTMAN 轻松地向myAPI + "/" + userID + ".json"发送 DELETE 请求,以删除我试图避免的选定记录,甚至向/users发送一个空数组,这将擦除整个数据库.

我无法在这里重现问题。 我使用的完整代码是:

var url = "https://stackoverflow.firebaseio.com/61015647";

fetch(url+".json", {
  method: 'POST', // *GET, POST, PUT, DELETE, etc.
  headers: {
    //'Content-Type': 'application/json'
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: JSON.stringify(Date.now())
}).then(function(response) {
  return response.json()
}).then(function(json) { 
  console.log("Child "+json.name+" added, trying to delete with POST...");
  return fetch(url+"/"+json.name+".json", {
    method: "DELETE"
  })
}).then(function(response) {
  return response.json()
}).then(function(json) { 
  console.log(json);
}).catch(function(error) {
  console.error(error);
});

以及相关规则:

{
  "rules": {
    "61015647": { 
      ".read": true, 
      "$child": { 
        ".write": "newData.exists() && newData.val() != null" 
      } 
    }
  }
}

当我运行此代码时,output 是:

null(来自 on("value" 监听器的初始值)

“添加了子 -M44hzNISkQNvycyH63B,尝试使用 POST 删除...”

[对象对象] {错误:“权限被拒绝”}

[object Object] { (on("value" listener 的下一个值) -M44hzNISkQNvycyH63B: 1586012616166 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM