【问题标题】:Firebase firestore timestamp based security rules基于 Firebase Firestore 时间戳的安全规则
【发布时间】:2018-11-27 15:54:09
【问题描述】:

在我的客户端,我正在创建一个包含“createdDate”字段之一的文档

以下是来自 Firestore 模拟器的有效负载

{
  "__name__": "/databases/(default)/documents/billing/aaaa",
  "data": {
    "createdDate": 1529237500239,
    "createdDateTimeFormat": "2018-06-12T07:00:00.000Z"
  }
}

我尝试设置安全规则以确保“createdDate”不在未来。

service cloud.firestore {
  match /databases/{database}/documents {
    match /billing/{userId}{
        allow create: if (request.resource.data.createdDate < request.time.toMillis());
    }     
  }
}

这会拒绝访问。

接下来,我尝试了以下引用时间戳格式的规则。这也会导致访问被拒绝。

service cloud.firestore {
  match /databases/{database}/documents {
    match /billing/{userId}{
        allow create: if (request.resource.data.createdDateTimeFormat < request.time);
    }     
  }
}

【问题讨论】:

  • 您不能使用 createdDateTimeFormat 之类的字符串与request.time 进行日期比较。正如我在回答中所说,request.time 是 Timestamp 类型的对象,只能与您文档中的其他 Timestamp 类型的对象进行比较。

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


【解决方案1】:

request.time 的类型为 Timestamp,正如您从 rules reference documentation 中看到的那样,但您将其用作数字。

你有两个选择。首先,您可以将 createdDate 字段更改为 Timestamp 类型的对象,这可能是更好的方法。

如果您必须将其保留为数字,则必须将 request.time Timestamp 对象转换为与您的数字度量相匹配的数字。如果您的数字自纪元以来以毫秒为单位,您可以使用toMills() 方法:

allow create: if (request.resource.data.createdDate < request.time.toMillis());

【讨论】:

  • 我将 createdDate 保留为数字并尝试了
  • 也许您可以编辑问题以显示您用于实际添加此文档的代码?
  • 我想看到的是客户端上用于添加文档的代码。
猜你喜欢
  • 2018-07-24
  • 1970-01-01
  • 2019-06-15
  • 2018-08-18
  • 2021-11-01
  • 2021-04-09
  • 1970-01-01
  • 2020-05-23
相关资源
最近更新 更多