【问题标题】:firebase delete data belongs to a userfirebase 删除数据属于用户
【发布时间】:2017-08-07 02:17:20
【问题描述】:

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

我检查过:

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 等数字是电影的 ID 号。正如你所见,每部电影都有具有唯一 ID 的 cmets。每个评论都有评论和 userId(发送此评论的用户的 uid)属性。我删除了评论,但我想要,用户可以删除自己的评论。所以我想设置如下规则:

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

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

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

但我不会写,因为我不知道我小时候应该如何获得唯一 ID。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢。我实际上尝试过类似的东西,但它以前没有用。但它现在有效。我想我在某个地方犯了一个错误。再次感谢坦率。
    猜你喜欢
    • 2017-11-05
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2020-01-17
    • 2018-08-28
    • 2019-04-18
    相关资源
    最近更新 更多