-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfirestore.rules.example
More file actions
194 lines (157 loc) · 6.55 KB
/
firestore.rules.example
File metadata and controls
194 lines (157 loc) · 6.55 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
rules_version = '2'
service cloud.firestore {
match /databases/{database}/documents {
function userIsLoggedIn(auth) {
return auth != null
}
function userIsVerified(auth) {
return auth.token.email_verified
}
function userWillBeOwner(auth, data) {
return auth.uid == data.author.id
}
function userIsOwner(auth, currentData) {
return auth.uid == currentData.author.id
}
function userIsAdmin(auth) {
return auth.token.firebase.sign_in_provider == "google.com"
&& auth.token.email_verified
&& auth.token.email == "ADMIN-EMAIL@HOST.COM"
}
function requireModeration() {
return false
}
function requireAnonymousModeration() {
return false
}
function postIsValid(data) {
return data.contents != ""
}
function postIsNotDeleted(currentData) {
return !("deletedAt" in currentData)
}
function postContentsIsNotChanged(data, currentData) {
return data.contents == currentData.contents
}
function postDataIsNotChanged(data, currentData) {
return data.paths == currentData.paths
&& data.url == currentData.url
&& data.author == currentData.author
&& data.parentId == currentData.parentId
&& data.approvedAt == currentData.approvedAt
&& data.createdAt == currentData.createdAt
}
function postVoteIsNotChanged(data, currentData) {
return data.voted == currentData.voted
&& data.voters == currentData.voters
}
function postIsBeingDeleted(data, currentData) {
return data.contents == ""
&& postIsNotDeleted(currentData)
&& postDataIsNotChanged(data, currentData)
}
function postIsBeingEdited(data, currentData) {
return postIsValid(data)
&& postIsNotDeleted(currentData)
&& postDataIsNotChanged(data, currentData)
&& postVoteIsNotChanged(data, currentData)
}
function postCanBeVoted(data, currentData) {
return "voters" in data
&& "voted" in data
&& postIsNotDeleted(currentData)
&& postDataIsNotChanged(data, currentData)
&& postContentsIsNotChanged(data, currentData)
}
function postIsBeingVoted(auth, data, currentData) {
return postCanBeVoted(data, currentData)
&& data.voted - currentData.voted == 1
&& (auth.uid in data.voters)
&& !(auth.uid in currentData.voters)
}
function postIsBeingUnVoted(auth, data, currentData) {
return postCanBeVoted(data, currentData)
&& currentData.voted - data.voted == 1
&& !(auth.uid in data.voters)
&& (auth.uid in currentData.voters)
}
function postRepliedIsBeingIncreased(data, currentData) {
return postIsNotDeleted(currentData)
&& postDataIsNotChanged(data, currentData)
&& postContentsIsNotChanged(data, currentData)
&& postVoteIsNotChanged(data, currentData)
&& data.replied - currentData.replied == 1
}
match /check/imAdmin {
allow read: if userIsAdmin(request.auth)
}
match /check/requireModeration {
allow read: if !requireModeration()
}
match /check/requireAnonymousModeration {
allow read: if !requireAnonymousModeration()
}
match /pending_posts/{document=**} {
// allows only admin can see list
allow list: if userIsAdmin(request.auth)
// allows owner can read
allow read: if userIsOwner(request.auth, resource.data)
// allows admin can read
allow read: if userIsAdmin(request.auth)
// allows user can create
allow create: if userIsLoggedIn(request.auth)
&& postIsValid(request.resource.data)
&& userWillBeOwner(request.auth, request.resource.data)
// allows admin can update
allow update: if userIsAdmin(request.auth)
// allows admin can delete
allow delete: if userIsAdmin(request.auth)
}
match /posts/{document=**} {
// allows anyone can see list
allow list: if true
// allows anyone can read
allow read: if true
// allows admin to create post
allow create: if userIsAdmin(request.auth)
// allows user to create post
allow create: if userIsVerified(request.auth)
&& !requireModeration()
&& postIsValid(request.resource.data)
&& userWillBeOwner(request.auth, request.resource.data)
// allows anonymous to create post
allow create: if userIsLoggedIn(request.auth)
&& !requireAnonymousModeration()
&& postIsValid(request.resource.data)
&& userWillBeOwner(request.auth, request.resource.data)
// allows admin to edit post
allow update: if userIsAdmin(request.auth)
&& postIsBeingEdited(request.resource.data, resource.data)
// allows owner to edit post
allow update: if userIsOwner(request.auth, request.resource.data)
&& postIsBeingEdited(request.resource.data, resource.data)
&& (
(userIsVerified(request.auth) && !requireModeration())
|| (userIsLoggedIn(request.auth) && !requireAnonymousModeration())
)
// allows admin / owner to delete post
allow update: if (userIsAdmin(request.auth) || userIsOwner(request.auth, resource.data))
&& postIsBeingDeleted(request.resource.data, resource.data)
// allows user to vote post
allow update: if userIsVerified(request.auth)
&& (
(postIsBeingVoted(request.auth, request.resource.data, resource.data))
|| (postIsBeingUnVoted(request.auth, request.resource.data, resource.data))
)
// allows user to increase post reply
allow update: if userIsVerified(request.auth)
&& !requireModeration()
&& postRepliedIsBeingIncreased(request.resource.data, resource.data)
// allows anonymous to increase post reply
allow update: if userIsLoggedIn(request.auth)
&& !requireAnonymousModeration()
&& postRepliedIsBeingIncreased(request.resource.data, resource.data)
allow delete: if userIsAdmin(request.auth)
}
}
}