【问题标题】:Firebase: allow anyone from an array of UIDs to access read / writeFirebase:允许 UID 数组中的任何人访问读/写
【发布时间】:2018-02-26 21:27:28
【问题描述】:

我正在使用 Firebase 的实时数据库开发一个应用程序,并且需要允许多个用户访问相同的数据,但我无法找出一个安全规则来使这项工作发挥作用。

数据库如下所示:

teams: {
    teamID3ic3kic9w3jkck : {
        userIDs: ["11111", "22222", "33333", "44444"]
        teamData { ....}
    }
}

我希望允许 ID 与“userIDs”数组中的任何 ID 匹配的用户访问“teamData”。非常感谢帮助解决这个问题。

【问题讨论】:

  • 请发布您尝试过的规则。

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


【解决方案1】:

每次您想要执行array.contains() 时,您都可能使用了错误的数据结构。例如,这在我看来更像是一个数学集合:一个独特项目的无序集合。在 Firebase 中,您可以将其建模为:

teams: {
    teamID3ic3kic9w3jkck : {
        userIDs: {
            "11111": true, 
            "22222": true, 
            "33333": true,
            "44444: true"
        ]
        teamData { ....}
    }
}

现在您可以使用以下方法保护它:

{
  "rules": {
    "teams": {
      "$teamid": {
        ".read": {
          ".read": "data.child('userIDs').child(auth.uid).exists()"
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 2020-05-03
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多