【问题标题】:Understanding Basic Firebase Security Rules on Firebase了解 Firebase 上的基本 Firebase 安全规则
【发布时间】:2021-01-21 21:15:44
【问题描述】:

我正在使用 Firebase 身份验证并阅读了很多关于安全规则、自定义声明、云功能的文档,但我真的变得更加困惑了。

考虑以下数据结构

{
    "company": {
        "idCompany1": {"data": "Restricted to Company1s users"},
        "idCompany2": {"data": "Restricted to Company2s users"}
    },
    "users": {
        "idUser1": {
            "companies": {
                "idCompany1": true
            }
        },
        "idUser2": {
            "companies": {
                "idCompany1": true,
                "idCompany2": true
            }
        }
    }
}

我想在 Firebase 控制台(Firebase 安全规则)中实现一个简单的规则,而不修改我的数据结构。

我要配置的规则是:用户只能读取或写入其所属公司中的信息(users/$idUser/companies/$idCompany === true on path公司/$idCompany)

此时我只配置了:

{
  "rules": {
    "company" : {
      ".read": "auth != null",
      ".write": "auth != null",
        }
      }
    },
    "users" : {
      "$user_id" : {
        ".read": "auth != null",
        ".write": "auth.uid === $user_id"           
  }
}

如何在 Firebase 控制台中配置此 Firebase 安全规则?

【问题讨论】:

    标签: firebase-realtime-database firebase-security


    【解决方案1】:

    听起来您正在寻找:

    {
      "rules": {
        "company" : {
          "$companyid": {
            ".read": "root.child('users').child(auth.uid).child('companies').child($companyid).val() === true"
          }
        }
      }
    }
    

    这将允许用户阅读其个人资料中列出的任何公司的/company/$companyid

    注意:您将无法读取/company 本身,就像rules are not filters

    【讨论】:

    • 是的,我想是的。我会测试它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 2016-08-19
    • 2020-08-27
    • 2020-09-06
    • 2016-07-16
    相关资源
    最近更新 更多