|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
| 17 | +rules_version = '2'; |
| 18 | + |
17 | 19 | // Define access rules for Firestore collections and documents. |
18 | 20 | service cloud.firestore { |
19 | 21 | match /databases/{database}/documents { |
|
22 | 24 | * Fetches and returns the survey with the specified id. |
23 | 25 | */ |
24 | 26 | function getSurvey(surveyId) { |
25 | | - return get(/databases/$(database)/documents/surveys/$(surveyId)).data; |
| 27 | + let doc = get(/databases/$(database)/documents/surveys/$(surveyId)); |
| 28 | + return doc == null ? null : doc.data; |
26 | 29 | } |
27 | 30 |
|
28 | 31 | /** |
|
55 | 58 | * Returns the regular expression matching emails granted access. |
56 | 59 | */ |
57 | 60 | function getPassRegexp() { |
58 | | - return get(/databases/$(database)/documents/passlist/regexp).data.regexp |
| 61 | + let doc = get(/databases/$(database)/documents/passlist/regexp); |
| 62 | + return doc == null ? null : doc.data.regexp; |
59 | 63 | } |
60 | 64 |
|
61 | 65 | /** |
62 | 66 | * Returns true iff the user's email is listed in the passlist or allowed via regex. |
63 | 67 | */ |
64 | 68 | function isPasslisted() { |
65 | | - return request.auth.token.email.matches(getPassRegexp()) |
| 69 | + let regexp = getPassRegexp(); |
| 70 | + return (regexp != null && request.auth.token.email.matches(regexp)) |
66 | 71 | || exists(/databases/$(database)/documents/passlist/$(request.auth.token.email)); |
67 | 72 | } |
68 | 73 |
|
|
78 | 83 | * survey. |
79 | 84 | */ |
80 | 85 | function canViewSurvey(survey) { |
81 | | - return isSignedIn() && |
| 86 | + return survey != null && isSignedIn() && |
82 | 87 | (isUnlistedOrPublic(survey) || getRole(survey) != null); |
83 | 88 | } |
84 | 89 |
|
|
113 | 118 | * assigned this role by default. |
114 | 119 | */ |
115 | 120 | function canManageSurvey(survey) { |
116 | | - return isSignedIn() && isOneOf(survey, [ |
| 121 | + return survey != null && isSignedIn() && isOneOf(survey, [ |
117 | 122 | 3 /* SURVEY_ORGANIZER */ |
118 | 123 | ]); |
119 | 124 | } |
|
123 | 128 | * and submissions to the specified survey. |
124 | 129 | */ |
125 | 130 | function canCollectData(survey) { |
126 | | - return isSignedIn() && |
| 131 | + return survey != null && isSignedIn() && |
127 | 132 | (isUnlistedOrPublic(survey) || isOneOf(survey, [ |
128 | 133 | 2 /* DATA_COLLECTOR */, |
129 | 134 | 3 /* SURVEY_ORGANIZER */ |
|
0 commit comments