唯一的IP写入权限Firebase集合

Dan*_*nze 2 javascript angularjs firebase angularfire

假设我有一些文章:

https://example.firebaseio.com/articles/$key
Run Code Online (Sandbox Code Playgroud)

我想添加一个viewCountercurrentUsersCounter孩子们articles.

https://example.firebaseio.com/articles/$key/viewCounter
https://example.firebaseio.com/articles/$key/currentUsersCounter
Run Code Online (Sandbox Code Playgroud)

我在用户滚动到下一篇文章时随时执行一个函数:

var currentArticle = function(key){
  //key == index of the article
  //This is where I want to increment the `viewCounter` and `currentUsersCounter`
}
Run Code Online (Sandbox Code Playgroud)

显然,我不希望任何人写这两个孩子的东西.

  • 如何将当前(黑名单所有写入)的安全规则扩展为仅针对这些特定集合的白名单写入?

  • 如何在这些列入白名单的集合的安全规则中限制对唯一IP地址的写入?(如果可能的话)

目前黑名单所有写入:

{
  "rules": {
    ".read": true,
    ".write": "auth.email == 'example@gmail.com'"
  }
}
Run Code Online (Sandbox Code Playgroud)

Ana*_*ant 5

您不能以保护数据完整性的方式执行此操作(即确保计数实际上是准确的),因为如果您授予对这些字段的写访问权限,则客户端可以编写它想要的任何值.

但是,您可以通过在安全规则中使用变量,仅为那些特定子项提供粒度读/写访问:

{
  "articles": {
    "$key": {
      "viewCounter": {
        ".write": true,
        ".validate": "newData.isNumber() && newData.val() == data.val() + 1"
      },
      "$other": {
        ".write": "auth.email == 'example@gmail.com'"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

无法根据IP地址进行任何过滤.您将希望使用来自可信服务器代码的秘密来执行注释中建议的操作.