Firebase规则-嵌套的ifs

时间:2019-11-18 07:31:00

标签: firebase firebase-realtime-database

我想做一些嵌套的if语句,以确保每个用户的电子邮件在获得读写权限之前都得到验证。 但是我不知道该怎么做,因为如果我用

"rules" : {
    "read" : "auth.token.email_verified",
    "write" : "auth.token.email_verified"
    "other_location" :{
        "read" : true, 
        "write": false
    }
}

即使我将other_location中的write设置为false,用户也将在other_location中写入。 我不知道为什么会这样,但我的模拟显示了这一点。 有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

Firebase RTDB规则cascade from higher tiers down to the more specific ones。如果您允许对给定键(在您的情况下是数据库的根)执行读/写操作的能力,则嵌套在该键下的任何键都将共享相同的允许读/写权限,即使嵌套规则另有说明。

要解决此问题,您可以使用variables in the key's path来匹配未命名的位置。按照惯例,这些“其他键”变量称为"$other"

{
  "rules": {
    "restricted-location": {
      ".read": true,
      ".write": false
    },
    "$other": { // any key not named above at this level  
      ".read": "auth.token.email_verified",
      ".write": "auth.token.email_verified"
    }
  }
}