首页 > 解决方案 > 与我在客户端代码中创建的内容相匹配的 firebase 规则是什么?

问题描述

我正在尝试设置对自己用户在 firebase 实时数据库中写入的数据的读取访问权限。

我在代码中以这种方式为每个firebase条目提供了我认为该条目的uid:

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Chat").child(myId));

但以下代码在 firebase 实时数据库中不起作用:

{
  "rules": {
    "Chat": {
      "$uid": {
        ".read": "$uid === auth.uid",
        (more code here)
      }
    }
  }
}

我正在使用匿名签名登录firebase,也许这就是它无法将其识别为uid的原因,或者只是这不是uid。

这将是这种情况下的firebase规则?我想通过更改“$whatever_this_field_is_understood_by firebase_rules”的“$uid”它会起作用。

这可能吗?

PD:根据 Frank van Puffelen 的要求,我正在描述我的实时数据库结构:

Level1

  Level2 (The one corresponding to "Chat")

    Level3.1 (this one should match with the id, it's named with its name)

     Level4 (field 1)

     Level4 (field 2)

     Level4 (field 3)

    Level3.2

     Level4 (field 1)

     Level4 (field 2)

     Level4 (field 3)

另一方面,我认为通过将规则更改为“.read”:“$uid === $uid”找到了解决方案,但我无法正确测试它。

我想检查是否有 $uid 的用户,例如“222”试图访问“111”的用户的字段 1,这是不可能的,但我无法找到一种方法来做到这一点代码作为测试。

有人可以确认这是正确的方式吗,或者我该如何进行上述测试?

标签: firebasefirebase-realtime-databasefirebase-security

解决方案


推荐阅读