【问题标题】:Allow read in FireStore rules based on timestamp value in document允许根据文档中的时间戳值读取 FireStore 规则
【发布时间】:2018-05-04 23:23:00
【问题描述】:

我想创建一个 FireStore 规则,在当前日期超过文档中的时间戳值后授予文档读取权限。

这是一个博客网络应用程序。 例如,博主将博文设置为在特定日期向公众开放。

通过阅读文档,这应该可以工作,但它没有。

service cloud.firestore {
  match /databases/{database}/documents {
    match /Articles/{article}{
   allow read: if request.time.date() < resource.data.date
    }
  }
}

我错过了什么?

【问题讨论】:

  • 你是如何保存你的约会的?

标签: firebase firebase-security google-cloud-firestore


【解决方案1】:

firebaser 在这里

我不久前尝试过同样的事情,但发现目前不可能。

可以根据特定文档的属性允许/拒绝读取该文档。

可以允许根据文档中的属性过滤文档的查询,但目前只能基于request.auth

这意味着很遗憾,您的过滤器目前无法使用安全规则实施。我建议你file a feature request 加入。

更新 (2018-04-24):这可能现在可以使用request.time,但我还没有机会测试。看看here

【讨论】:

  • 感谢弗兰克的回复。我将提交功能请求?
  • 对基于日期的规则有帮助吗?
【解决方案2】:

尝试将 。

request.time 将是访问文档的时间,而 resource.data.date 应该是文档的创建时间戳。

尝试将其用于您的安全规则:

    allow read: if request.time > (resource.data.timestampPropertyName + duration.time(1, 0, 0, 0));

duration.time(4, 3, 2, 1) 将创建一个四小时三分两秒一纳秒的持续时间。

更多信息请访问: https://firebase.google.com/docs/firestore/reference/security/#timestamp

记得在保存安全规则后等待一段时间才能生效!

【讨论】:

  • 在文档中哪里提到 duration 是一个全局对象,而 time 是一个方法?谢谢
【解决方案3】:

注意:因为这是我在 Stack Overflow 上的第一个答案,所以我不允许对 Frank van Pueffelen 的答案发表评论,因此请注意,此解决方案的功劳是他的!

请求有一个request.time,它是一个时间戳,Firestore 允许在timestamp &lt;&gt; timestamp 操作上使用基本的数学运算符,所以你的request.time &lt; resource.data.date 可以工作;)

service cloud.firestore {
  match /databases/{database}/documents {
    match /Articles/{article}{
   allow read: if request.time < resource.data.date
    }
  }
}

这是基于我在 2018.09.29 的个人测试

【讨论】:

    【解决方案4】:

    @user776914 的答案很好,但如果有不同的时区怎么办

    让我们添加 +- 27 小时以确保它是例如当天创建 +- 1

    duration.abs(request.time - request.resource.data.created) < duration.value(27, 'h')
    

    What's max timezone offset

    【讨论】:

    【解决方案5】:

    我想为游戏做类似的事情。我只想在特定日期之后激活和停用游戏对象。我使用谷歌的云功能来做到这一点。我部署了一个每天运行的函数来检查 firestore 文档并根据脚本更改值。

    【讨论】:

      【解决方案6】:

      将此规则添加到 fire-store db 的规则部分。

      [https://firebase.google.com/docs/firestore/security/get-started][1]

       service cloud.firestore {
        match /databases/{database}/documents {
          match /{document=**} {
            allow read, write: if request.auth != null;
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-08-06
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-05
        • 1970-01-01
        相关资源
        最近更新 更多