【问题标题】:Firestore security rules to allow querying documents within maximum gap of 31 days between start and end dateFirestore 安全规则允许在开始和结束日期之间的最大间隔 31 天内查询文档
【发布时间】:2020-08-08 21:06:26
【问题描述】:

我有这样的文件:

timesheets (collection)
-- auto_doc_id_1 (document): {
   start_time: yyyy:mm-dd hh:mm:ss
   duration: int

-- auto_doc_id_2 (document): {
   start_time: yyyy:mm-dd hh:mm:ss
   duration: int

....

我像这样查询它:

firestore.collection("timesheets")
.where("start_time", ">", start_from)
.where("start_time", "<", start_to)
.orderBy("start_time")
.get();

我想添加安全规则,其中start_fromstart_to 之间的最大差异应小于/等于 31 天。

【问题讨论】:

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


    【解决方案1】:

    我认为这在安全规则中是不可能的,甚至在查询中也是不可能的,因为不可能在条件值中使用对字段的引用。

    解决此问题的常用方法是将两个日期之间的间隔存储为单独的字段。然后,您可以强制该时间间隔是安全规则中的正确值,并在该时间间隔字段上进行查询。

    【讨论】:

      【解决方案2】:

      这不可能在查询中单独为每个文档强制执行安全规则。您可以要求查询中提供的值相对于彼此具有一定的限制,但您不能对彼此分开的每个文档执行此操作。

      您唯一可行的选择是将开始和结束之间的时间距离写为文档中自己的字段,并将其用作查询和规则中的过滤器。

      【讨论】:

        猜你喜欢
        • 2021-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-20
        • 1970-01-01
        相关资源
        最近更新 更多