firebase删除数据属于用户

时间:2017-08-06 16:55:16

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

我有一个电影应用程序。人们可以对电影发表评论。

我查了一下:

Firebase Security & Rules, How can I let users delete their own data?

https://www.firebase.com/docs/security/guide/securing-data.html#section-data-variables

https://www.firebase.com/docs/security/guide/user-security.html

但我可以找到答案。

comments {
  550: {
     UKs5lg...: {
           comment: "this is comment",
           userId: "Jkulsç122doasdjk"
    },
     WfyG5Hsl...: {
           comment: "this is comment2",
           userId: "Jkulsç122doasdjk"
    }
},
 1694: {
     Ki5Uydmk...: {
           comment: "movie 2 comment 1",
           userId: "Jkulsç122doasdjk"
    },
     Kovx9ox...: {
           comment: "movie 2 comment2",
           userId: "Jkulsç122doasdjk"
    }
}

}

在数据库中,550,1694等数字是电影的身份证号码。每部电影都有你所看到的具有独特身份的评论。每条评论都有评论和userId(发送此评论的用户的uid)属性。我删除了评论,但我想要,用户可以删除自己的评论。所以我想设置如下规则:

{
  "rules": {
    "comments": {
      "$movieId": {
                ".read": true,
                ".write": ??????,
      }
  }
}
}

我应该写什么安全规则而不是问号?我想写得像;

".write" : "!data.exists() || (!newData.exists() && data.child(blabla+ 'userId').val() === auth.uid)"

但是我不能写,因为我不知道我应该如何获得独特的童年身份。

1 个答案:

答案 0 :(得分:2)

您需要将规则下推到要强制执行操作的级别。既然你说每个用户都应该只能删除他们自己的评论,那么强制执行此操作的规则应该在评论中:

{
  "rules": {
    "comments": {
      ".read": true,
      "$movieId": {
        "$commentId": {
          ".write": "!data.exists() || (!newData.exists() && data.child('userId').val() === auth.uid)",
        }
      }
    }
  }
}