forked from Calebnathan/Auto_Saver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
259 lines (210 loc) · 11.1 KB
/
firestore.rules
File metadata and controls
259 lines (210 loc) · 11.1 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper functions
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function hasRequiredFields(fields) {
return request.resource.data.keys().hasAll(fields);
}
function isValidString(field, minLen, maxLen) {
return request.resource.data[field] is string
&& request.resource.data[field].size() >= minLen
&& request.resource.data[field].size() <= maxLen;
}
function isValidNumber(field, min) {
return request.resource.data[field] is number
&& request.resource.data[field] >= min;
}
// Public user profiles for friend lookups
// Only contains email, displayName, and uid - safe to query
match /public_users/{userId} {
// Anyone authenticated can read to search for friends
allow read: if isAuthenticated();
// Only the user can create/update their own public profile
allow create, update: if isOwner(userId)
&& hasRequiredFields(['email', 'displayName'])
&& isValidString('email', 3, 100)
&& isValidString('displayName', 1, 200)
&& request.resource.data.keys().hasOnly(['email', 'emailLowercase', 'displayName', 'photoUrl', 'createdAt', 'updatedAt']);
allow delete: if isOwner(userId);
}
// User profile document
match /users/{userId} {
allow read: if isOwner(userId);
allow create: if isOwner(userId)
&& hasRequiredFields(['fullName', 'contact'])
&& isValidString('fullName', 1, 200)
&& isValidString('contact', 7, 20)
&& (!('contactLowercase' in request.resource.data) || request.resource.data.contactLowercase == request.resource.data.contact.lower())
&& request.resource.data.keys().hasOnly(['fullName', 'contact', 'contactLowercase', 'profilePhotoPath', 'createdAt', 'updatedAt']);
allow update: if isOwner(userId)
&& hasRequiredFields(['fullName', 'contact'])
&& isValidString('fullName', 1, 200)
&& isValidString('contact', 7, 20)
&& (!('contactLowercase' in request.resource.data) || request.resource.data.contactLowercase == request.resource.data.contact.lower())
&& (!('createdAt' in request.resource.data) || request.resource.data.createdAt == resource.data.createdAt);
// Categories subcollection
match /categories/{categoryId} {
allow read: if isOwner(userId);
allow create: if isOwner(userId)
&& hasRequiredFields(['name'])
&& isValidString('name', 1, 100)
&& request.resource.data.keys().hasOnly(['name', 'createdAt', 'updatedAt']);
allow update: if isOwner(userId)
&& hasRequiredFields(['name'])
&& isValidString('name', 1, 100)
&& (!('createdAt' in request.resource.data) || request.resource.data.createdAt == resource.data.createdAt);
allow delete: if isOwner(userId);
}
// Goals subcollection
match /goals/{goalId} {
allow read: if isOwner(userId);
allow create: if isOwner(userId)
&& hasRequiredFields(['month', 'minGoal', 'maxGoal'])
&& isValidString('month', 7, 7)
&& request.resource.data.month.matches('^\\d{4}-\\d{2}$')
&& goalId == request.resource.data.month
&& isValidNumber('minGoal', 0)
&& isValidNumber('maxGoal', 0)
&& request.resource.data.maxGoal >= request.resource.data.minGoal
&& request.resource.data.keys().hasOnly(['month', 'minGoal', 'maxGoal', 'createdAt', 'updatedAt']);
allow update: if isOwner(userId)
&& hasRequiredFields(['month', 'minGoal', 'maxGoal'])
&& goalId == request.resource.data.month
&& isValidNumber('minGoal', 0)
&& isValidNumber('maxGoal', 0)
&& request.resource.data.maxGoal >= request.resource.data.minGoal
&& (!('createdAt' in request.resource.data) || request.resource.data.createdAt == resource.data.createdAt);
allow delete: if isOwner(userId);
}
// Expenses subcollection
match /expenses/{expenseId} {
allow read: if isOwner(userId);
allow create: if isOwner(userId)
&& hasRequiredFields(['categoryId', 'date', 'amount'])
&& isValidString('categoryId', 1, 100)
&& isValidString('date', 10, 10)
&& request.resource.data.date.matches('^\\d{4}-\\d{2}-\\d{2}$')
&& isValidNumber('amount', 0.01)
&& request.resource.data.keys().hasOnly(['categoryId', 'date', 'amount', 'description', 'startTime', 'endTime', 'photoPath', 'createdAt', 'updatedAt']);
allow update: if isOwner(userId)
&& hasRequiredFields(['categoryId', 'date', 'amount'])
&& isValidString('categoryId', 1, 100)
&& isValidString('date', 10, 10)
&& isValidNumber('amount', 0.01)
&& (!('createdAt' in request.resource.data) || request.resource.data.createdAt == resource.data.createdAt);
allow delete: if isOwner(userId);
}
// Friend requests subcollection
match /friend_requests/{requestId} {
allow read: if isOwner(userId);
allow create: if isAuthenticated()
&& request.auth.uid == requestId
&& request.resource.data.fromUid == request.auth.uid
&& request.resource.data.toUid == userId
&& request.resource.data.status == 'PENDING';
allow update: if isOwner(userId);
allow delete: if isOwner(userId);
}
// Friends subcollection
match /friends/{friendUid} {
function hasAcceptedRequest() {
return exists(/databases/$(database)/documents/users/$(friendUid)/friend_requests/$(userId))
&& get(/databases/$(database)/documents/users/$(friendUid)/friend_requests/$(userId)).data.status == 'ACCEPTED';
}
allow read: if isOwner(userId);
allow create: if isOwner(userId)
|| (isAuthenticated()
&& request.auth.uid == friendUid
&& hasAcceptedRequest());
allow update, delete: if isOwner(userId);
}
}
// Collaborative Goals collection (Social feature)
match /collaborative_goals/{goalId} {
function isGoalParticipant() {
return isAuthenticated() && request.auth.uid in resource.data.participants;
}
function isGoalCreator() {
return isAuthenticated() && request.auth.uid == resource.data.createdBy;
}
// Anyone authenticated can read goals they're part of
allow read: if isGoalParticipant();
// Only authenticated users can create goals
allow create: if isAuthenticated()
&& request.auth.uid == request.resource.data.createdBy
&& request.resource.data.participants.hasOnly([request.auth.uid])
&& request.resource.data.status == 'ACTIVE'
&& hasRequiredFields(['name', 'createdBy', 'totalBudget', 'currentSaved', 'participants', 'categories', 'status'])
&& isValidString('name', 1, 100)
&& isValidNumber('totalBudget', 0.01)
&& request.resource.data.currentSaved == 0
&& request.resource.data.categories is list;
// Only participants can update goals
allow update: if isGoalParticipant();
// Only creator can delete
allow delete: if isGoalCreator();
// Contributions subcollection
match /contributions/{contributionId} {
function isGoalMember() {
return isAuthenticated()
&& request.auth.uid in get(/databases/$(database)/documents/collaborative_goals/$(goalId)).data.participants;
}
// All goal members can read contributions
allow read: if isGoalMember();
// Only the contributor can create their own contributions
allow create: if isAuthenticated()
&& request.auth.uid == request.resource.data.uid
&& isGoalMember()
&& hasRequiredFields(['goalId', 'categoryId', 'uid', 'amount', 'date'])
&& request.resource.data.goalId == goalId
&& isValidNumber('amount', 0.01)
&& isValidString('date', 10, 10);
allow update, delete: if false; // Contributions are immutable
}
}
// Challenges collection (Race feature)
match /challenges/{challengeId} {
function isParticipant() {
return isAuthenticated() && request.auth.uid in resource.data.participants;
}
function isCreator() {
return isAuthenticated() && request.auth.uid == resource.data.createdBy;
}
// Anyone authenticated can read challenges they're part of
allow read: if isParticipant();
// Only authenticated users can create challenges
allow create: if isAuthenticated()
&& request.auth.uid == request.resource.data.createdBy
&& request.resource.data.participants.hasOnly([request.auth.uid])
&& request.resource.data.status == 'PENDING'
&& hasRequiredFields(['name', 'createdBy', 'createdByEmail', 'budget', 'startDate', 'endDate', 'status', 'participants', 'inviteCode'])
&& isValidString('name', 1, 100)
&& isValidNumber('budget', 0.01)
&& isValidString('startDate', 10, 10)
&& isValidString('endDate', 10, 10)
&& isValidString('inviteCode', 6, 6);
// Creator can update status; participants can update to join
allow update: if isAuthenticated()
&& (isCreator() || request.auth.uid in request.resource.data.participants);
// Only creator can delete
allow delete: if isCreator();
// Participants subcollection
match /participants/{participantUid} {
function isChallengeParticipant() {
return isAuthenticated()
&& request.auth.uid in get(/databases/$(database)/documents/challenges/$(challengeId)).data.participants;
}
// All challenge members can read participant data
allow read: if isChallengeParticipant();
// Only the participant can write their own data
allow write: if isAuthenticated() && request.auth.uid == participantUid;
}
}
}
}