如何在Firebase中的用户之间共享数据?

时间:2015-03-27 17:18:00

标签: firebase firebase-security

在这样的数据库中:

{
  "users": {
    "simplelogin:213": {    
      "provider": "password",
      "name": "bobtony",
      "friends": {
        "Andrew Lee": true,
        "James Tamplin": true
      }   
    },
    "twitter:123": {
      "provider": "twitter",
      "name": "Andrew Lee",
      "friends": {}
    },  
    "facebook:456": {
      "provider": "facebook",
      "name": "James Tamplin",
      "friends": {}
    }   
  },  
  "messages": {  
    "simplelogin:213": {    
       "-JkpwAnieKjQVsdtPD4m" : {
          "content" : "Hello World"
      }
    }  
  }  
}

考虑到bobtony加上Andrew Lee作为他的朋友,Andrew Lee如何阅读bobtony消息?类似的问题问here,但没有很好的解决方案。我知道我可以有一条规则允许阅读信息,但是朋友"用户只是不知道阅读它们的路径。

更新:花了一些时间思考后,我得到了:

{
  "users": {
    "user1": {  
      "name": "bobtony",
      "partners": {
        "user2": {
            "accepted": true
        }
      },
      "customers":{
        "customer1": true,
        "customer2": true
        // hundreds of clients
      },
      "orders":{
        "order1": true,
        "order2": true
        // hundreds of orders
      }     
    }
  },  
  "customers": {  
    "customer1": {  
       "name" : "james"
      }
    }
}

那么,可以这样做吗?通过这种方式,我可以遍历我自己的客户和我的合作伙伴客户。

2 个答案:

答案 0 :(得分:2)

如果你设置了这样的数据:

{
  "users": {
    "simplelogin:213": {    
      "provider": "password",
      "name": "bobtony",
      "friends": {
        "twitter:123": "Andrew Lee",
        "facebook:456": "James Tamplin"
      },
      "messages": {
        "sent" : {
          "-JkpwAnieKjQVsdtPD4m" : {
            "content" : "Hello World",
            "uid" : "twitter:123"
          }
        }
      }
    },
    "twitter:123": {
      "provider": "twitter",
      "name": "Andrew Lee",
      "friends": {
        "simplelogin:213": "bobtony"
      },
      "messages": {
        "received" : {
          "-JkpwAnieKjQVsdtPD4m" : {
            "content" : "Hello World",
            "uid" : "simplelogin:213"
          }
        }
      }
    },  
    "facebook:456": {
      "provider": "facebook",
      "name": "James Tamplin",
      "friends": {
        "simplelogin:213": "bobtony"
      }
    }   
  }
}

如果发送用户的uid在接收用户的“/ friends”中,您可以设置规则,以便一个用户可以写入另一个用户的/messages/received

{
  "rules": {
    "users" : {
      "$user_id" : {
        ".read": "auth.uid === $user_id",
        ".write": "auth.uid === $user_id",
        "messages" : {
          "received" : {
            ".write" : "root.child('users').child($user_id).child('friends').hasChild(auth.uid)"
          }
        }
      }
    }
  }
}

如果对sent的写入成功,您需要使用Firebase.transaction()来确保对received的写入成功。

答案 1 :(得分:0)

该视频很有帮助:https://www.youtube.com/watch?v=3hj_r_N0qMs 总体思路是相同的,而不是isTeacher == true,只需将它们添加到另一个用户的角色中即可(例如userX { usersWithViewAccess : userY, userZ ... }

service cloud.firestore { 
  match /databases/{database}/documents { 
    match /class/ {classId}/grades/{studentId} {
        allow read: if request.auth.uid == studentld; 
        allow write: if get(/databases/$(database)/documents/ 
        administrators/$( request. auth.uid)).data.isTeacher == true; 
        }
    }
}

以此为准

administrators: { 
    userid123: { 
        isTeacher : true 
    }
    userid009: { 
        isAdminAssistant : true 
    }
}