允许根据文档中的时间戳值读取 FireStore 规则

Allow read in FireStore rules based on timestamp value in document

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

这是一个博客网络应用程序。 例如,一位博主将博客 post 设置为在特定日期对 public 可用。

从阅读文档来看,这应该可行,但实际上不行。

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

我错过了什么??

尝试将 < 切换为 >。

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

保存安全规则后记得稍等片刻才能生效!

firebaser 在这里

我刚才试过同样的东西,发现目前不可能。

可以allow/deny 读取基于该文档的 属性 的特定文档。

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

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

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

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

该请求有一个 request.time,它是一个时间戳,并且 Firestore 允许对 timestamp <> timestamp 操作进行基本数学运算,因此您的 request.time < resource.data.date 可以工作 ;)

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

这是我在2018.09.29的个人测试

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

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

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

What's max timezone offset

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


将此规则添加到 fire-store 数据库的规则部分。

[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;
    }
  }
}