-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirestore.rules
78 lines (67 loc) · 2.22 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper functions
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function isAdmin() {
return isAuthenticated() &&
exists(/databases/$(database)/documents/users/$(request.auth.uid)) &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
function isValidUser() {
return request.resource.data.keys().hasAll([
'email', 'displayName', 'role', 'createdAt', 'updatedAt'
]);
}
// Users collection
match /users/{userId} {
allow read: if isAuthenticated();
allow create: if isAuthenticated() && isValidUser();
allow update: if isOwner(userId) || isAdmin();
allow delete: if isAdmin();
// User's private data subcollection
match /private/{document=**} {
allow read, write: if isOwner(userId);
}
}
// Agencies collection
match /agencies/{agencyId} {
allow read: if isAuthenticated();
allow create: if isAuthenticated() &&
request.resource.data.createdBy == request.auth.uid;
allow update: if isAuthenticated() && (
resource.data.createdBy == request.auth.uid || isAdmin()
);
allow delete: if isAdmin();
// Agency's policies subcollection
match /policies/{policyId} {
allow read: if isAuthenticated();
allow write: if isAuthenticated() && (
resource.data.createdBy == request.auth.uid || isAdmin()
);
}
// Agency's scenarios subcollection
match /scenarios/{scenarioId} {
allow read: if isAuthenticated();
allow write: if isAuthenticated() && (
resource.data.createdBy == request.auth.uid || isAdmin()
);
}
}
// Analytics collection
match /analytics/{document=**} {
allow read: if isAuthenticated();
allow write: if isAdmin();
}
// Public data collection
match /public/{document=**} {
allow read: if true;
allow write: if isAdmin();
}
}
}