-
Notifications
You must be signed in to change notification settings - Fork 5
/
firestore.rules
91 lines (67 loc) · 3.13 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function isAuthorized() {
return request.auth != null ;
}
function isCurrentUser(userId) {
return request.auth != null && request.auth.uid == userId;
}
// For Users Collection
match /users/{userId} {
allow read: if isAuthorized();
allow create: if isCurrentUser(userId);
allow update: if isCurrentUser(userId);
allow delete: if isCurrentUser(userId);
}
// For Share Codes Collection
match /shared_codes/{sharedCodeId} {
allow read: if isAuthorized();
allow create: if isUserPartOfGroup(request.resource.data.group_id);
allow delete: if isUserPartOfGroup(resource.data.group_id);
}
// For Groups Collection
match /groups/{groupId} {
allow read: if isAuthorized() &&
resource.data.members.hasAny([request.auth.uid]);
allow create: if isAuthorized() &&
isCurrentUser(request.resource.data.created_by);
allow update: if isAuthorized() &&
(resource.data.members.hasAny([request.auth.uid]) ||
(!resource.data.members.hasAny([request.auth.uid]) &&
request.resource.data.diff(resource.data).affectedKeys().hasOnly(["members"])
)
);
allow delete: if isAuthorized() &&
resource.data.members.hasAny([request.auth.uid]);
}
// For Expenses Collection
match /groups/{groupId}/expenses/{expenseId} {
allow read: if isUserPartOfGroup(groupId);
allow create: if isCurrentUser(request.resource.data.added_by) &&
isUserPartOfGroup(groupId);
allow update: if isUserPartOfGroup(groupId);
allow delete: if isUserPartOfGroup(groupId);
}
// For Transactions Collection
match /groups/{groupId}/transactions/{transactionId} {
allow read: if isUserPartOfGroup(groupId);
allow create: if isUserPartOfGroup(groupId) &&
isMemberPartOfGroup(groupId, request.resource.data.payer_id) &&
isMemberPartOfGroup(groupId, request.resource.data.receiver_id);
allow update: if isUserPartOfGroup(groupId) &&
isMemberPartOfGroup(groupId, request.resource.data.payer_id) &&
isMemberPartOfGroup(groupId, request.resource.data.receiver_id);
allow delete: if isUserPartOfGroup(groupId);
}
// Use for sub-collection as it'll not have parent's data
function isUserPartOfGroup(groupId) {
let memberIds = get(/databases/$(database)/documents/groups/$(groupId)).data.members;
return isAuthorized() && memberIds.hasAny([request.auth.uid]);
}
function isMemberPartOfGroup(groupId, memberId) {
let memberIds = get(/databases/$(database)/documents/groups/$(groupId)).data.members;
return isAuthorized() && memberIds.hasAny([memberId]);
}
}
}