【问题标题】:Firebase Database Rules: different rule for parent and childFirebase 数据库规则:父子规则不同
【发布时间】:2017-09-11 17:03:47
【问题描述】:

我的数据库是这样的:

user123 是管理员。因此,他应该能够遍历 entries 中的所有节点。除非 entryID 的 uidauth.uid

,否则其他人无法看到 entries 的子项

我该如何为此制定规则?如果没有可能的方法,任何更改数据库的建议:)

【问题讨论】:

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


    【解决方案1】:

    如果您已经知道 admin 是,在您的问题 user123 中。那么你的数据库规则应该是这样的

    "entities": { 
      "$entryId":{
          // you don't what others to see other to see teh data
        ".read": "auth.uid == 'user123'"
          // any one who is logged in should write to the /entries node
        ".write": "auth.uid != null"
      }
    }
    

    如果你想让规则更加动态,那么你可以做

    "entities": {
      "$entityId":{
          // you don't what others to see other to see teh data
          ".read": "root.child('users').child(auth.uid).child('isAdmin').val() == true || root.child('entities').child($entityId).child('uid').val() == auth.uid"
          // any one who is logged in should write to the /entries node
          ".write": "auth.uid != null"
      }
    }
    

    您可以从这里获得更多信息https://firebase.google.com/docs/reference/security/database/

    或者,您可以将条目模型更改为用户特定的

    {
      "entities" :{
         "user465": {
           "entry456": {
             "text" : "Some sample text"
           }
         }
       }
    }
    

    在这种情况下,你写你的规则

    "entities": {
      "$userId":{
         // you don't what others to see other to see teh data
         ".read": "root.child('users').child(auth.uid).child('isAdmin').val() == true || $userId == auth.uid"
         // any one who is logged in should write to the /entries node
         ".write": "auth.uid == $userId"
      }
    }
    

    【讨论】:

    • 规则是动态的。使用此规则,创建条目的用户无法阅读它。例如,“user123”应该能够读取“entry456”
    • @Nameer 查看更新后的答案,您也可以更改条目模型。
    • 但管理员无法遍历所有条目,它显示“权限被拒绝”错误。
    • @npk 你可以添加一个额外的规则;如果一条规则拒绝,则将检查以下规则。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多