-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirestore.rules
83 lines (74 loc) · 2.69 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper functions
function isAuthenticated() {
return request.auth != null;
}
function isParent() {
return isAuthenticated() &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'parent';
}
function isChild() {
return isAuthenticated() &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'child';
}
function isParentOf(childId) {
return isParent() &&
get(/databases/$(database)/documents/users/$(childId)).data.parentId == request.auth.uid;
}
// User profiles
match /users/{userId} {
allow read: if isAuthenticated() && (
request.auth.uid == userId ||
(isParent() && resource.data.parentId == request.auth.uid) ||
(isChild() && resource.data.parentId == userId)
);
allow create: if isAuthenticated();
allow update: if isAuthenticated() && (
request.auth.uid == userId ||
(isParent() && resource.data.parentId == request.auth.uid)
);
allow delete: if isParent() && resource.data.parentId == request.auth.uid;
}
// Tasks
match /tasks/{taskId} {
allow read: if isAuthenticated() &&
(isParent() ||
isChild() && resource.data.assignedTo.hasAny([request.auth.uid]));
allow create: if isParent();
allow update: if isParent() ||
(isChild() && resource.data.assignedTo.hasAny([request.auth.uid]));
allow delete: if isParent();
}
// Rewards
match /rewards/{rewardId} {
allow read: if isAuthenticated();
allow create, update, delete: if isParent();
}
// Reward Claims
match /rewardClaims/{claimId} {
allow read: if isAuthenticated() && (
isParent() && resource.data.parentId == request.auth.uid ||
isChild() && resource.data.childId == request.auth.uid
);
allow create: if isAuthenticated();
allow update: if isParent() && resource.data.parentId == request.auth.uid;
}
// Activity
match /activity/{activityId} {
allow read: if isAuthenticated() && (
isParent() ||
isChild() && resource.data.childId == request.auth.uid
);
allow create: if isAuthenticated();
}
// Notifications
match /notifications/{notificationId} {
allow read: if isAuthenticated() && resource.data.userId == request.auth.uid;
allow create: if isAuthenticated();
allow update: if isAuthenticated() && resource.data.userId == request.auth.uid;
allow delete: if isAuthenticated() && resource.data.userId == request.auth.uid;
}
}
}