Security firebase数据库规则允许读取/写入具有特定信息的特定用户

Security firebase数据库规则允许读取/写入具有特定信息的特定用户,security,firebase,firebase-realtime-database,Security,Firebase,Firebase Realtime Database,我的firebase数据库规则如下所示: { "rules": { "users": { "$uid": { ".read": "auth != null || root.child('users').child(auth.uid).child('role').val() == 'teacher'", ".write": "auth != null || root.child('users'

我的firebase数据库规则如下所示:

    {
      "rules": {
        "users": {
          "$uid": {

            ".read": "auth != null || root.child('users').child(auth.uid).child('role').val() == 'teacher'",
            ".write": "auth != null || root.child('users').child(auth.uid).child('role').val() == 'teacher'"

          }
        }
    }
}
我的目标如下:

    {
      "rules": {
        "users": {
          "$uid": {

            ".read": "auth != null || root.child('users').child(auth.uid).child('role').val() == 'teacher'",
            ".write": "auth != null || root.child('users').child(auth.uid).child('role').val() == 'teacher'"

          }
        }
    }
}
  • 每个用户只能读/写自己的数据
  • 只有在其名为“角色”的对应子级中定义了值“teacher”的用户才能读取/写入其他用户的数据。

    如何实现此规则设置

通过删除
$uid
这个如何:

  {
      "rules": {
        "users": {

            ".read": "auth != null || root.child('users').child(auth.uid).child('role').val() == 'teacher'",
            ".write": "auth != null || root.child('users').child(auth.uid).child('role').val() == 'teacher'"

        }
    }
}

这似乎就是你想要的:

{
  "rules": {
    "users": {
      ".read": "root.child('users').child(auth.uid).child('role').val() == 'teacher'",
      ".write": "root.child('users').child(auth.uid).child('role').val() == 'teacher'",
      "$uid": {
        ".read": "auth.uid == $uid",
        ".write": "auth.uid == $uid"
      }
    }
  }
}
为此:

  • 教师对整个
    用户
    节点具有读写权限
  • 其他用户仅对自己的节点具有读写权限

@AHC:我回滚了您的编辑。
$uid
节点是有意嵌套的,这不是打字错误。删除uid规则并不满足OP的第一个要求:“每个用户只能读/写自己的数据。”