-
Notifications
You must be signed in to change notification settings - Fork 88
/
firestore.rules
167 lines (147 loc) · 5.72 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
166
167
/**
* Copyright 2020 The Ground Authors.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Define access rules for Firestore collections and documents.
service cloud.firestore {
match /databases/{database}/documents {
/**
* Fetches and returns the survey with the specified id.
*/
function getSurvey(surveyId) {
return get(/databases/$(database)/documents/surveys/$(surveyId)).data;
}
/**
* Returns the current user's role in the given survey.
*/
function getRole(survey) {
// Get role from `acl` nested object (field 4).
return survey["4"][request.auth.token.email];
}
/**
* Returns the regular expression matching emails granted access.
*/
function getPassRegexp() {
return get(/databases/$(database)/documents/passlist/regexp).data.regexp
}
/**
* Returns true iff the user's email is explicitly listed in the passlist.
*/
function inPasslist() {
return exists(/databases/$(database)/documents/passlist/$(request.auth.token.email));
}
/**
* Returns true iff the user's email matches the passlist regex or
* is explicitly listed in the passlist.
*/
function canAccess() {
return request.auth != null && (request.auth.token.email.matches(getPassRegexp()) || inPasslist());
}
/**
* Returns true iff the user with the given email can read the specified
* survey.
*/
function canViewSurvey(survey) {
return canAccess() && getRole(survey) != null;
}
/**
* Returns true if the current user has one of the specified roles in the
* given survey.
*/
function isOneOf(survey, roles) {
return survey["4"][request.auth.token.email] in roles;
}
/**
* Returns true if the current user is passlisted and has the `SURVEY_ORGANIZER` role
* in the specified survey. Note that this include survey owners, since they are
* assigned this role by default.
*/
function canManageSurvey(survey) {
return canAccess() && isOneOf(survey, [
3 /* SURVEY_ORGANIZER */
]);
}
/**
* Returns true iff the current user with the given email can contribute LOIs
* and submissions to the specified survey.
*/
function canCollectData(survey) {
return canAccess() && isOneOf(survey, [
2 /* DATA_COLLECTOR */,
3 /* SURVEY_ORGANIZER */
]);
}
/**
* Returns true iff the current user is the owner of the specified LOI.
*/
function isLoiOwner(loi) {
return loi.data['5'] == request.auth.uid;
}
/**
* Returns true iff the current user is the owner of the specified submission.
*/
function isSubmissionOwner(submission) {
return submission.data['5'] == request.auth.uid;
}
// Allow users to read, create, and write their own profiles in the db.
match /users/{userId} {
allow read: if userId == request.auth.uid || canAccess();
allow create, write: if userId == request.auth.uid;
}
// All users need to be able to read the passlist for rules to work.
match /passlist/{entry} {
// TODO(#1602): Replace "regexp" with simple pattern matching.
allow read: if request.auth != null && entry in ['regexp', request.auth.token.email];
}
// Apply passlist and survey-level ACLs to survey documents.
match /surveys/{surveyId} {
allow read: if canViewSurvey(resource.data);
allow write: if canManageSurvey(resource.data);
allow create: if canAccess();
}
// Allow passlisted users to access Terms of Service and other config.
match /config/{id} {
allow read: if canAccess();
}
// Allow all users to access Access Denied config.
match /config/accessDenied {
allow read: if request.auth != null;
}
// Apply passlist and survey-level ACLs to LOI documents.
match /surveys/{surveyId}/lois/{loiId} {
// Allow if user has has read access to the survey.
allow read: if canViewSurvey(getSurvey(surveyId));
// Allow if user is owner of the new LOI and can collect data.
allow create: if isLoiOwner(request.resource) && canCollectData(getSurvey(surveyId));
// Allow if user is owner of the existing LOI or can manage survey.
allow write: if isLoiOwner(resource) || canManageSurvey(getSurvey(surveyId));
}
// Apply passlist and survey-level ACLs to submission documents.
match /surveys/{surveyId}/submissions/{submissionId} {
// Allow if user has has read access to the survey.
allow read: if canViewSurvey(getSurvey(surveyId));
// Allow if user is owner of the new submission and can collect data.
allow create: if isSubmissionOwner(request.resource) && canCollectData(getSurvey(surveyId));
// Allow if user is owner of the existing submission or can manage survey.
allow write: if isSubmissionOwner(resource) || canManageSurvey(getSurvey(surveyId));
}
// Apply passlist and survey-level ACLs to job documents.
match /surveys/{surveyId}/jobs/{jobId} {
// Allow if user has has read access to the survey.
allow read: if canViewSurvey(getSurvey(surveyId));
// Allow if user can manage survey.
allow create, write: if canManageSurvey(getSurvey(surveyId));
}
}
}