【问题标题】:firebase database rule allow read/write to specific users with specific infofirebase 数据库规则允许读取/写入具有特定信息的特定用户
【发布时间】:2016-10-18 22:52:20
【问题描述】:

我的 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'"

          }
        }
    }
}

我的目标如下:

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

    如何实现此规则设置?

【问题讨论】:

    标签: security firebase firebase-realtime-database


    【解决方案1】:

    这似乎就是你要找的东西:

    {
      "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"
          }
        }
      }
    }
    

    有了这个:

    • 教师拥有整个users节点的读写权限
    • 其他用户只有自己节点的读写权限

    【讨论】:

    • @AHC:我回滚了你的编辑。 $uid 节点是故意嵌套的,这不是错字。
    【解决方案2】:

    删除$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'"
    
            }
        }
    }
    

    【讨论】:

    • 去掉uid规则不满足OP的第一个要求:“每个用户只能读/写自己的OWN数据。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2017-12-01
    相关资源
    最近更新 更多