-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
185 lines (143 loc) Β· 5.33 KB
/
firestore.rules
File metadata and controls
185 lines (143 loc) Β· 5.33 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
/* =========================
π§ Helper Functions
========================== */
function isSignedIn() {
return request.auth != null;
}
function isUser(uid) {
return isSignedIn() && request.auth.uid == uid;
}
function isOwner(field) {
return isSignedIn() && request.auth.uid == field;
}
/* =========================
π€ USERS
========================== */
match /users/{userId} {
allow read: if isSignedIn();
allow create, update: if isUser(userId);
allow delete: if false;
match /likedPosts/{postId} {
allow read, write: if isUser(userId);
}
match /savedPosts/{postId} {
allow read, write: if isUser(userId);
}
match /blocked/{blockedId} {
allow read, write: if isUser(userId);
}
match /connections/{connectionId} {
allow read, write: if isUser(userId);
}
match /classrooms/{classroomId} {
allow read, write: if isUser(userId);
}
}
/* =========================
π POSTS
========================== */
match /posts/{postId} {
allow read: if true;
allow create: if isSignedIn() && isOwner(request.resource.data.userId);
allow update, delete: if isSignedIn() && isOwner(resource.data.userId);
match /comments/{commentId} {
allow read: if true;
allow create: if isSignedIn() && isOwner(request.resource.data.userId);
allow update, delete: if isSignedIn() && isOwner(resource.data.userId);
}
}
/* =========================
π₯ COMMUNITIES
========================== */
match /communities/{communityId} {
allow read: if true;
allow create: if isSignedIn() && isOwner(request.resource.data.creatorId);
allow update, delete: if isSignedIn() && isOwner(resource.data.creatorId);
match /members/{memberId} {
allow read: if true;
allow create, delete: if isSignedIn() &&
(isUser(memberId) ||
isOwner(get(/databases/$(database)/documents/communities/$(communityId)).data.creatorId));
}
}
/* =========================
π NOTIFICATIONS
========================== */
match /notifications/{notificationId} {
allow read: if isSignedIn() &&
(request.auth.uid == resource.data.recipientId ||
request.auth.uid == resource.data.actor.id);
allow create: if isSignedIn() &&
request.auth.uid == request.resource.data.actor.id;
allow update, delete: if isSignedIn() &&
request.auth.uid == resource.data.recipientId;
}
/* =========================
π€ FRIEND REQUESTS
========================== */
match /friendRequests/{requestId} {
allow read: if isSignedIn() &&
(request.auth.uid == resource.data.fromUserId ||
request.auth.uid == resource.data.toUserId);
allow create: if isSignedIn() &&
request.auth.uid == request.resource.data.fromUserId;
allow update: if isSignedIn() &&
request.auth.uid == resource.data.toUserId;
allow delete: if isSignedIn() &&
(request.auth.uid == resource.data.fromUserId ||
request.auth.uid == resource.data.toUserId);
}
/* =========================
π STUDY QUESTIONS
========================== */
match /studyQuestions/{questionId} {
allow read: if true;
allow create: if isSignedIn() && isOwner(request.resource.data.authorId);
allow update, delete: if isSignedIn() && isOwner(resource.data.authorId);
match /comments/{commentId} {
allow read: if true;
allow create: if isSignedIn() && isOwner(request.resource.data.userId);
allow update, delete: if isSignedIn() && isOwner(resource.data.userId);
}
}
/* =========================
π« CLASSROOMS
========================== */
match /classrooms/{classroomId} {
allow read: if isSignedIn() &&
resource.data.memberIds.hasAny([request.auth.uid]);
allow create: if isSignedIn() &&
isOwner(request.resource.data.creatorId);
allow update, delete: if isSignedIn() &&
isOwner(resource.data.creatorId);
}
/* =========================
π¬ CHATS (FINAL & BULLETPROOF)
========================== */
match /chats/{chatId} {
// Only participants can read chat
allow read: if isSignedIn() &&
request.auth.uid in resource.data.participantIds;
// Create chat if user is listed as participant
allow create: if isSignedIn() &&
request.auth.uid in request.resource.data.participantIds;
// Participants can update metadata
allow update: if isSignedIn() &&
request.auth.uid in resource.data.participantIds;
allow delete: if false;
/* ---- MESSAGES ---- */
match /messages/{messageId} {
// Only signed-in users can read messages of existing chats
allow read: if isSignedIn() &&
request.auth.uid in get(/databases/$(database)/documents/chats/$(chatId)).data.participantIds;
// β
FIRST MESSAGE + ALL MESSAGES SAFE
allow create: if isSignedIn() &&
request.auth.uid == request.resource.data.senderId;
allow update, delete: if false;
}
}
}
}