【问题标题】:Acessing adjacent data in firebase访问firebase中的相​​邻数据
【发布时间】:2018-06-04 17:44:49
【问题描述】:

让我们想象一个简单的场景:firebase 中的一个简单邮件系统,树形如下:

{
 "users":{
    "user-0001":{
       "mailbox":{
        "user-0002":{
           "DSdljkdkd333klll":{
              "message":"Hi ! I am user-0002 and I sent to user-0001 a message"
           },
           "JjkJHHH8888Gggg2":{
              "message":"Hi ! It's Me, user-0002 again !"
           }
        }
     },
     "secretStuff":"Something Private - nobody can write anything here",
     "myContacts":"This is my contact list. Obviously just me can access"
    },
    "user-0002":{
       "mailbox":{
         "user-0056":{
           "DSdljkdkd333klll":{
              "message":"Party tonight ! Don't forget !"
           }
        },
        "user-0282":{
           "3893NJJj33333eddf":{
              "message":"How are you ?"
           }
        }
     },
     "secretStuff":"Something Private - nobody can write anything here",
     "myContacts":"This is my contact list. Obviously just me can access"
  }
 }
}

我作为 user-0002 ,我可以在自己的树上写字。

好的,但是我应该能够在 user-*/mailbox/user-0002 上写信,因为我可以向我想要的任何用户发送消息。当然:我无法访问任何其他密钥。

那么,如何归档这个:我可以像上面的例子一样在我的树和相邻的树上写的规则?

【问题讨论】:

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


    【解决方案1】:

    您应该将公共数据和私有数据分别保存在不同的顶级列表中。所以:

    mailboxes
      user-0001
        user-0002:{
          "DSdljkdkd333klll":{
           "message":"Hi ! I am user-0002 and I sent to user-0001 a message"
          },
          "JjkJHHH8888Gggg2":{
           "message":"Hi ! It's Me, user-0002 again !"
          }
        }
      user-0002
        user-0056:{
          "DSdljkdkd333klll":{
           "message":"Party tonight ! Don't forget !"
          }
        },
        user-0282:{
          "3893NJJj33333eddf":{
           "message":"How are you ?"
          }
        }
    contacts
      user-0001: "This is my contact list. Obviously just me can access"
      user-0002: "This is my contact list. Obviously just me can access"
    secrets
      user-0001: "Something Private - nobody can write anything here"
      user-0002: "Something Private - nobody can write anything here"
    

    现在您可以根据信息类型保护访问,并加载特定类型的信息。

    如果您需要特定用户的所有信息,则需要从所有三个位置读取。但鉴于您通常会为特定用户执行此操作,这些读取不是可伸缩性问题。即使您需要多个用户的所有信息,从Firebase pipelines the requests over a single connection 开始通常也很快。

    另见:

    【讨论】:

    • 还是不好,弗兰克。以这种方式,例如 user-0002 可以覆盖 user-0005 的内容。当然:user-0002 可以读取任何邮箱。隔离路径很重要。
    【解决方案2】:

    在进行更多研究和测试后,我得出结论认为这应该可行...

    {
     "rules":{
      "users":{
         "$uid":{
            ".write":"$uid === auth.uid",
            ".read":"$uid === auth.uid",
            "mailbox":{
               "$uid":{
                  ".write":" $uid === auth.uid"
               }
            }
         }
      }
     }
    }
    

    这意味着:

    如果我是 user-0001,我可以在 /users/user-0001 读取/写入任何内容,但如果路径是 /users/*/mailbox/user-0001,我只能在其他地方写入

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 2017-06-09
      • 2023-03-07
      • 2017-07-15
      相关资源
      最近更新 更多