Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
334 lines (293 loc) · 15.5 KB
/
Copy pathfirestore.rules
File metadata and controls
334 lines (293 loc) · 15.5 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper Functions
function isValidStudent() {
let data = request.resource.data;
return data.keys().hasAll(['full_name', 'course', 'created_at'])
&& data.full_name is string
&& data.course is string;
}
function isAttendanceValid(docId) {
let data = request.resource.data;
return data.keys().hasAll(['event_id', 'student_id', 'scanned_at'])
&& data.event_id is string
&& data.student_id is string
&& docId == (data.event_id + '_' + data.student_id);
}
function isClubRegistrationValid(docId) {
let data = request.resource.data;
return data.keys().hasAll(['club_id', 'student_id', 'registered_at'])
&& data.club_id is string
&& data.student_id is string
&& docId == (data.club_id + '_' + data.student_id);
}
function isSuperAdmin() {
return request.auth != null && (
('role' in request.auth.token && request.auth.token.role == 'super_admin') ||
(exists(/databases/$(database)/documents/users/$(request.auth.uid)) &&
'role' in get(/databases/$(database)/documents/users/$(request.auth.uid)).data &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'super_admin')
);
}
function isAdminOrCoordinator() {
return request.auth != null && (
('role' in request.auth.token && (request.auth.token.role == 'super_admin' || request.auth.token.role == 'coordinator')) ||
(exists(/databases/$(database)/documents/users/$(request.auth.uid)) &&
'role' in get(/databases/$(database)/documents/users/$(request.auth.uid)).data &&
(get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'super_admin' || get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'coordinator'))
);
}
// 1. EVENTS
match /events/{eventId} {
allow read: if true;
allow write: if isAdminOrCoordinator();
}
// 2. STUDENTS
match /students/{enrollmentId} {
allow get: if true;
allow list: if request.auth != null && (request.auth.token.role == 'super_admin' || request.auth.token.role == 'coordinator');
allow create: if isValidStudent();
allow update: if
// 1. Department ID cannot be changed (Lock)
request.resource.data.department_id == resource.data.department_id &&
// 2. Points can only be updated if they increment by 5, 10 or 25
(request.resource.data.points == resource.data.points + 5 ||
request.resource.data.points == resource.data.points + 10 ||
request.resource.data.points == resource.data.points + 25 ||
request.resource.data.points == resource.data.points) &&
// 3. Other core fields cannot be changed
request.resource.data.enrollment_no == resource.data.enrollment_no &&
request.resource.data.full_name == resource.data.full_name;
allow delete: if false;
match /private/contact {
allow get: if request.auth != null && request.auth.uid == get(/databases/$(database)/documents/students/$(enrollmentId)).data.auth_uid;
allow list: if false;
allow create, update: if true;
allow delete: if false;
}
}
// 3. ATTENDANCE
match /attendance/{docId} {
allow get: if true;
allow list: if request.auth != null && (request.auth.token.role == 'super_admin' || request.auth.token.role == 'coordinator');
allow create: if isAttendanceValid(docId);
allow update, delete: if false;
}
// 4. CLUBS
match /clubs/{clubId} {
allow read: if true;
// Admins and coordinators manage clubs (create, update, delete)
// Required for club creation with image URL, tagline, whatsappGroup, capacity fields
allow write: if isAdminOrCoordinator();
}
// 5. CLUB REGISTRATIONS
match /club_registrations/{regId} {
allow get: if true;
allow list: if request.auth != null && (request.auth.token.role == 'super_admin' || request.auth.token.role == 'coordinator');
allow create: if isClubRegistrationValid(regId);
allow update, delete: if false;
}
// 6. CLUB EVENTS & ATTENDANCE
match /club_events/{eventId} {
allow read: if true;
allow write: if false;
}
match /club_attendance/{docId} {
allow get: if true;
allow list: if false;
allow create: if isAttendanceValid(docId);
allow update, delete: if false;
}
// 7. ADMINS
match /admins/{adminId} {
allow read: if true;
allow write: if false;
}
// 8. SECURE DOCUMENTS
match /secure_documents/{docId} {
allow read: if request.auth != null && (request.auth.token.role == 'super_admin' || request.auth.token.role == 'coordinator');
allow create: if request.auth != null && (request.auth.token.role == 'coordinator' || request.auth.token.role == 'super_admin');
allow update: if request.auth != null && (request.auth.token.role == 'coordinator' || request.auth.token.role == 'super_admin');
allow delete: if request.auth != null && request.auth.token.role == 'super_admin';
}
// 9. USERS (RBAC)
match /users/{userId} {
allow read: if request.auth != null && (request.auth.uid == userId || request.auth.token.role == 'super_admin');
allow write: if request.auth != null && request.auth.token.role == 'super_admin';
}
// 10. AUDIT LOGS
match /audit_logs/{logId} {
allow read: if request.auth != null && request.auth.token.role == 'super_admin';
allow create: if request.auth != null;
allow update, delete: if false;
}
// 11. ANNOUNCEMENTS
match /announcements/{announcementId} {
allow read: if resource.data.status == 'active' || isSuperAdmin();
allow write: if isSuperAdmin();
}
// 12. USER NOTIFICATIONS
match /user_notifications/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
match /reads/{announcementId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
}
}
// ── 13. EVENT ATTENDANCE (isolated system — no relation to induction) ──────
//
// Collection: event_attendance
// Document ID: {event_id}_{enrollment_number} (compound — O(1) dedup)
//
// Public write is required because students scan QR without logging in.
// Security is enforced server-side in api/event-attendance-mark.ts:
// - student must exist in students/{enrollment_number}
// - event must be active and within attendance window
// - rate limiting applied (IP + enrollment + event)
// - server writes the document — client NEVER writes directly
//
// Therefore client rules are read-only (admins only).
match /event_attendance/{docId} {
// Admins and coordinators can list records (for the admin panel)
allow read: if isAdminOrCoordinator();
// No client writes — server only (Firebase Admin SDK bypasses rules)
allow write: if false;
}
// 14. EVENT ATTENDANCE LOGS (audit trail — append-only via server)
match /event_attendance_logs/{logId} {
allow read: if isSuperAdmin();
// No client writes — server only
allow write: if false;
}
// 15. EVENT DATASETS (Admin upload & management)
match /event_datasets/{datasetId} {
// Admins and coordinators can read all dataset records
allow read: if isAdminOrCoordinator();
// No direct client writes — all writes go through the Admin SDK (server-side APIs)
allow write: if false;
}
// 16. EVENT PARTICIPANTS (imported from datasets — server only)
match /event_participants/{participantId} {
// Admins and coordinators can read participant records
allow read: if isAdminOrCoordinator();
// No direct client writes — populated by the import API via Admin SDK
allow write: if false;
}
// 17. ROOMS
match /rooms/{roomId} {
allow read: if isAdminOrCoordinator();
allow write: if false;
}
// 18. ROOM ALLOCATIONS
match /room_allocations/{allocId} {
allow read: if isAdminOrCoordinator();
allow write: if false;
}
// ── 19. INDUCTION DATASETS ────────────────────────────────────────────────
// Platform-wide dataset metadata records (no event_id — induction only).
// Created/updated exclusively by the unified upload/import/activate/delete APIs
// via Firebase Admin SDK. Clients (including admins in the browser) can only read.
match /induction_datasets/{datasetId} {
allow read: if isAdminOrCoordinator();
allow write: if false; // Admin SDK only
}
// ── 20. INDUCTION PARTICIPANTS ────────────────────────────────────────────
// Document ID = Application Number (KRMU ID) — O(1) lookup, no composite index.
// Populated by the chunked import API. registration_status: PENDING → REGISTERED.
// Clients CANNOT read the list (PII protection); the server exposes masked data
// via /api/induction-lookup. Admins can read individual documents.
match /induction_participants/{applicationNumber} {
allow get: if isAdminOrCoordinator();
allow list: if isSuperAdmin();
allow write: if false; // Admin SDK only (import + register APIs)
}
// ── 21. INDUCTION PLATFORM CONFIG ────────────────────────────────────────
// Single-document config at induction_platform_config/settings.
// Holds active_dataset_id and import_lock. Server-side only.
match /induction_platform_config/{docId} {
allow read: if isAdminOrCoordinator();
allow write: if false; // Admin SDK only
}
// ── 22. REGISTRATION SESSIONS ─────────────────────────────────────────────
// Short-lived tokens used to authorise the induction fast-track registration.
// Created by /api/verify-otp; consumed (deleted) by /api/induction-register.
// Never readable or writable by clients — 100% server-side.
match /registration_sessions/{sessionId} {
allow read, write: if false; // Admin SDK only
}
// ── 23. SECURITY EVENTS ───────────────────────────────────────────────────
// Rate limit and auth attempt logs for abuse detection
match /security_events/{eventId} {
allow read: if isAdminOrCoordinator();
allow write: if false; // Admin SDK only
}
// ── 24. CONFLICT LOGS ─────────────────────────────────────────────────────
// Dataset import collisions and overwrites
match /conflict_logs/{logId} {
allow read: if isAdminOrCoordinator();
allow write: if false; // Admin SDK only
}
// ── 25. MEDIA ASSETS ──────────────────────────────────────────────────────
match /media_assets/{assetId} {
// Public read so homepage can query media
allow read: if true;
// Admins and coordinators can upload and manage metadata
allow write: if isAdminOrCoordinator();
}
// ══════════════════════════════════════════════════════════════════════════
// AARAMBH 2026 — INDUCTION PLANNER COLLECTIONS
// ALL six collections are server-write only (Firebase Admin SDK).
// Client NEVER reads planner Firestore directly — data is served via
// authenticated API endpoints (/api/planner-student-dashboard, etc.)
// ══════════════════════════════════════════════════════════════════════════
// ── 26. INDUCTION PLANNERS (metadata + lifecycle state) ───────────────────
match /induction_planners/{plannerId} {
allow read: if false; // API only — /api/planner-admin-status
allow write: if false; // Admin SDK only
}
// ── 27. INDUCTION ROOM ALLOCATIONS ────────────────────────────────────────
match /induction_room_allocations/{docId} {
allow read: if false; // API only — /api/planner-student-dashboard
allow write: if false; // Admin SDK only (planner-engine.ts)
}
// ── 28. INDUCTION SESSIONS ─────────────────────────────────────────────────
match /induction_sessions/{docId} {
allow read: if false; // API only — /api/planner-student-dashboard, /api/planner-day-schedule
allow write: if false; // Admin SDK only
}
// ── 29. INDUCTION VENUES ───────────────────────────────────────────────────
match /induction_venues/{docId} {
allow read: if false; // API only
allow write: if false; // Admin SDK only
}
// ── 30. INDUCTION FACULTY COORDINATORS ────────────────────────────────────
match /induction_faculty_coordinators/{docId} {
allow read: if false; // API only
allow write: if false; // Admin SDK only
}
// ── 31. PLANNER AUDIT LOGS ────────────────────────────────────────────────
match /planner_audit_logs/{logId} {
allow read: if false; // API only — /api/planner-admin-status
allow write: if false; // Admin SDK only (planner-engine.ts)
}
// ── 32. LANDING EXPERIENCE (CMS) ──────────────────────────────────────────
match /landing_settings/{settingId} {
allow read: if true;
allow write: if isAdminOrCoordinator() && request.auth.token.landing_super_admin == true;
}
match /landing_collections/{collectionId} {
allow read: if true;
allow write: if isAdminOrCoordinator() && request.auth.token.landing_super_admin == true;
}
match /landing_slides/{slideId} {
allow read: if true;
allow write: if isAdminOrCoordinator() && request.auth.token.landing_super_admin == true;
}
// ── 33. EMAIL INDEX ───────────────────────────────────────────────────────
// Allows dashboard fallback to securely resolve enrollment numbers for returning users
match /email_index/{emailId} {
allow get: if request.auth != null && request.auth.uid == 'email:' + emailId;
allow list, write: if false;
}
}
}