【问题标题】:Firebase security rule for array of messages消息数组的 Firebase 安全规则
【发布时间】:2014-12-19 06:05:11
【问题描述】:

我正在尝试根据收件人显示邮件列表,但现在,让我们保持简单。我只是想显示一个消息列表。

我的规则是这样的

{
"rules": {
  "communications" : {
    "$communication":{
      ".read" : true,
      ".write": true
    }
  }
}

由于某种原因,我的应用程序不想阅读它

fireRef = new Firebase(url);
fireRef.auth(MY_TOKEN);
commsRef = fireRef.child('communications')
$scope.communications = $firebase(commsRef)

只有当我有一个看起来像这样的规则时它才有效

{
"rules": {
  "communications" : {
    ".read" : true,
    ".write": true
  }
}

但这会导致问题,因为我想在我的通信的子节点上添加条件。比如:

{
"rules": {
  "communications" : {
    ".read" : true, ### I would like to get rid of this line as well and have the child handling it
    ".write": true,

    "$communication":{
      ".read" : "data.child('to').val() == auth.uid"
    }
  }
}

我假设这是因为我在通信中有一个 $firebase,它需要一些读取或写入规则,但是当添加新消息时如何获取事件

谢谢

【问题讨论】:

    标签: angularjs firebase angularfire firebase-security


    【解决方案1】:

    关于安全规则,Firebase 操作是孤注一掷。

    这意味着发送到客户端的数据列表永远不会是不完整的,或者是完整服务器数据的过滤视图。因此,在使用第一组安全规则时尝试加载/communications 的所有数据将失败,即使您确实有权读取其中的一些数据,这些数据由/communications/$communication 的子规则管理。

    要处理此用例,请考虑重组您的数据,以便每个通信都按收件人进行索引,即/communications/$recipient/$communication,这将简化您的安全规则。

    此外,您甚至可以让接收者(即.read: auth.id == $recipient)将该存储桶设为只读,同时允许任何人向该用户(即.write: auth != null && !data.exists())发送消息。最后一条规则确保发送客户端经过身份验证并写入尚不存在的位置,例如新的推送 ID。

    【讨论】:

    • 我刚刚浏览了您的结构化数据指南。 firebase.com/docs/web/guide/structuring-data.html 并想知道我是否犯了错误。显然我做到了。谢谢 这解释了为什么聊天需要一个房间,即使是 121 我猜也是如此。
    • 有一个communications/$sender/$recipient/$communication 有意义还是太嵌套了?
    • @Matthieu 不用担心在 Firebase 中“过于嵌套”,因为我们的嵌套限制非常高,而且性能不会受到影响。关键是构建您的数据,使其与您的访问模式相匹配。
    猜你喜欢
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    相关资源
    最近更新 更多