Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
203 lines (176 loc) · 7.51 KB
/
Copy pathfirestore.rules
File metadata and controls
203 lines (176 loc) · 7.51 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// --- HELPERS ---
function isAuthenticated() {
return request.auth != null;
}
function getUserData() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
}
function hasRole(role) {
return isAuthenticated() && getUserData().role == role;
}
function hasAnyRole(roles) {
return isAuthenticated() && getUserData().role in roles;
}
function isOwner(uid) {
return isAuthenticated() && request.auth.uid == uid;
}
function isSameRegion(resourceData) {
return resourceData.region == getUserData().region;
}
function assignmentDocId(taskId, volunteerId) {
return taskId + '_' + volunteerId;
}
function hasAcceptedAssignment(taskId) {
return isAuthenticated() &&
exists(/databases/$(database)/documents/taskAssignments/$(assignmentDocId(taskId, request.auth.uid))) &&
get(/databases/$(database)/documents/taskAssignments/$(assignmentDocId(taskId, request.auth.uid))).data.status == 'accepted';
}
// --- COLLECTIONS ---
// Users Collection (Auth & Profiles)
match /users/{uid} {
allow read: if isAuthenticated() && (
isOwner(uid) ||
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) ||
(hasRole('field_lead') && isSameRegion(resource.data))
);
allow create: if isAuthenticated() && isOwner(uid);
allow update: if isAuthenticated() && (
isOwner(uid) ||
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin'])
);
allow delete: if hasRole('super_admin');
}
// Volunteers Collection (Verified Profiles)
match /volunteers/{volId} {
allow read: if isAuthenticated() && (
hasAnyRole(['volunteer', 'field_lead', 'ngo_founder', 'ngo_admin', 'super_admin'])
);
allow create: if hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']);
allow update: if isAuthenticated() && (
isOwner(volId) ||
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin'])
);
allow delete: if hasRole('super_admin');
}
// Needs Collection (Crisis Reports)
match /needs/{needId} {
// All authenticated users can read needs (applicants see the live map during onboarding)
allow read: if isAuthenticated();
allow create: if isAuthenticated() && (
hasAnyRole(['volunteer', 'field_lead', 'ngo_founder', 'ngo_admin', 'super_admin'])
);
allow update: if isAuthenticated() && (
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) ||
(hasRole('field_lead') && isSameRegion(resource.data))
);
allow delete: if hasRole('super_admin');
}
// Tasks Collection (Assigned Operations)
match /tasks/{taskId} {
allow read: if isAuthenticated() && (
hasAnyRole(['volunteer', 'field_lead', 'ngo_founder', 'ngo_admin', 'super_admin']) ||
(request.auth.uid in resource.data.volunteerIds)
);
allow create: if hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin', 'field_lead']);
allow update: if isAuthenticated() && (
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) ||
(hasRole('field_lead') && isSameRegion(resource.data)) ||
(request.auth.uid in resource.data.volunteerIds) ||
hasAcceptedAssignment(taskId)
);
allow delete: if hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']);
}
// Task Assignments (Volunteer requests)
match /taskAssignments/{assignmentId} {
allow read: if isAuthenticated() && (
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) ||
(hasRole('field_lead') && isSameRegion(resource.data)) ||
(resource.data.volunteerId == request.auth.uid)
);
allow create: if isAuthenticated() && (
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) ||
(hasRole('field_lead') && request.resource.data.region == getUserData().region)
)
&& request.resource.data.requestedBy == request.auth.uid
&& request.resource.data.status == 'pending';
allow update: if isAuthenticated() && (
// volunteer response
(
resource.data.volunteerId == request.auth.uid &&
resource.data.status == 'pending' &&
request.resource.data.status in ['accepted', 'declined']
)
||
// coordinator/admin cancel
(
(hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) || (hasRole('field_lead') && isSameRegion(resource.data))) &&
resource.data.status == 'pending' &&
request.resource.data.status == 'cancelled'
)
);
allow delete: if false;
}
// Task Contacts (contact unlocked only after acceptance)
match /taskContacts/{taskId} {
allow read: if isAuthenticated() && (
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) ||
(hasRole('field_lead') && isSameRegion(resource.data)) ||
hasAcceptedAssignment(taskId)
);
allow create, update: if isAuthenticated() && (
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) ||
(hasRole('field_lead') && request.resource.data.region == getUserData().region)
) && request.resource.data.createdBy == request.auth.uid;
allow delete: if hasRole('super_admin');
}
// Activities Collection (Audit Log)
match /activities/{activityId} {
// All authenticated users can read activities (feeds home & insights screens)
allow read: if isAuthenticated();
allow create: if isAuthenticated();
allow update, delete: if false; // Immutable audit log
}
// Inventory Collection (Resources)
match /inventory/{itemId} {
allow read: if isAuthenticated() && hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin', 'field_lead', 'volunteer']);
allow create, update: if hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']);
allow delete: if hasRole('super_admin');
}
// Inventory Transactions (Movement Ledger)
match /inventory_transactions/{txId} {
allow read: if isAuthenticated() && hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin', 'field_lead']);
allow create: if isAuthenticated() && hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin', 'field_lead']);
allow update, delete: if false; // Immutable transaction log
}
// NGO Registry (Organization Profiles)
match /ngos/{ngoId} {
allow read: if isAuthenticated();
allow create: if isAuthenticated() && (
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) &&
request.resource.data.founderId == request.auth.uid
);
allow update: if isAuthenticated() && (
hasRole('super_admin') ||
(hasAnyRole(['ngo_founder', 'ngo_admin']) && request.auth.uid in resource.data.memberIds)
);
allow delete: if hasRole('super_admin');
}
// NGO Memberships (Org-User Relationships)
match /ngo_memberships/{membershipId} {
allow read: if isAuthenticated() && (
hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']) ||
resource.data.userId == request.auth.uid
);
allow create: if hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']);
allow update: if hasAnyRole(['ngo_founder', 'ngo_admin', 'super_admin']);
allow delete: if hasRole('super_admin');
}
// Notification Preferences (User-Private Settings)
match /notification_preferences/{uid} {
allow read, write: if isAuthenticated() && isOwner(uid);
}
}
}