firebase规则,以确保注释的所有者有权读取、写入和任何其他用户只喜欢它。数字\u检查是用户表

firebase规则,以确保注释的所有者有权读取、写入和任何其他用户只喜欢它。数字\u检查是用户表,firebase,firebase-realtime-database,firebase-security,Firebase,Firebase Realtime Database,Firebase Security,以下是数据库结构,我希望确保其用户id为注释对象一部分的注释的所有者具有对注释的读写访问权限,并且所有其他用户具有对注释的读访问权限,并且能够喜欢它以增加喜欢数量: 以下是我提出的安全规则: { "rules": { "comments": { ".read": "root.child('users').child(auth.uid).val() != null", &qu

以下是数据库结构,我希望确保其用户id为注释对象一部分的注释的所有者具有对注释的读写访问权限,并且所有其他用户具有对注释的读访问权限,并且能够喜欢它以增加喜欢数量:

以下是我提出的安全规则:

   {
  "rules": {
    "comments": {
      ".read": "root.child('users').child(auth.uid).val() != null",
      ".write": "(newData.parent().child('users').child(auth.uid).val() != null && newData.parent().child('comments').hasChildren().hasChildren().child('user_id').val() == auth.uid)",
      "$commentId": {
        ".read": "root.child('users').child(auth.uid).val() != null",
        ".write": "(newData.parent().parent().child('users').child(auth.uid).val() != null && !newData.parent().parent().child('comments').hasChildren().hasChildren().child('like_count'))"
      }
    }
  }
}

因此,为了更好地阅读,我们将:

".read": "root.child('users').hasChild(auth.uid)"
“.write”更为复杂:

  • 您只需要允许在like_计数中进行编辑,并且每个用户只需要允许一个like
  • 正确的方法是在这里或其他地方扩展“帖子”的结构,比如_列表,以防止客户端下载帖子时数据传输量增加
因此,在“注释”节点中,您希望允许每个身份验证用户进行.write和.read操作,这样他们就可以添加新的注释了(上面的规则适用于此)。 对于$commentId,节点规则如下所示:

".write":".data.child('user_id').val() == auth.uid"
通过将用户id添加到“like/$commentId”节点,并在此节点的firebase功能中设置侦听器,可以将like_列表链接到clinet。添加新的用户id将触发“写入”事件,然后调用函数以确保增加like\ U count的值

您可以使用firebase函数和事务对其进行归档。

如果确实不想更改模式,则需要保护注释的每个子项,如:

"$commentID":{
       "comment":{ ".write":"".data.parent().child('user_id').val() == auth.uid" ),
       ...//things allowed to edit by owner
       "like_count":{
            ".write":"root.child('users').hasChild(auth.uid)",
            ".validate":"newData.val() == data.val() + 1"
       }
 }

但这并不能保证post不会为每个用户提供多个like。

1)您遇到问题的操作代码是什么?2) 这些规则和该操作有什么问题?我想确保只有注释的所有者才能对其执行CRUD操作,但应允许其他用户增加,如\u count,并设置is\u reply和has\u reples字段。上述规则不起作用,因为安全规则本身不起任何作用。它们只在代码访问数据库时才执行某些操作。编辑您的问题,使其包含最少的代码,您可以以一种我们都可以用最少的努力运行的方式重现问题。