简体   繁体   English

Firebase 实时数据库 - 允许用户删除自己的数据

[英]Firebase Realtime Database - Allow users to delete their own data

I currently have the following rules on my database which basically (a) allow users to enter their data if they have not done it yet ( this is for their first time) -->.data.exists (b) allow them to write only if their new values are less than the current ones.我目前在我的数据库上有以下规则,基本上 (a) 允许用户输入他们的数据,如果他们还没有输入数据(这是他们第一次)-->.data.exists (b) 允许他们只写如果他们的新值小于当前值。 --> data.child('time').val() > newData.child('time').val() and finally (c) allow them to change their username --> data.child('userName').val().= newData.child('userName').val()" --> data.child('time').val() > newData.child('time').val() 最后 (c) 允许他们更改用户名 --> data.child('userName') .val().= newData.child('userName').val()"

All of the above work perfectly but now due to also Google PLay store policies i want the user to have the abillity to delete their data if they want to.以上所有功能都完美无缺,但现在由于 Google PLay 商店政策,我希望用户能够删除他们的数据,如果他们愿意的话。

I tried several solutions like data.exists() but even though this works bypasses my other rule about the value check if it less and allows to write values that are greater than the existing one.我尝试了几种解决方案,如 data.exists() 但即使这绕过了我关于值检查的其他规则,如果它更少并允许写入大于现有值的值。

Can you help on this how to add another rule that will allow the user to delete their entry more specific the userID and at the same time all of the above continue working你能帮忙解决这个问题吗,如何添加另一条规则,允许用户删除他们更具体的用户 ID 的条目,同时以上所有内容继续工作

My command is我的命令是

var DBTaskWrite = DBReference.Child("Users").Child("3X3").Child(_userID).RemoveValueAsync();
{
  "rules": {            
    //".read": true,
      "Users" : {
           "3X3": {
              ".indexOn": ["time"],
             ".read": "data.exists()",
             "$userID": {
               ".write": "!data.exists()  || data.child('time').val() > newData.child('time').val() || data.child('userName').val() != newData.child('userName').val()"
             }
          }
       }
    }
  }

Tried the data.exists which is working, but bypass the other rules.尝试了有效的data.exists ,但绕过了其他规则。

If you also want to allow the user to delete their data, you can add ||.newData.exists() to the rule.如果您还想允许用户删除他们的数据,您可以将||.newData.exists()添加到规则中。 So:所以:

"$userID": {
  ".write": "!data.exists() || !newData.exists || ..."
}

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

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