【问题标题】:Sharing Between Clients in Firebase在 Firebase 中的客户端之间共享
【发布时间】:2013-09-16 05:05:19
【问题描述】:

我想知道是否有可能实现 Firebase 安全性,以允许我的应用程序的每个用户完全访问他们自己位置的数据,同时允许他们自己的用户启用或禁用该类型其他用户访问他们自己的数据?或者基本上,是否可以在我的应用程序的用户之间以严格执行的方式实现简单的共享、Dropbox 或 Google Drive 样式?

【问题讨论】:

    标签: firebase firebase-security


    【解决方案1】:

    由于 Firebase security rules 允许您引用 Firebase 中的数据,因此您可以将安全规则建立在您可以为其创建数据的任何内容上。所以是的,您可以允许用户以您可以计划的任何方式共享他们自己的数据。

    为了设计一个基于 Dropbox 想法的简化示例,我可以在我的数据下有一个“共享”文件夹,以及一个用于存储访问权限的安全文件夹:

    /security/$user_id/$friend/...   // where I put the access rights
    /folders/$user_id/shares/...     // where I put the shared files
    

    现在我可以通过将用户名和他们可以访问的文件夹列表放入我的 Firebase 数据来控制对它的访问:

    /security/$user_id/$friend_id = /never/gonna/give/you/up = true
    

    现在在我的安全规则中,我可以这样写:

    {
       "security": {
          "$user_id": { // only authenticated user may read/write his rules
             "shares": {
                ".read": "auth.id === $user_id",
                ".write": "auth.id === $user_id"
             }
          }
       }
       "folders": {
          "$user_id": {
             // only authenticated user may read/write his folders
             ".read": "auth.id === $user_id",
             ".write": "auth.id === $user_id",
             "shares": {
                // but my friends can read data in shares
                ".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares').val() === true"
             }
          }
       }
    }
    

    请注意,对此的一个限制(暂时)是安全规则不能递归或以任何嵌套方式工作。但是,由于规则是允许的(如果路径的任何父级允许访问,则允许访问),您可以解决此问题。

    您可能需要对子路径的最大数量设置硬性限制,并在规则中手动声明它们,如下所示:

    // allow sharing up to 3 levels deep
    "shares": {
       ".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares').val() === true",
       "$child1": {
          ".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares/'+$child1).val() === true",
          "$child2": {
             ".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares/'+$child1+'/'+$child2).val() === true",
             "$child3": {
                ".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares/'+$child1+'/'+$child2+'/'+$child3).val() === true",
             }
          }
       }
    }
    

    这不是最漂亮的东西,但在 Firebase 获得一些嵌套功能之前是一个很好的临时解决方案。

    【讨论】:

    • 考虑到 firebase 的变化,这仍然是最佳答案吗?
    • 我知道这已经很老了,但是"security" 节点中的".read": "auth.id === $user_id" 不会阻止检查给定用户是否通过@ 将当前auth.id 添加到"shares" 的可能性首先是987654329@?
    • 不,只要 $userid 与您的身份验证 ID 匹配,您就可以读取 security/$userid/shares 下的任何内容。请注意,现在是auth.uid
    猜你喜欢
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2019-04-02
    • 2016-03-01
    • 2016-12-26
    • 1970-01-01
    相关资源
    最近更新 更多