【问题标题】:Firebase: Security rules for a collaborative appFirebase:协作应用的安全规则
【发布时间】:2016-08-28 09:19:45
【问题描述】:

我正在编写一个笔记共享应用程序,我正在尝试为数据结构找到最佳方法,以允许将协作者添加到用户笔记中,同时为相关结构制定合理的安全规则。

我现在拥有的如下:

"users": {
   "johndoe": {
      "notes": {
        "note1Key",
        "note2Key"
      }
   },
   "jane": {
      "notes": {
        "note3Key",
        "note4Key",
        "note5Key"
      }
   }
   ...
},
"notes": {
  "note1Key": {
    // actual note data
  },
  "note2Key": {
    // actual note data
  },
  "note3Key": {
    // actual note data
  },
  ...
},
"shared": {
    "johndoe" : {
        "note5Key" : true,
        "note3Key" : true   
    },
    "jane" : {
        "note1Key" : true  
    }
    ...
}

当“John Doe”创建便笺时,便笺将存储在 notes/noteKey 中,并授予所有者和所有者添加的协作者的读/写访问权限。此外,笔记的密钥存储在user/johndoe/notes/noteKey,只有他可以读取和写入。当这个用户想要在他的笔记中添加一个协作者(“Jane”)时,这个相同的笔记密钥存储在shared/jane/noteKey 中,可以全局读取和写入。这样,在列出每个用户的笔记时,我只需从 2 个位置读取即可列出用户有权访问的所有笔记:user/johndoe/notesshared/johndoe

有没有更好的方法?我不喜欢全局可访问shared 索引,我可以以某种方式限制它吗?由于一个用户可能会在不同的笔记上与大量不同的用户协作,所以我不确定如何设置安全规则,以限制对该索引的读/写访问。

我正在考虑反转 shared 节点逻辑,将音符键存储在其受人尊敬的所有者子节点下,并包括如下协作者列表:shared/jane/noteKey/collaborators/johndoe。这样我可以有一个全局读取规则和一个更严格的写入规则(每个用户只能在他自己的shared 节点中写入),但是这会大大增加列出用户有权访问的所有笔记的复杂性。

【问题讨论】:

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


    【解决方案1】:

    你想:

    1. 允许向用户备注添加所有者和协作者。
    2. 列出用户拥有的所有笔记。
    3. 列出用户有权访问的所有笔记。

    您应该已将collaborators 列表添加到每个注释中,如下所示:

    {"rules":{
    
      "users": {
        "$user_id": {
            "profile_and_settings":{
               ".write":"auth != null && auth.uid == $user_id"
            },
            "owned_notes":{
               ".write":"auth != null && auth.uid == $user_id",
               "$note_id":{}
            },
            "accesssible_notes": {
               ".write":"auth != null",
               "$note_id":{}
            }
        }
      },
    
      "notes": {
        "$note_id": {
    
            // to edit this node: must authenticated, new entry or owner of this node.
            ".write":"auth != null && ( !data.exists() || data.child('owner').val() == auth.uid )",
    
            "owner":{
                ".validate":"newData.val() == auth.uid"
            },
    
            "collaborators":{
                "$user_id":{}
            },
    
            // ... other note data
    
        }
        //...
      }
    }}
    

    查看相关问题:
    Firebase rule: Do we have better ways to manage object ownership?

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      • 2016-08-19
      • 2018-11-05
      • 2020-08-24
      相关资源
      最近更新 更多