This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirestore.rules
98 lines (94 loc) · 3.88 KB
/
firestore.rules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /contacts/{contact} {
allow create:
// ensure no extra fields
if request.resource.data.keys().size() == 3
// ensure exactly these fields
&& request.resource.data.keys().hasAll(["name", "email", "createdAt"])
// ensure types are correct
&& request.resource.data.name is string
&& request.resource.data.email is string
&& request.resource.data.createdAt is timestamp
// reject special string that triggers errors
&& request.resource.data.name != "__reject_submission__";
}
match /invitees/{invitee} {
allow get:
if invitee != "[email protected]"
&& (resource == null || !resource.data.get("inactive", false));
}
match /invitations/{code} {
allow get:
if (resource == null || !resource.data.get("inactive", false));
match /rsvps/{rsvp} {
function attending() {
return request.resource.data.attending;
}
function guests() {
return request.resource.data.guests;
}
function guestsCount() {
return request.resource.data.guests.size();
}
function validEvents(events, itype) {
return
(
(!attending() && events.size() == 0)
||
// Must be kept up to date with values in Gatsby config
(attending() && (
(itype == "a" && events.hasOnly(["puja", "haldi", "sangeet", "ceremony", "reception"]))
|| (itype == "psr" && events.hasOnly(["puja", "haldi", "sangeet", "reception"]))
|| (itype == "pr" && events.hasOnly(["puja", "haldi", "reception"]))
|| (itype == "w" && events.hasOnly(["sangeet", "ceremony", "reception"]))
|| (itype == "ow" && events.hasOnly(["ceremony", "reception"]))
|| (itype == "sr" && events.hasOnly(["sangeet", "reception"]))
|| (itype == "r" && events.hasOnly(["reception"]))
))
);
}
function validGuest(i, itype) {
return
guests()[i].name.trim().size() != 0
&& validEvents(guests()[i].events, itype);
}
function validGuests(numInvitedGuests, itype) {
return
guestsCount() <= numInvitedGuests
&& validGuest(0, itype)
&& (guestsCount() < 2 || validGuest(1, itype))
&& (guestsCount() < 3 || validGuest(2, itype))
&& (guestsCount() < 4 || validGuest(3, itype))
&& (guestsCount() < 5 || validGuest(4, itype))
&& (guestsCount() < 6 || validGuest(5, itype))
&& (guestsCount() < 7 || validGuest(6, itype))
&& (guestsCount() < 8 || validGuest(7, itype))
&& (guestsCount() < 9 || validGuest(8, itype));
// should bump this if we ever have more than 9 guests in a party.
}
function validGuestsFromInvitation(invitation) {
return validGuests(
invitation.numGuests,
invitation.get("itype", "")
)
}
allow create:
// ensure fields only include the following
if request.resource.data.keys().hasOnly(["attending", "guests", "createdAt", "comments"])
// ensure these required keys are always present
&& request.resource.data.keys().hasAll(["attending", "guests", "createdAt"])
&& attending() is bool
&& request.resource.data.createdAt is timestamp
&& (
!("comments" in request.resource.data.keys())
|| request.resource.data.comments is string
)
&& guests() is list
&& guestsCount() > 0
&& validGuestsFromInvitation(get(/databases/$(database)/documents/invitations/$(code)).data);
}
}
}
}