Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
343 lines (291 loc) · 15.2 KB
/
Copy pathfirestore.rules
File metadata and controls
343 lines (291 loc) · 15.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
rules_version = '2';
// Deploy: firebase deploy --only firestore:rules (from project root)
service cloud.firestore {
match /databases/{database}/documents {
// Helper function to check if user is authenticated
function isAuthenticated() {
return request.auth != null;
}
// Helper function to check if user owns the document
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
// Helper function to check if this is the test user ID (for simulator testing)
function isTestUser(userId) {
return userId == 'simulator-test-user-shared';
}
// Same doc ID as app code: sorted UIDs joined with underscore (see FirebaseSyncService.friendDocId)
function friendPairId(uid1, uid2) {
return uid1 < uid2 ? uid1 + '_' + uid2 : uid2 + '_' + uid1;
}
// True if both UIDs have an accepted friendship document in /friends/{pairId}
function areAcceptedFriends(readerUid, ownerUid) {
let pairId = friendPairId(readerUid, ownerUid);
return exists(/databases/$(database)/documents/friends/$(pairId))
&& get(/databases/$(database)/documents/friends/$(pairId)).data.status == 'accepted';
}
// Friend may read another user's reading plan only when the plan is marked shared
function canFriendReadSharedReadingPlan(readerUid, ownerUid) {
return isAuthenticated()
&& readerUid == request.auth.uid
&& areAcceptedFriends(readerUid, ownerUid);
}
// Allow test collection for testing/debugging (top-level)
// This allows creating test documents without authentication for development
match /testConnection/{document=**} {
allow read, write: if true; // Allow all access for testing
}
// Users collection - users can read/write their own data
match /users/{userId} {
// Read: any authenticated user (for Faith Friends search); Write: own doc or test user
allow read: if isAuthenticated() || isTestUser(userId);
allow write: if isOwner(userId) || isTestUser(userId);
// User's journal entries
match /journalEntries/{entryId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User's prayer requests
match /prayerRequests/{requestId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User's mood entries
match /moodEntries/{entryId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User's bookmarked verses
match /bookmarkedVerses/{verseId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User's Bible highlights
match /bibleHighlights/{highlightId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User's Bible notes
match /bibleNotes/{noteId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User's reading plans: owner (and test id) read/write; accepted friends may read if sharedWithFriends
match /readingPlans/{planId} {
allow read: if isOwner(userId) || isTestUser(userId)
|| (canFriendReadSharedReadingPlan(request.auth.uid, userId)
&& resource.data.sharedWithFriends == true);
allow create, update, delete: if isOwner(userId) || isTestUser(userId);
}
// User's session invitations (personal collection)
match /sessionInvitations/{invitationId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User's live sessions
match /liveSessions/{sessionId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User's devotional completions
match /devotionalCompletions/{completionId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// Friend-visible prayer mirror (owner writes; friends may update intercessorIds when they pray)
match /sharedPrayers/{prayerId} {
allow read: if isAuthenticated() || isTestUser(userId);
allow create, delete: if isOwner(userId) || isTestUser(userId);
allow update: if isOwner(userId) || isTestUser(userId)
|| (isAuthenticated() && resource.data.isSharedWithFriends == true);
}
// Owner receives an alert doc when someone prays (intercessor creates under owner's uid)
match /prayerIntercessorAlerts/{alertId} {
allow read, delete: if isOwner(userId) || isTestUser(userId);
allow create: if (isAuthenticated() && request.auth.uid == request.resource.data.intercessorId)
|| request.resource.data.intercessorId == 'simulator-test-user-shared';
allow update: if false;
}
// Test connection collection (for debugging)
match /testConnection/{testId} {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// Catch-all for any other subcollections - SECURE BY DEFAULT
match /{document=**} {
// Only allow if user owns the parent user document OR if it's the test user
allow read, write: if isOwner(userId) || isTestUser(userId);
}
}
// Invite code index (document id = invite code) - for join-by-code lookup
// Read: public. Writes use setData(merge: true) (create + update); rules must allow
// updates when the merged `hostId` still matches the signed-in user (not only the pre-merge resource).
match /sessionInviteCodes/{inviteCode} {
allow read: if true;
// Create, or idempotent re-publish: merged doc must name the caller as host (or shared simulator test id for dev).
allow create: if (isAuthenticated() && request.resource.data.hostId == request.auth.uid)
|| request.resource.data.hostId == 'simulator-test-user-shared';
// Merge updates: allow when the *result* keeps ownership with request.auth, including:
// - re-save by same host, or
// - first merge field population (legacy doc missing hostId), or
// - migrate index from shared simulator id → real auth uid
allow update: if
(isAuthenticated() && request.resource.data.hostId == request.auth.uid
&& ( !('hostId' in resource.data)
|| resource.data.hostId == request.auth.uid
|| resource.data.hostId == 'simulator-test-user-shared' ))
|| (request.resource.data.hostId == 'simulator-test-user-shared'
&& ( !('hostId' in resource.data) || resource.data.hostId == 'simulator-test-user-shared' ));
// Delete: only the stored host
allow delete: if (isAuthenticated() && resource.data.hostId == request.auth.uid)
|| resource.data.hostId == 'simulator-test-user-shared';
}
// Public session invitations collection (for joining by code)
// Anyone can read (to join by code), but only authenticated users can write
// Users can only write invitations where they are the host
match /sessionInvitations/{inviteCode} {
// Allow anyone to read (for joining by code)
allow read: if true;
// Allow authenticated users to write, but only if they're the host
allow create: if isAuthenticated() &&
request.resource.data.hostId == request.auth.uid;
// Allow users to update their own invitations
allow update: if isAuthenticated() &&
(resource.data.hostId == request.auth.uid ||
request.resource.data.hostId == request.auth.uid);
// Allow users to delete their own invitations
allow delete: if isAuthenticated() &&
resource.data.hostId == request.auth.uid;
}
// Live sessions collection (for public discovery and join-by-code)
// Host can create/delete; host and participants can update (e.g. participant count when leaving)
match /liveSessions/{sessionId} {
allow read: if true;
allow create: if (isAuthenticated() && request.resource.data.hostId == request.auth.uid)
|| request.resource.data.hostId == 'simulator-test-user-shared';
allow update: if isAuthenticated();
allow delete: if (isAuthenticated() && resource.data.hostId == request.auth.uid)
|| resource.data.hostId == 'simulator-test-user-shared';
}
// Public sessions collection (for cross-user session lookups - legacy)
match /publicSessions/{sessionId} {
// Allow anyone to read (for joining sessions)
allow read: if true;
// Allow authenticated users to create sessions where they are the host
allow create: if isAuthenticated() &&
request.resource.data.hostId == request.auth.uid;
// Allow users to update their own sessions
allow update: if isAuthenticated() &&
(resource.data.hostId == request.auth.uid ||
request.resource.data.hostId == request.auth.uid);
// Allow users to delete their own sessions
allow delete: if isAuthenticated() &&
resource.data.hostId == request.auth.uid;
}
// Sessions collection (for chat messages and participants)
match /sessions/{sessionId} {
// Allow anyone to read session metadata (for joining)
allow read: if true;
// Only authenticated users can write session metadata
allow write: if isAuthenticated();
// Chat messages subcollection
match /messages/{messageId} {
// Allow anyone in the session to read messages
allow read: if true;
// Allow authenticated users to create messages (userId must be self)
allow create: if isAuthenticated() &&
request.resource.data.userId == request.auth.uid;
// Allow any authenticated user to update (so anyone can add reactions to any message)
allow update: if isAuthenticated();
// Allow users to delete their own messages
allow delete: if isAuthenticated() &&
resource.data.userId == request.auth.uid;
}
// Participants subcollection (presence, mute state for live stream)
match /participants/{participantId} {
// Allow anyone to read participants (to see who's in the session; participants listen for host mute)
allow read: if true;
// Allow authenticated users to create their own participant entry
allow create: if isAuthenticated() &&
request.resource.data.userId == request.auth.uid;
// Update: own entry (participant) OR session host (writes isMuted for remote mute)
allow update: if isAuthenticated() && (
resource.data.userId == request.auth.uid ||
get(/databases/$(database)/documents/sessions/$(sessionId)).data.hostId == request.auth.uid
);
// Allow users to delete their own participant entry
allow delete: if isAuthenticated() &&
resource.data.userId == request.auth.uid;
}
// Presentation state (Bible study / shared content) - host writes, everyone reads
match /presentation/{docId} {
allow read: if true;
allow write: if isAuthenticated();
}
// Live session UI controls (e.g. broadcast "open floor" for co-presenting).
// App path: sessions/{sessionId}/liveControls/state — fields: broadcastOpenFloor (bool), updatedAt (timestamp).
match /liveControls/{docId} {
allow read: if true;
allow create, update: if isAuthenticated() &&
get(/databases/$(database)/documents/sessions/$(sessionId)).data.hostId == request.auth.uid;
allow delete: if isAuthenticated() &&
get(/databases/$(database)/documents/sessions/$(sessionId)).data.hostId == request.auth.uid;
}
}
// User channels
match /users/{userId}/channel/info {
allow read, write: if isOwner(userId) || isTestUser(userId);
}
// User search profiles - for Faith Friends in-app search (prefix search by name)
// Read allowed for all (including simulator/macOS demo) - display names are for discovery
match /userSearchProfiles/{userId} {
allow read: if true;
allow create, update, delete: if isOwner(userId) || isTestUser(userId);
}
// Friend codes - lookup by code to send friend request (like invite codes)
// Same simulator pattern as sessionInviteCodes/liveSessions: no auth token in simulator demo,
// but app uses userId simulator-test-user-shared (see FirebaseSyncService.getCurrentUserId).
match /friendCodes/{code} {
allow read: if true;
allow create: if (isAuthenticated() && request.resource.data.userId == request.auth.uid)
|| request.resource.data.userId == 'simulator-test-user-shared';
allow update, delete: if (isAuthenticated() && resource.data.userId == request.auth.uid)
|| resource.data.userId == 'simulator-test-user-shared';
}
// Friends / friend requests
// Read: allow if doc doesn't exist (app does getDocument before create) or user is userA/userB
// Create: allow if authenticated and requester is one of userA or userB (Firebase Auth UIDs)
match /friends/{friendId} {
allow read: if isAuthenticated() && (
!exists(/databases/$(database)/documents/friends/$(friendId)) ||
resource.data.userA == request.auth.uid ||
resource.data.userB == request.auth.uid ||
resource.data.userA == 'simulator-test-user-shared' ||
resource.data.userB == 'simulator-test-user-shared'
);
allow create: if isAuthenticated() &&
(request.resource.data.userA == request.auth.uid || request.resource.data.userB == request.auth.uid);
allow update, delete: if isAuthenticated() &&
(resource.data.userA == request.auth.uid || resource.data.userB == request.auth.uid);
}
// Friend session alerts - when a friend creates a live session (for notifications)
match /users/{userId}/friendSessionAlerts/{alertId} {
allow read, delete: if isOwner(userId);
allow create: if isAuthenticated();
}
// Public channels collection (for discovery)
match /channels/{channelId} {
allow read: if true;
allow write: if (isAuthenticated() && channelId == request.auth.uid) || isTestUser(channelId);
}
// HLS / broadcast live chat (LiveStreamChatService) — path: liveStreamChats/{sessionId}/messages
match /liveStreamChats/{sessionId}/messages/{messageId} {
allow read: if true;
allow create: if isAuthenticated() &&
request.resource.data.userId == request.auth.uid &&
request.resource.data.keys().hasAll([
'sessionId', 'userId', 'userName', 'text', 'messageUUID', 'createdAt'
]) &&
request.resource.data.sessionId is string &&
request.resource.data.sessionId == sessionId &&
request.resource.data.text is string &&
request.resource.data.text.size() > 0 &&
request.resource.data.text.size() < 2000;
allow update, delete: if false;
}
// Default deny all other access
match /{document=**} {
allow read, write: if false;
}
}
}