如何指定Firebase数据库规则“超出范围”?

时间:2018-11-25 18:39:59

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

我的数据库结构现在看起来像这样:

database: {
  users: {
    uniqueUserId1: {
      name: "an user",
      company: "Company1",
      rank: "manager"
    }
  }
  rota: {
    Company1: {
      2018: "rota for 2018"
    }
  }
}

因此,我不想让用户阅读所有公司的名册,而只是他们拥有公司的名册。

对于用户规则,这并不难:

rules: {
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    },

    //But a bit complicated for things "outside the scope".
    //What I want is something like:

    "rota": {
      "$company": {
        ".read": "$company === users[auth.uid].company",
        ".write": "$company === users[auth.uid].company && users[auth.uid].rank === 'manager'"
      }
    }
  }
}

我知道这些规则不起作用,但是如何解决此问题?

1 个答案:

答案 0 :(得分:0)

您需要以root开头来构建用户数据的路径。所以您正在寻找这样的东西:

".read": "$company === root.child('users').child(auth.uid).child('company').val()",

另请参阅: