Firebase实时数据库规则

时间:2016-06-28 17:54:36

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

我使用以下规则检查只有'管理员'可以写入陷阱部分。

"traps": {
    ".read": "auth != null",
    ".write": "auth != null  && root.child('admins').child(auth.uid).val() === true",
        },

这很好用。但是,如果我将规则更改为以下内容,

"traps": {
    "$trap" : {
      ".read": "auth != null",
      ".write": "auth != null  && root.child('admins').child(auth.uid).val() === true",
       }
    },

然后我收到了一个权限错误。 W / SyncTree:侦听/陷阱失败:DatabaseError:权限被拒绝。

我改变这种结构的原因是我试图使用验证功能,但由于我无法通过第一期,我无法达到这个目的。验证。

"traps": {
    "$trap" : {
    ".read": "auth != null",
    ".write": "auth != null  && root.child('admins').child(auth.uid).val() === true",
      "trapNumber": {
         ".validate": "newData.isString()"
       }
    }
},

我确定这是一个新手理解错误,但在尝试了许多排列之后我没有取得什么进展。非常感谢您的协助。

更新20160629

Frank,管理部分如下所示

admins
   somegeneratedkey: true

1 个答案:

答案 0 :(得分:1)

这对我有用

"rules": {
    "traps": {
    ".read": "auth != null",
    ".write": "auth != null  && root.child('admins').child(auth.uid).val() === true",
    "$trap" : {
            ".validate": "newData.hasChildren(['trapNumber']) && newData.child('trapNumber').isString()"
        }
    },
    etc.

我必须将读取和写入保留在陷阱级别,因为这是我的数据库引用所使用的级别。

一小部分附加信息,isNumber()没有测试String的数值,它正在测试是否传递了一个数字字段(int)。不是我期待的行为。