【问题标题】:firebase security api read permission deniedfirebase 安全 api 读取权限被拒绝
【发布时间】:2014-06-17 02:17:35
【问题描述】:

我发现了一个使用 firebase 安全 API here 的基于权限的聊天室的简洁小示例

注意"chat": { // the list of chats may not be listed (no .read permissions here)

实际上,当我加载用户的收件箱时,我需要列出用户所属的聊天记录,但我似乎无法正确获取 .read 规则。

我尝试使用以下规则,这完全有道理但不起作用:

 "convos": {
     ".read" : "auth != null && data.child('users').hasChild(auth.id)",

我怀疑问题在于 convo 和用户之间仍然存在一个级别.. aka 这样做更有意义:

 "convos": {
     ".read" : "auth != null && data.child($key + '/users').hasChild(auth.id)", 
     $key : { ... }

但这是不允许的,就是抱怨 $key 还不存在。

如何允许用户使用此设置提取他们所属的所有 convos?

【问题讨论】:

    标签: firebase angularfire firebase-security


    【解决方案1】:

    您不能使用安全规则来过滤数据。通常,您的数据结构将在很大程度上取决于您的特定用例——最直接地取决于数据将如何被读回。

    一般的解决方案是将您的用户所属的聊天与批量聊天数据分开列出,即heavily denormalize,并单独访问聊天。

    /messages/$chat_id/... (messages chronologically ordered using push() ids)
    /chats/$chat_id/... (meta data)
    /my_chats/$user_id/$chat_id/true (the value here is probably not important)
    

    现在要访问我的所有聊天记录,我可以执行以下操作:

    var fb = new Firebase(URL);
    
    fb.child('my_chats/'+myUserId).on('child_added', function(snap) {
       var chatID = snap.name());
       loadChat(chatID);
    });
    
    function loadChat(chatID) {
       fb.child('messages/'+chatID).on('child_added', function(snap) {
          console.log('new message', chatID, snap.val());
       });
    }
    

    您仍然需要安全规则来验证聊天消息的结构,以及访问用户的聊天列表等。但是过滤功能可以通过这样的索引或其他创造性的数据结构来完成。

    【讨论】:

      【解决方案2】:

      我不完全确定您是如何构建 Firebase 的,但这可能会奏效:

       "convos": {
           $key : {
            ".read" : "auth != null && data.child('users').hasChild(auth.id)",
            ... 
           }
      

      【讨论】:

      • 这似乎不允许我阅读 convos 列表,如该代码的评论中所述
      猜你喜欢
      • 2016-11-27
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      相关资源
      最近更新 更多