首页 > 解决方案 > Firebase 数据库规则中不同用户的多个规则

问题描述

更新:

这就是我的firebase db的结构看起来像它有两个主要节点结果和调查所示的“

Firebase 数据库结构

这些是我目前的规则:

{
  "rules": {
    "surveys": {
      ".indexOn":["userId"],
    ".read": true,
    ".write": "auth.uid != null"
    },
  "results": {
    ".read": "auth.uid != null",
      "$resultid": {
    ".write": true
  }
  }
  }
}

基本上,当用户进行新调查时,它会被存储,它将模板保存在调查中,现在只有经过身份验证的用户才能写入它,所以它非常好,现在是阅读部分,现在任何人都可以阅读整个调查节点,我无法将 auth != null 添加到它,因为当有人分享他们的调查时,即使未经身份验证的用户也应该能够阅读该特定调查并进行操作,所以我应该怎么做才能不允许每个人都阅读整个调查节点但是只有他们应该阅读的那个,否则任何人都可以通过提取所有 ID 来执行任何人的调查。顺便说一句,$resultid 规则不起作用,现在仍然允许写访问:(

标签: firebasefirebase-realtime-databasefirebase-security

解决方案


The closest you can get it:

{
  "rules": {
    "surveys": {
      ".read": "auth != null",
      "$surveyid": {
        ".read": true,
        ".write": true
      }
    }
  }
}

These rules:

  • Allow any authentication user to read all surveys.
  • Allow anyone who knows a survey ID to read/write that survey. You'll note that this is slightly different than your second requirement, as there is no way to know who created each survey unless they are signed in.

Update

For the results you'd then add:

"results": {
  ".read": "auth != null",
  "$resultid": {
    ".write": true
  }
}

推荐阅读