Firebase数据库规则:父和子的不同规则

时间:2017-09-11 17:03:48

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

我的数据库是这样的:

Firebase Rules ScreenShot

user123 是管理员。因此,他应该能够遍历条目中的所有节点。其他人看不到条目的孩子,除非entryID的 uid auth.uid

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

1 个答案:

答案 0 :(得分:2)

如果你已经知道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"
  }
}