Allow read in FireStore rules based on timestamp value in document

前端 未结 5 1092
再見小時候
再見小時候 2020-12-11 07:11

I want to create a FireStore rule that grantes read privilages to documents after the current date has surpassed a timestamp value in the document.

This is for a bl

相关标签:
5条回答
  • 2020-12-11 07:54

    firebaser here

    I tried the same thing a while ago, and found out it isn't currently possible.

    It is possible to allow/deny read to a specific document based on a property of that document.

    It is possible to allow a query that filters documents based on a property in that document, but currently that is only possible based on request.auth.

    This means that unfortunately your filter currently can't be implemented with security rules. I recommend you file a feature request to chime in.

    Update (2018-04-24): this might now be possible with request.time, but I haven't had a chance to test yet. Have a look here.

    0 讨论(0)
  • 2020-12-11 07:57

    trying switching the < to >.

    request.time will be the time of accessing the document while resource.data.date should be the creation timestamp of the document.

    try using this for your security rules:

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

    duration.time(4, 3, 2, 1) will create a four hour, three minute, two second, one nanosecond duration.

    More information can be found at: https://firebase.google.com/docs/firestore/reference/security/#timestamp

    Do remember to wait for sometime after saving your security rules for it to take effect!

    0 讨论(0)
  • 2020-12-11 07:57

    NOTE: As this is my first answer on Stack Overflow, I wasn't allowed to comment on Frank van Pueffelen's answer, so just as a heads-up, the credits for this solution are his!

    The request has a request.time which is a timestamp, and Firestore allows for basic math operators on timestamp <> timestamp operations, so your request.time < resource.data.date will work ;)

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

    This is based on my personal testing on 2018.09.29

    0 讨论(0)
  • 2020-12-11 08:00

    Answer of @user776914 is nice but what if there are diff timezones

    Lets add +- 27 hours to be sure it was e.g. created in that day +- 1

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

    What's max timezone offset

    0 讨论(0)
  • 2020-12-11 08:05

    I wanted to do a similar thing for a game. I wanted to activate and deactivate a game object only after a particular date. I used google's cloud functions to do it. I deployed a function that runs every day to check the firestore documents and changes values according to the script.

    0 讨论(0)
提交回复
热议问题