summaryrefslogtreecommitdiffstats
path: root/firestore.rules
blob: daddd3760ac59aa5431e10cbe5bc6edeafc6f83b (plain)
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
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    function isSignedIn() {
      return request.auth != null;
    }

    function isOwner(userId) {
      return isSignedIn() && request.auth.uid == userId;
    }

    function reservationCreateIsValid() {
      return request.resource.data.userId == request.auth.uid
        && request.resource.data.userEmail is string
        && request.resource.data.officeId is string
        && request.resource.data.officeName is string
        && request.resource.data.date is string
        && request.resource.data.startTime is string
        && request.resource.data.endTime is string
        && request.resource.data.status == 'active';
    }

    function reservationUpdateIsValid() {
      // validar que no cambien los campos inmutables
      return request.resource.data.userId == resource.data.userId
        && request.resource.data.officeId == resource.data.officeId
        && request.resource.data.officeName == resource.data.officeName
        && request.resource.data.userEmail == resource.data.userEmail
        && request.resource.data.status in ['active', 'completed', 'cancelled']
        // permitir cambios en date, startTime, endTime
        && request.resource.data.date is string
        && request.resource.data.startTime is string
        && request.resource.data.endTime is string
        && (
          !('checkInAt' in resource.data)
          || request.resource.data.checkInAt == resource.data.checkInAt
          || request.resource.data.checkInAt is timestamp
        );
    }

    match /users/{userId} {
      allow create: if isOwner(userId);
      allow read: if isOwner(userId);
      allow update: if isOwner(userId);
      allow delete: if false;
    }

    match /users/{userId}/favorites/{officeId} {
      allow create: if isOwner(userId)
        && request.resource.data.officeId == officeId;
      allow read: if isOwner(userId);
      allow delete: if isOwner(userId);
      allow update: if false;
    }

    match /offices/{officeId} {
      allow read: if isSignedIn();
      allow write: if false;
    }

    match /reservations/{reservationId} {
      allow create: if isSignedIn() && reservationCreateIsValid();
      allow read: if isSignedIn();
      allow update: if isOwner(resource.data.userId) && reservationUpdateIsValid();
      allow delete: if false;
    }

    match /reservationLocks/{lockId} {
      allow read: if isSignedIn();
      allow create: if isSignedIn()
        && request.resource.data.userId == request.auth.uid
        && request.resource.data.officeId is string
        && request.resource.data.date is string
        && request.resource.data.time is string
        && request.resource.data.reservationId is string;
      allow delete: if isOwner(resource.data.userId);
      allow update: if false;
    }

    match /{document=**} {
      allow read, write: if false;
    }
  }
}