|
| 1 | +rules_version = '2'; |
| 2 | + |
| 3 | +service cloud.firestore { |
| 4 | + match /databases/{database}/documents { |
| 5 | + |
| 6 | + // Helper function to check if user is authenticated |
| 7 | + function isAuthenticated() { |
| 8 | + return request.auth != null; |
| 9 | + } |
| 10 | + |
| 11 | + // Helper function to get GitHub handle from auth |
| 12 | + // Assumes GitHub auth is being used with Coder |
| 13 | + function getGitHubHandle() { |
| 14 | + return request.auth.token.github_handle; |
| 15 | + } |
| 16 | + |
| 17 | + // Helper function to get the user's team name from their participant document |
| 18 | + function getUserTeam() { |
| 19 | + let participantDoc = get(/databases/$(database)/documents/participants/$(getGitHubHandle())); |
| 20 | + return participantDoc.data.team_name; |
| 21 | + } |
| 22 | + |
| 23 | + // Helper function to check if user belongs to a specific team |
| 24 | + function isUserTeam(teamName) { |
| 25 | + return isAuthenticated() && getUserTeam() == teamName; |
| 26 | + } |
| 27 | + |
| 28 | + // Global keys collection - read-only for all authenticated users |
| 29 | + match /global_keys/{document=**} { |
| 30 | + allow read: if isAuthenticated(); |
| 31 | + allow write: if false; // Only admins via backend |
| 32 | + } |
| 33 | + |
| 34 | + // Teams collection - users can only read their own team's data |
| 35 | + match /teams/{teamId} { |
| 36 | + // Users can only read their own team (which contains API keys) |
| 37 | + allow read: if isUserTeam(teamId); |
| 38 | + allow write: if false; // Only admins via backend |
| 39 | + } |
| 40 | + |
| 41 | + // Participants collection |
| 42 | + match /participants/{participantId} { |
| 43 | + // Users can read their own participant document |
| 44 | + allow read: if isAuthenticated() && |
| 45 | + participantId == getGitHubHandle(); |
| 46 | + |
| 47 | + // Users can update their own onboarded status and onboarded_at timestamp |
| 48 | + allow update: if isAuthenticated() && |
| 49 | + participantId == getGitHubHandle() && |
| 50 | + request.resource.data.diff(resource.data).affectedKeys() |
| 51 | + .hasOnly(['onboarded', 'onboarded_at']); |
| 52 | + |
| 53 | + // No create or delete for participants (admin only) |
| 54 | + allow create, delete: if false; |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments