-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfirestore.rules
165 lines (133 loc) · 5.63 KB
/
firestore.rules
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
rules_version = '2';
service cloud.firestore {
match /databases/{databaseId}/documents {
function isAuthenticated() {
return request.auth != null && ('type' in request.auth.token);
}
function isAdmin() {
return isAuthenticated()
&& request.auth.token.type == 'admin';
}
function isPartOf(organizationId) {
return ('organization' in request.auth.token)
&& request.auth.token.organization == organizationId;
}
function isOwnerOf(organizationId) {
return organizationId != null
&& isAuthenticated()
&& request.auth.token.type == 'owner'
&& isPartOf(organizationId);
}
function isOwnerOrClinicianOf(organizationId) {
return organizationId != null
&& isAuthenticated()
&& request.auth.token.type in ['owner', 'clinician']
&& isPartOf(organizationId);
}
function isUser(userId) {
return isAuthenticated()
&& request.auth.uid == userId;
}
function getUser(userId) {
return get(/databases/$(databaseId)/documents/users/$(userId));
}
function getInvitation(invitationId) {
return get(/databases/$(databaseId)/documents/invitations/$(invitationId));
}
function isEqualOptional(object0, property0, object1, property1) {
return (property0 in object0)
? ((property1 in object1) && object0[property0] == object1[property1])
: !(property1 in object1);
}
match /invitations/{invitationId} {
function securityRelatedFieldsDidNotChange() {
return isEqualOptional(request.resource.data.user, 'type', resource.data.user, 'type')
&& isEqualOptional(request.resource.data.user, 'organization', resource.data.user, 'organization')
}
allow read, delete: if isAdmin() || isOwnerOrClinicianOf(resource.data.user.organization);
allow create: if isAdmin();
allow update: if isAdmin() || (securityRelatedFieldsDidNotChange() && isOwnerOrClinicianOf(resource.data.user.organization));
}
match /invitations/{invitationId}/{collectionName}/{documentId} {
function isOwnerOrClinicianOfSameOrganization() {
let invitation = getInvitation(invitationId);
return invitation != null && isOwnerOrClinicianOf(invitation.data.user.organization);
}
function isPatientWritableCollectionName() {
return collectionName.matches('^[A-Za-z]+Observations$')
|| collectionName in ['questionnaireResponses']
}
function isClinicianWritableCollectionName() {
return isPatientWritableCollectionName()
|| collectionName in ['allergyIntolerances', 'appointments', 'medicationRequests'];
}
allow read: if isAdmin()
|| isOwnerOrClinicianOfSameOrganization();
allow write: if isAdmin()
|| (isOwnerOrClinicianOfSameOrganization() && isClinicianWritableCollectionName());
}
match /medicationClasses/{medicationClassId} {
allow read: if isAuthenticated();
allow write: if isAdmin();
}
match /medications/{documents=**} {
allow read: if isAuthenticated();
allow write: if isAdmin();
}
match /organizations/{organizationId} {
allow read: if isAdmin() || isPartOf(organizationId);
allow update: if isAdmin() || isOwnerOf(organizationId);
allow create, delete: if isAdmin();
}
match /questionnaires/{questionnaireId} {
allow read: if isAuthenticated();
allow write: if isAdmin();
}
match /users/{userId} {
function securityRelatedFieldsDidNotChange() {
return isEqualOptional(request.resource.data, 'type', resource.data, 'type')
&& isEqualOptional(request.resource.data, 'organization', resource.data, 'organization');
}
function isAllowedUpdateWithinOrganization() {
return resource.data.type in ['patient']
? isOwnerOf(resource.data.organization)
: isOwnerOrClinicianOf(resource.data.organization) && resource.data.type in ['clinician'];
}
allow read: if isAdmin()
|| (resource == null && isAuthenticated())
|| (request.auth != null && request.auth.uid == userId)
|| isOwnerOrClinicianOf(resource.data.organization);
allow create: if isAdmin()
|| (isUser(userId) && !('organization' in request.resource.data) && !('type' in request.resource.data));
allow update: if isAdmin()
|| (securityRelatedFieldsDidNotChange() && (isUser(userId) || isAllowedUpdateWithinOrganization()));
allow delete: if isAdmin();
}
match /users/{userId}/{collectionName}/{documentId} {
function isOwnerOrClinicianOfSameOrganization() {
let userDoc = getUser(userId);
return (userDoc.data != null)
&& ('organization' in userDoc.data)
&& isOwnerOrClinicianOf(userDoc.data.organization);
}
function isPatientWritableCollectionName() {
return collectionName.matches('^[A-Za-z]+Observations$')
|| collectionName in ['questionnaireResponses']
}
function isClinicianWritableCollectionName() {
return isPatientWritableCollectionName()
|| collectionName in ['allergyIntolerances', 'appointments', 'medicationRequests'];
}
allow read: if isAdmin()
|| (request.auth != null && request.auth.uid == userId)
|| isOwnerOrClinicianOfSameOrganization();
allow write: if isAdmin()
|| (isUser(userId) && isPatientWritableCollectionName())
|| (isOwnerOrClinicianOfSameOrganization() && isClinicianWritableCollectionName());
}
match /videoSections/{documents=**} {
allow read: if isAuthenticated();
allow write: if isAdmin();
}
}
}