首页 > 解决方案 > 多个数据库的 Firebase 规则

问题描述

我正在使用 Firebase 实时数据库开发一个 Android 应用程序。我的项目包含 3 个不同的数据库:

  1. 数据库 1:用于常见项目;
  2. 数据库 2:用于特征 A;
  3. 数据库 3:用于特征 B;

在数据库 1 中,我可以使用其节点创建规则(为此数据库工作)来验证特定用户是否能够读取和写入任何数据,例如:

{
  "rules": 
  {
    "plants":
    {
      ".read": true, /*"auth !== null",*/
      ".write": "auth !== null && root.child('userRegistration/' + auth.uid).child('profileTypeInfo').val()=='0'"
    },
    "criteria":
    {
      ".read": "auth !== null",
      ".write": "auth !== null && root.child('userRegistration/' + auth.uid).child('profileTypeInfo').val()=='0'"
    },
    "guide":
    {
      ".read": "auth !== null",
      ".write": "auth !== null && root.child('userRegistration/' + auth.uid).child('profileTypeInfo').val()=='0'"
    },
    "userRegistration":
    {
      ".read": true,
      ".write": true
    },
      "report" : 
      {
        ".read": "auth !== null && root.child('userRegistration/' + auth.uid).child('isActiveInfo').val()==true",
        ".write": "auth !== null && root.child('userRegistration/' + auth.uid).child('isActiveInfo').val()==true",
      }
  }
}

对于这个数据库是可以的,因为我使用的是当前数据库的root.child。这个问题与数据库 A 和 B 有关,因为我想使用这样的东西:

database1.child('userRegistration/' + auth.uid).child('profileTypeInfo').val()=='0'"

而不是使用

root.child('userRegistration/' + auth.uid).child('profileTypeInfo').val()=='0'"

换句话说,我需要使用数据库 1中的子数据,尽管使用了不包含子userRegistration的根子

我非常感谢您的帮助!

标签: androidfirebasefirebase-realtime-database

解决方案


推荐阅读