-
Notifications
You must be signed in to change notification settings - Fork 1
/
firestore.rules
212 lines (167 loc) · 5.61 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
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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if isStriveAdmin(userId());
}
match /striveAdmin/{userId} {
allow read: if isOwner(userId);
allow write: if false;
}
function isStriveAdmin(userId) {
return exists(/databases/$(database)/documents/striveAdmin/$(userId));
}
match /meta/version {
allow read;
}
// collectionGroup query security rules
match /{path=**}/{collection}/{doc} {
allow read: if collection == "GStakeholders"
allow read: if collection == "Spectators"
allow read: if collection == "Supports"
}
match /miscellaneous/aggregation {
allow read;
}
match /Users/{uid} {
allow read;
allow write: if isOwner(uid);
match /Notifications/{notificationId} {
allow read: if isOwner(uid)
allow write: if isOwner(uid)
}
match /Spectators/{spectatorId} {
allow read;
allow create: if isSignedIn()
allow write: if isOwner(spectatorId)
}
match /Exercises/{exerciseId} {
allow read;
allow write: if isOwner(uid)
match /Entries/{entryId} {
allow read: if isOwner(uid);
allow write: if isOwner(uid);
}
}
match /Personal/{userId} {
allow get: if isOwner(uid)
allow create: if isOwner(uid)
allow update: if isOwner(uid) && isOnlyUpdatingAllowedPersonalFields()
}
function isOnlyUpdatingAllowedPersonalFields() {
// Only "fcmTokens" can be updated.
return isSignedIn()
&& isNotUpdatingId('uid', existingData().uid);
}
}
// ===== GOALS ====
match /Goals/{goalId} {
allow read;
allow create: if isSignedIn()
allow update: if userIsGoalAdmin(goalId) || userIsGoalAchiever(goalId)
allow delete: if userIsGoalAdmin(goalId)
match /GStakeholders/{uid} {
allow read;
allow write: if userIsGoalAdmin(goalId) || isOwner(uid)
match /Reminders/{id} {
allow read: if isOwner(uid);
allow write: if isOwner(uid);
}
}
match /Milestones/{milestoneId} {
allow read;
allow write: if userIsGoalAdmin(goalId) || userIsGoalAchiever(goalId)
}
match /Story/{itemId} {
allow read;
}
match /Posts/{postId} {
allow read;
allow create: if userIsGoalAdmin(goalId) || userIsGoalAchiever(goalId)
allow update: if userIsGoalAdmin(goalId) || userIsGoalAchiever(goalId)
allow delete: if userIsGoalAdmin(goalId) || userIsGoalAchiever(goalId)
}
match /Media/{mediaId} {
allow read;
allow create: if userIsGoalAdmin(goalId) || userIsGoalAchiever(goalId)
allow update: if userIsGoalAdmin(goalId) || userIsGoalAchiever(goalId)
allow delete: if userIsGoalAdmin(goalId) || userIsGoalAchiever(goalId)
}
match /Supports/{supportId} {
allow read;
allow create: if isSignedIn()
allow update: if isSupporterOrRecipient(goalId, supportId)
allow delete: if isSupporter(goalId, supportId)
}
match /InviteTokens/{tokenId} {
allow read;
allow create: if userIsGoalAdmin(goalId)
}
match /Comments/{commentId} {
allow read;
allow write: if isSignedIn();
}
match /ChatGPT/{messageId} {
allow read;
allow write: if isSignedIn();
}
}
// ===== GOAL EVENTS ====
match /GoalEvents/{eventId} {
allow read;
}
// ===== INTEGRATIONS ====
match /Strava/{stravaId} {
allow read;
allow update: if isIntegrationOwner(stravaId);
}
// FUNCTIONS
function isSignedIn() {
return request.auth != null;
}
function userId() {
return request.auth.uid;
}
function isOwner(userId) {
return request.auth.uid == userId;
}
function isIntegrationOwner(stravaId) {
return getStravaIntegration(stravaId).userId == request.auth.uid
}
function existingData() {
return resource.data;
}
function incomingData() {
return request.resource.data;
}
function userIsGoalAdmin(goalId) {
return getGoalStakeholder(goalId).isAdmin == true;
}
function userIsGoalAchiever(goalId) {
return getGoalStakeholder(goalId).isAchiever == true;
}
function getStravaIntegration(stravaId) {
return get(/databases/$(database)/documents/Strava/$(stravaId)).data
}
function getGoalStakeholder(goalId) {
return get(/databases/$(database)/documents/Goals/$(goalId)/GStakeholders/$(request.auth.uid)).data
}
function isSupporter(goalId, supportId) {
return getSupport(goalId, supportId).supporterId == request.auth.uid
}
function isSupporterOrRecipient(goalId, supportId) {
return (getSupport(goalId, supportId).supporterId == request.auth.uid || getSupport(goalId, supportId).recipientId == request.auth.uid)
}
function getSupport(goalId, supportId) {
return get(/databases/$(database)/documents/Goals/$(goalId)/Supports/$(supportId)).data
}
// We check that the field is not sent or if field sent is equal to actual
function isNotUpdatingField(fieldName) {
return ( !incomingData().keys().hasAll([fieldName]) || incomingData()[fieldName] == existingData()[fieldName] );
}
// Checks that the user is not trying to change document id
function isNotUpdatingId(fieldName, value) {
return ( !incomingData().keys().hasAll([fieldName]) || incomingData()[fieldName] == value );
}
}
}