问题
On my client side I am creating a document with one of the fields "createdDate"
below is a payload from the firestore simulator
{
"__name__": "/databases/(default)/documents/billing/aaaa",
"data": {
"createdDate": 1529237500239,
"createdDateTimeFormat": "2018-06-12T07:00:00.000Z"
}
}
I tried to set the security rule to make sure the "createdDate" is not in the future.
service cloud.firestore {
match /databases/{database}/documents {
match /billing/{userId}{
allow create: if (request.resource.data.createdDate < request.time.toMillis());
}
}
}
This gives access denied.
Next, I tried the following rule referencing the Timestamp format. That gives access denied as well.
service cloud.firestore {
match /databases/{database}/documents {
match /billing/{userId}{
allow create: if (request.resource.data.createdDateTimeFormat < request.time);
}
}
}
回答1:
request.time
is of type Timestamp, as you can see from the rules reference documentation, but you're using it as a number.
You have two options. First, you could change your createdDate field to a Timestamp type object, which is probably the better way to go.
If you must leave it as a number, you'll have to convert the request.time
Timestamp object to a number that matches the measurement of your number. If your number measured in milliseconds since epoch, you can use the toMills() method on it:
allow create: if (request.resource.data.createdDate < request.time.toMillis());
来源:https://stackoverflow.com/questions/50915616/firebase-firestore-timestamp-based-security-rules