-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfirestore.rules
More file actions
86 lines (72 loc) · 2.62 KB
/
Copy pathfirestore.rules
File metadata and controls
86 lines (72 loc) · 2.62 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper Functions
function isAuthenticated() {
return request.auth != null;
}
function isAdmin() {
return isAuthenticated() &&
exists(/databases/$(database)/documents/users/$(request.auth.uid)) &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
function isOwner(uid) {
return isAuthenticated() && request.auth.uid == uid;
}
// USERS / AMBASSADORS COLLECTION
match /users/{userId} {
// Public access to read profiles for leaderboard and community listings
allow read: if true;
// Users can create their own profile, but cannot assign themselves 'admin' or set verified counts
allow create: if isOwner(userId)
&& request.resource.data.role != 'admin'
&& request.resource.data.verifiedRegistrations == 0;
// Users can update basic profile info, admins can update anything
allow update: if isAdmin() || (
isOwner(userId)
&& !request.resource.data.diff(resource.data).affectedKeys()
.hasAny(['role', 'ambassadorId', 'verifiedRegistrations'])
);
allow delete: if isAdmin();
}
// REFERRALS / CLICKS COLLECTION
match /clicks/{clickId} {
// Anyone can create a click (public referral tracking)
allow create: if true;
// Authenticated users and admins can read click logs
allow read: if isAuthenticated();
allow update, delete: if isAdmin();
}
// REGISTRATIONS (Actual hackathon registrations)
match /referrals/{refId} {
allow create: if true;
// Ambassadors can read their own referrals
allow read: if isAdmin() || (
isAuthenticated() &&
resource.data.ambassadorId == get(/databases/$(database)/documents/users/$(request.auth.uid)).data.ambassadorId
);
allow update, delete: if isAdmin();
}
// CAMPAIGNS COLLECTION
match /campaigns/{campaignId} {
allow read: if true;
allow write: if isAdmin();
}
// ANNOUNCEMENTS COLLECTION
match /announcements/{announcementId} {
allow read: if true;
allow write: if isAdmin();
}
// COUNTERS (For sequential ID generation)
match /counters/{counterId} {
allow read: if true;
// Only admins can increment the ambassador ID counter
allow write: if isAdmin();
}
// LEADERBOARD (Publicly accessible)
match /leaderboard/{docId} {
allow read: if true;
allow write: if isAdmin();
}
}
}