-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirestore.rules
More file actions
100 lines (83 loc) · 3.66 KB
/
firestore.rules
File metadata and controls
100 lines (83 loc) · 3.66 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// --- Helper Functions ---
function isAuthenticated() {
return request.auth != null;
}
function getUserData() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
}
function isRole(role) {
return isAuthenticated() &&
exists(/databases/$(database)/documents/users/$(request.auth.uid)) &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == role;
}
function isAdmin() {
return isRole('admin');
}
function isMaintainer() {
return isRole('maintainer');
}
// --- Users Collection ---
match /users/{userId} {
// Public read access for leaderboard visibility.
// Admins can read/write all users; users can write their own data.
allow read: if true;
allow write: if isAuthenticated() && (request.auth.uid == userId || isAdmin());
}
// --- Repositories Collection ---
match /repositories/{repoId} {
// Public read access
allow read: if true;
// Only admins can create/update/delete repositories
// Allow maintainers to update specific fields (like maintainer metadata if needed) not implemented yet
allow write: if isAdmin();
}
// --- Pull Requests Collection ---
match /pull_requests/{prId} {
// Public read access (transparency)
allow read: if true;
// Syncing creates/updates PRs.
// Ideally, any authenticated user should be able to trigger a sync (create/update basic info).
// But only maintainers/admins should be able to grade (update status/points/feedback).
allow create: if isAuthenticated();
allow update: if isAuthenticated() && (
// Allow anyone (e.g. the sync process) to update basic fields?
// For security, strict grading updates should be maintainer only.
// Let's allow maintainers/admins to update everything.
isAdmin() || isMaintainer() ||
// Allow regular users to update only if they are syncing (checking incoming data fields not status/points)
// This is complex to enforce perfectly in rules without checking every field.
// For MVP, we'll allow authenticated users to update, but rely on UI and trusted maintainers.
// A stricter rule would be:
(request.resource.data.diff(resource.data).affectedKeys()
.hasOnly(['title', 'html_url', 'updatedAt', 'user', 'status', 'points', 'feedback', 'criteriaScores']))
// Wait, 'status', 'points' should be protected.
);
// Let's simplify for now: Allow any auth user to write (sync), but trust the app logic.
// Ideally:
// allow update: if isAdmin() || isMaintainer() || (isAuthenticated() && !request.resource.data.diff(resource.data).affectedKeys().hasAny(['points', 'criteriaScores']));
allow write: if isAuthenticated();
}
// --- Maintainer Whitelist Collection ---
match /maintainer_whitelist/{docId} {
// Admins can manage whitelist
allow read, write: if isAdmin();
// Also allow users to read to check their status during login
allow read: if isAuthenticated();
}
// --- Legacy Forms/Submissions (Keep existing logic) ---
match /forms/{formSlug} {
allow read: if true;
allow create: if isAdmin();
allow update: if isAdmin();
allow delete: if isAdmin();
}
match /submissions/{submissionId} {
allow create: if isAuthenticated() &&
request.resource.data.userId == request.auth.uid;
allow read, delete: if isAdmin();
}
}
}