Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow read in FireStore rules based on timestamp value in document

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 blog web application. E.G a blogger sets a blog post to be available to the public on a certain date.

From reading the documentation this should work, but It dosn't.

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

What I am i missing ??

like image 750
Rudi Avatar asked Nov 21 '17 13:11

Rudi


2 Answers

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.

like image 158
Frank van Puffelen Avatar answered Nov 15 '22 11:11

Frank van Puffelen


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

like image 25
Henrique Doro Avatar answered Nov 15 '22 11:11

Henrique Doro