📜 Firestore Security Rules Documentation

File: rules/firestore.rules


📖 Table of Contents

  1. Overview
  2. Core Validation Functions
  3. Global Rules (Wildcard Match)
  4. Events Collection
    • 4.1. Comments Subcollection
    • 4.2. Promotions Subcollection
  5. Users Collection
  6. Tags & Brands Collections
  7. Places Collection
  8. Rule Evaluation Flow

🔍 1. Overview

These Firestore Security Rules protect the PartyHunt Goa application data by:

  • Ensuring only authenticated users may read or write.
  • Validating document schema and types on creation.
  • Isolating permissions per-collection and per-operation.
  • Enforcing minimal field requirements and type checks via helper functions.

The rules are written in Rules Version 2, offering expressive type-checking and custom functions .


🛠️ 2. Core Validation Functions

A set of reusable functions verify incoming document shapes:

Function Purpose
isSignedIn() Checks that request.auth is not null (user is authenticated).
isEvent(event) Validates an event document has ≥17 fields with correct types and required values.
isPlace(place) Validates a place document has ≥14 fields and correct types.
isUser(user) Validates a user document has ≥14 fields and correct types.
isComment(comment) Validates a comment document has ≥4 fields and correct types.
function isSignedIn() {
  return request.auth != null;
}

function isEvent(event) {
  return isSignedIn()
      && event.size() >= 17
      && event.docId is string
      && event.picture is string
      // … additional field & type checks …
      && event.commentsCount is int;
}

function isPlace(place) { /* similar checks for place */ }
function isUser(user)   { /* similar checks for user */ }
function isComment(c)   { /* checks comment.comment, .timestamp, .userId, … */ }

All validation functions return a boolean, gating document create (and in some cases update) operations .


🌐 3. Global Rules (Wildcard Match)

match /{document=**} {
  allow create, delete, read: if isSignedIn();
}
  • Scope: All documents across any path.
  • Permissions:
    • create, delete, read allowed for any authenticated user.
  • Note:
    • update is not globally permitted (must be scoped).
    • A future admin-only delete rule is commented out.

🎉 4. Events Collection

match /events/{eventId} {
  allow read, update : if isSignedIn();
  allow create       : if isEvent(request.resource.data);
  // … nested subcollections …
}
  • Read / Update: Any signed-in user may read or update existing events.
  • Create: Only if the event payload passes isEvent.

4.1 💬 Comments Subcollection

match /comments_collection/{commentId} {
  allow read, create, update: if isComment(request.resource.data);
}
  • Ensures comments conform to the comment schema.

4.2 📣 Promotions Subcollection

match /promotions_collection/{promotionId} {
  allow read, create, update: if isSignedIn();
}
  • Any authenticated user may manage promotions.
  • A stricter isPromotion is hinted but not implemented.

👤 5. Users Collection

match /users/{userId} {
  allow read, update;
  allow create: if isUser(request.resource.data);
}
  • Create: Validated by isUser.
  • Read & Update: Blanket access for all users.
  • A more restrictive approach (e.g., matching request.auth.uid) is commented out.

🏷️ 6. Tags & Brands Collections

match /tags/{tagId} {
  allow read, write;
}
match /brands/{brandId} {
  allow read, write;
}
  • Open access for both read & write on tags and brands.
  • Validation functions (isTag, isBrand) are declared but commented out.

📍 7. Places Collection

match /places/{placeId} {
  allow read, create, update: if isPlace(request.resource.data);
}
  • Reads are open to all signed-in users.
  • Create/Update: Enforced by isPlace schema checks.

🔄 8. Rule Evaluation Flow

flowchart TD
  A[Client Request] --> B{Authenticated?}
  B -- No --> Z[Denied]
  B -- Yes --> C[Global & Scoped Matches]
  C -->|Root `{document=**}`| D[Allow Create/Delete/Read]
  C -->|`/events/{eventId}`| E[Check isEvent on Create]
  C -->|`/users/{userId}`| F[Check isUser on Create]
  C -->|`/places/{placeId}`| G[Check isPlace on Write]
  D & E & F & G --> H[Allowed or Denied]
  1. Authentication: All paths start by checking isSignedIn().
  2. Wildcard Match: Grants broad create/delete/read.
  3. Collection-Specific: Overrides or supplements global rules with schema validation.

🎉 Summary:
These Security Rules enforce a robust access-control and data-validation layer for your Firestore backend. By centralizing schema checks in reusable functions and scoping permissions per-collection, the rules help ensure data integrity and secure multi-user access for PartyHunt Goa.