-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
124 lines (100 loc) · 5.52 KB
/
Copy pathfirestore.rules
File metadata and controls
124 lines (100 loc) · 5.52 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper functions
function isSignedIn() {
return request.auth != null;
}
function isOwner(userId) {
return isSignedIn() && request.auth.uid == userId;
}
function isAdmin() {
return isSignedIn() && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
function isFaculty() {
return isSignedIn() && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'faculty';
}
// Rules for the 'users' collection
match /users/{userId} {
// Users can read their own profile; admins can read and list all profiles.
allow read: if isOwner(userId) || isAdmin();
// Users can create their own student profile on signup; admins can create managed profiles.
allow create: if isAdmin() || (isOwner(userId) && request.resource.data.role == 'student');
// Users can update their own profile, but cannot promote themselves by changing role.
allow update: if isAdmin() || (isOwner(userId) && request.resource.data.role == resource.data.role);
// Only admins should be able to delete user documents
allow delete: if isAdmin();
}
// Rules for immutable server-written admin audit logs.
match /auditLogs/{logId} {
allow read: if isAdmin();
allow create, update, delete: if false;
}
// Rules for 'systemSettings' collection
match /systemSettings/appConfiguration {
// Allow anyone to read system settings (e.g., for maintenance mode checks)
allow read: if true;
// Only admins can write system settings
allow write: if isAdmin();
}
// Rules for 'classrooms' collection
match /classrooms/{classroomId} {
// Read:
// - Admins can read any classroom.
// - Faculty can read classrooms they own or are invited to.
// - Students can read classrooms they are enrolled in (checked via studentUids array).
allow read: if isAdmin() || (isFaculty() && (resource.data.ownerFacultyId == request.auth.uid || request.auth.uid in resource.data.invitedFacultyIds)) || (request.auth.uid in resource.data.studentUids);
// Create: Only authenticated faculty can create classrooms.
allow create: if isFaculty();
// Update:
// - Only faculty who own the classroom or are invited can update it.
allow update: if isFaculty() && (resource.data.ownerFacultyId == request.auth.uid || request.auth.uid in resource.data.invitedFacultyIds);
// Delete: Only the faculty owner can delete the classroom.
allow delete: if isFaculty() && resource.data.ownerFacultyId == request.auth.uid;
// Rules for 'messages' subcollection within a classroom
match /messages/{messageId} {
// Read: Same logic as reading the classroom itself.
allow read: if isAdmin() || (isFaculty() && (get(/databases/$(database)/documents/classrooms/$(classroomId)).data.ownerFacultyId == request.auth.uid || request.auth.uid in get(/databases/$(database)/documents/classrooms/$(classroomId)).data.invitedFacultyIds)) || (request.auth.uid in get(/databases/$(database)/documents/classrooms/$(classroomId)).data.studentUids);
// Create: Any signed-in user who is part of the classroom (student, faculty, or admin) can send a message.
allow create: if isSignedIn() && (request.auth.uid in get(/databases/$(database)/documents/classrooms/$(classroomId)).data.studentUids || get(/databases/$(database)/documents/classrooms/$(classroomId)).data.ownerFacultyId == request.auth.uid || request.auth.uid in get(/databases/$(database)/documents/classrooms/$(classroomId)).data.invitedFacultyIds || isAdmin());
// Update, Delete: Generally, chat messages are not updated or deleted by users.
allow update, delete: if false;
}
}
// Rules for 'profileChangeRequests' collection
match /profileChangeRequests/{requestId} {
// Create: A user can create a request for themselves.
allow create: if isOwner(request.resource.data.userId);
// Read:
// - Admins can read all requests.
// - Users can read their own requests.
allow read: if isAdmin() || isOwner(resource.data.userId);
// Update, Delete: Only admins can update (approve/deny) or delete requests.
allow update, delete: if isAdmin();
}
// Rules for 'grades' collection
match /grades/{gradeId} {
// Read:
// - Admins can read all grades.
// - Faculty can read all grades.
// - A user can read their own grades.
allow read: if isAdmin() || isFaculty() || isOwner(resource.data.studentId);
// Create, Update, Delete: Only faculty and admins can manage grades.
allow create, update, delete: if isFaculty() || isAdmin();
}
// Rules for 'lectureAttendance' collection
match /lectureAttendance/{attendanceId} {
// Read:
// - Admins can read all records.
// - Faculty can read all records.
// - A user can read their own attendance records.
allow read: if isAdmin() || isFaculty() || isOwner(resource.data.studentId);
// Create, Update, Delete: Only faculty and admins can submit or change attendance.
allow create, update, delete: if isFaculty() || isAdmin();
}
// Fallback rule: Deny all access to any other collection by default
match /{document=**} {
allow read, write: if false;
}
}
}