-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirestore.rules
More file actions
46 lines (40 loc) · 1.69 KB
/
Copy pathfirestore.rules
File metadata and controls
46 lines (40 loc) · 1.69 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function isStudent() {
return request.auth != null && request.auth.token.role == 'student';
}
// Firebase omits null claims from custom tokens — key may be absent, not explicitly null.
function isGlobalSupport() {
return request.auth != null
&& request.auth.token.role == 'support_agent'
&& (!('partnerPrepId' in request.auth.token) || request.auth.token.partnerPrepId == null);
}
function isPartnerSupport(conv) {
return request.auth != null
&& request.auth.token.role == 'support_agent'
&& 'partnerPrepId' in request.auth.token
&& request.auth.token.partnerPrepId != null
&& request.auth.token.partnerPrepId == conv.partnerPrepId;
}
function ownsConversation(conv) {
return isStudent() && conv.userId == request.auth.token.userId;
}
match /conversations/{conversationId} {
allow read: if isGlobalSupport()
|| isPartnerSupport(resource.data)
|| ownsConversation(resource.data);
// All writes go through NestJS Admin SDK, which bypasses Security Rules.
allow write: if false;
}
match /messages/{messageId} {
allow read: if isGlobalSupport()
|| isPartnerSupport(
get(/databases/$(database)/documents/conversations/$(resource.data.conversationId)).data
)
|| (isStudent() && resource.data.conversationUserId == request.auth.token.userId);
// All writes go through NestJS Admin SDK, which bypasses Security Rules.
allow write: if false;
}
}
}