-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathfirestore.rules
More file actions
47 lines (40 loc) · 1.65 KB
/
Copy pathfirestore.rules
File metadata and controls
47 lines (40 loc) · 1.65 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Reusable functions for role-based access control
function isAuthenticated() {
return request.auth != null;
}
function isTeacher() {
return isAuthenticated() && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'teacher';
}
function isStudent() {
return isAuthenticated() && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'student';
}
// Users Collection
// Users can read their own profile; teachers can read all profiles for class management
// Users can only edit their own profile
match /users/{userId} {
allow read: if isAuthenticated() && (request.auth.uid == userId || isTeacher());
allow write: if isAuthenticated() && request.auth.uid == userId;
}
// Courses Collection
// Students can read courses, but only teachers can create/update/delete them
match /courses/{courseId} {
allow read: if isAuthenticated();
allow write: if isTeacher();
}
// Enrollments Collection
// Students can read their own enrollments. They can only create an enrollment for themselves.
// Teachers can read all enrollments for their courses.
match /enrollments/{enrollmentId} {
allow read: if isAuthenticated() && (request.auth.uid == resource.data.studentId || isTeacher());
allow create: if isStudent() && request.resource.data.studentId == request.auth.uid;
allow update, delete: if isTeacher();
}
// Default deny
match /{document=**} {
allow read, write: if false;
}
}
}