Skip to content

Commit 7a5afeb

Browse files
rfontanarosagino-m
andauthored
Update rules to allow users to see data collected by others (#2211)
Co-authored-by: Gino Miceli <[email protected]>
1 parent 215f73b commit 7a5afeb

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

firestore/firestore.rules

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@
3030
* data to the specified survey.
3131
*/
3232
function isUnlistedOrPublic(survey) {
33-
return survey["8"] in [
33+
return survey["8"] in [
3434
2 /* UNLISTED */,
3535
3 /* PUBLIC */
3636
];
3737
}
3838

39+
/**
40+
* Returns true iff data collectors can see each others' data.
41+
*/
42+
function canViewDataCollectedByOthers(survey) {
43+
return survey["9"] == 2 /* ALL_SURVEY_PARTICIPANTS */
44+
}
45+
3946
/**
4047
* Returns the current user's role in the given survey.
4148
*/
@@ -67,14 +74,30 @@
6774
}
6875

6976
/**
70-
* Returns true iff the user with the given email can read the specified
77+
* Returns true iff the user with the user's email can read the specified
7178
* survey.
7279
*/
7380
function canViewSurvey(survey) {
7481
return canAccess() &&
7582
(isUnlistedOrPublic(survey) || getRole(survey) != null);
7683
}
7784
85+
/**
86+
* Returns true iff user can see each other's lois in the specified
87+
* survey.
88+
*/
89+
function canViewLoi(survey, loi) {
90+
return canViewSurvey(survey) && (isLoiOwner(loi) || canViewDataCollectedByOthers(survey));
91+
}
92+
93+
/**
94+
* Returns true iff user can see each other's submissions in the specified
95+
* survey.
96+
*/
97+
function canViewSubmission(survey, submission) {
98+
return canViewSurvey(survey) && (isSubmissionOwner(submission) || canViewDataCollectedByOthers(survey));
99+
}
100+
78101
/**
79102
* Returns true if the current user has one of the specified roles in the
80103
* given survey.
@@ -95,7 +118,7 @@
95118
}
96119
97120
/**
98-
* Returns true iff the current user with the given email can contribute LOIs
121+
* Returns true iff the current user with the given email can contribute LOIs
99122
* and submissions to the specified survey.
100123
*/
101124
function canCollectData(survey) {
@@ -149,20 +172,20 @@
149172
allow read: if request.auth != null;
150173
}
151174
152-
// Apply passlist and survey-level ACLs to LOI documents.
175+
// Apply passlist and survey-level General Access and/or ACLs to LOI documents.
153176
match /surveys/{surveyId}/lois/{loiId} {
154-
// Allow if user has has read access to the survey.
155-
allow read: if canViewSurvey(getSurvey(surveyId));
177+
// Allow if user has read access to the survey and the LOI.
178+
allow read: if canViewLoi(getSurvey(surveyId), request.resource);
156179
// Allow if user is owner of the new LOI and can collect data.
157180
allow create: if isLoiOwner(request.resource) && canCollectData(getSurvey(surveyId));
158181
// Allow if user is owner of the existing LOI or can manage survey.
159182
allow write: if isLoiOwner(resource) || canManageSurvey(getSurvey(surveyId));
160183
}
161184
162-
// Apply passlist and survey-level ACLs to submission documents.
185+
// Apply passlist and survey-level General Access and/or ACLs to submission documents.
163186
match /surveys/{surveyId}/submissions/{submissionId} {
164-
// Allow if user has has read access to the survey.
165-
allow read: if canViewSurvey(getSurvey(surveyId));
187+
// Allow if user has read access to the survey and the submission.
188+
allow read: if canViewSubmission(getSurvey(surveyId), request.resource);
166189
// Allow if user is owner of the new submission and can collect data.
167190
allow create: if isSubmissionOwner(request.resource) && canCollectData(getSurvey(surveyId));
168191
// Allow if user is owner of the existing submission or can manage survey.
@@ -171,7 +194,7 @@
171194
172195
// Apply passlist and survey-level ACLs to job documents.
173196
match /surveys/{surveyId}/jobs/{jobId} {
174-
// Allow if user has has read access to the survey.
197+
// Allow if user has read access to the survey.
175198
allow read: if canViewSurvey(getSurvey(surveyId));
176199
// Allow if user can manage survey.
177200
allow create, write: if canManageSurvey(getSurvey(surveyId));

0 commit comments

Comments
 (0)