forked from Fluffy-Bunny-23/Wisher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
75 lines (59 loc) · 2.61 KB
/
Copy pathfirestore.rules
File metadata and controls
75 lines (59 loc) · 2.61 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
rules_version = '2';
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 is the owner
function isOwner(userId) {
return request.auth.uid == userId;
}
// Helper function to check if user is a collaborator
function isCollaborator(collaborators) {
return request.auth.uid in collaborators;
}
// Helper function to check user has read permission
function canRead(userId, collaborators) {
return isOwner(userId) || isCollaborator(collaborators);
}
// Helper function to check user has write permission
function canWrite(userId, collaborators) {
return isOwner(userId);
}
// Lists collection
match /lists/{listId} {
// Only authenticated users can create lists
allow create: if isAuthenticated();
// Only owner can read or delete their own lists
allow read, delete: if isAuthenticated() && isOwner(resource.data.ownerId);
// Only owner can update their own list
allow update: if isAuthenticated() && isOwner(resource.data.ownerId);
// Items subcollection
match /items/{itemId} {
// Only authenticated users can create items in lists they own or collaborate on
allow create: if isAuthenticated() && canRead(resource.data.ownerId, resource.data.collaborators);
// Only owner or collaborators can read items
allow read: if isAuthenticated() && canRead(resource.data.ownerId, resource.data.collaborators);
// Only owner can update or delete items
allow update, delete: if isAuthenticated() && isOwner(resource.data.ownerId);
}
// Collaborators subcollection
match /collaborators/{collaboratorId} {
// Only owner can add collaborators
allow create, delete: if isAuthenticated() && isOwner(resource.data.ownerId);
// Only owner can read collaborators
allow read: if isAuthenticated() && isOwner(resource.data.ownerId);
// No updates to collaborator records - delete and recreate instead
allow update: if false;
}
}
// User preferences collection (optional, for future use)
match /userPreferences/{userId} {
// Users can read their own preferences
allow read: if isAuthenticated() && isOwner(userId);
// Users can write their own preferences
allow write: if isAuthenticated() && isOwner(userId);
}
}
}