【问题标题】:Firebase - creating two tiers of usersFirebase - 创建两层用户
【发布时间】:2016-04-21 22:31:03
【问题描述】:

我正在尝试将 Firebase 用作公司情况的后端,在这种情况下,客户需要访问受限于他们创建的内容;以及需要能够查看所有数据的员工。这在 Firebase 中是否可行。例如,如果我有一组员工的 firebase uId,我无法立即看到如何创建这样的规则。

【问题讨论】:

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


    【解决方案1】:

    是的,这在 Firebase 中绝对是可能的。

    给定一个简单的 Firebase 结构:

    users
      uid_0
        name: "Rupert"
      uid_1
        name: "Buffy"
      uid_2
        name: "Zoran"
    
    admins
      uid_2: true
    
    data
      uid_0: "something interesting"
      uid_1: "look ma, no hands"
      uid_2: "Beam me up"
    

    和规则

    "data": {
      "$uid": {
        ".read": "auth != null && ( $uid == auth.uid || root.child('admins').child(auth.uid).exists() )"
        ".write": "auth != null && ( $uid == auth.uid || root.child('admins').child(auth.uid).exists() )"
      }
    }
    

    这条规则将确保用户是

    1) authenticated and
    2) the node they are accessing has their uid as a key
       or
       their uid exists in the admins node.
    

    所以 uid_1 的 Buffy 只能访问“look ma, no hands”,而拥有 uid_2(管理员)的 Zoran 可以访问任何节点。

    这可能可以简化,但它展示了这个概念。

    【讨论】:

      【解决方案2】:

      Firebase 文档有一些 recipes,其中一个显示了 role based security 实现的具体示例

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-21
        • 2018-12-02
        • 2016-09-29
        • 1970-01-01
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多