-
Notifications
You must be signed in to change notification settings - Fork 1
/
firestore.bak.rules
546 lines (481 loc) · 19.8 KB
/
firestore.bak.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Enforce a global whitelist-only rule on all documents.
match /{document=**} {
allow read, write: if false;
}
match /users/{docId} {
// Allow GET, if the document belongs to the authenticated user.
allow get: if isAuthenticated()
&& request.auth.uid == docId;
// Allow GET, if the authenticated user has an access level of 2 or more.
allow get: if isAuthenticated()
&& getAccessLevel() >= 2;
// Allow LIST, if the authenticated user has an access level of 2 or more,
// and the request limit <= QUERY_LIMIT.
// Refer to function QUERY_LIMIT.
allow list: if isAuthenticated()
&& getAccessLevel() >= 2
&& isWithinQueryLimit(15);
// Allow UPDATE, if
// * the user is authenticated;
// * the user has an access level of 3 or more;
// * the update request only has the field `Access Level`;
// * the field value `Access Level` is the data type of int;
// * the request field `Access Level` is lower than the resource field;
allow update: if isAuthenticated()
&& getAccessLevel() >= 3
&& isValidUpdate();
// Allow CREATE, if
// * the user is authenticated;
// * the document belongs to the user;
// * the document structure matches the data model;
// * the field `User ID` is of type string;
// * the field `Email` is of type string;
// * the field `Photo URL` is of type string;
// * the field `Display Name` is of type string;
// * the field `Access Level` is of type int;
// * the field `updatedAt` is of type timestamp;
// * the field `createdAt` is of type timestamp;
//
// * the field `User ID` == the user uid;
// * the field `Email` == the user email;
// * the field `Display Name` == user name;
// * the field `Membership ID` == null;
// * the field `Access Level` == 0;
// * the field `updatedAt` == current timestamp;
// * the field `createdAt` == current timestamp;
allow create: if isAuthenticated()
&& isValidCreate();
// Deny DELETE operations; handled by Cloud Functions.
allow delete: if false;
function isValidUpdate() {
let affectedKeys = getIncomingData().diff(resource.data).affectedKeys();
return affectedKeys.hasOnly(['Access Level', 'updatedAt'])
&& getIncomingData()['Access Level'] is int
&& getIncomingData()['updatedAt'] is timestamp
&& getIncomingData()['Access Level'] <= getExistingData()['Access Level']
&& getIncomingData()['updatedAt'].toMillis() == request.time.toMillis();
}
function isValidCreate() {
let authUser = request.auth;
let allowedKeys = [
'User ID',
'Email',
'Photo URL',
'Display Name',
'Membership ID',
'Access Level',
'updatedAt',
'createdAt'
];
return docId == authUser.uid
&& getIncomingData().keys().hasOnly(allowedKeys)
&& getIncomingData()['User ID'] is string
&& getIncomingData()['Email'] is string
&& getIncomingData()['Photo URL'] is string
&& getIncomingData()['Display Name'] is string
&& getIncomingData()['Access Level'] is int
&& getIncomingData()['updatedAt'] is timestamp
&& getIncomingData()['createdAt'] is timestamp
&& getIncomingData()['User ID'] == authUser.uid
&& getIncomingData()['Email'] == authUser.token.email
&& getIncomingData()['Photo URL'] != ''
&& getIncomingData()['Display Name'] == authUser.token.name
&& getIncomingData()['Membership ID'] == null
&& getIncomingData()['Access Level'] == 0
&& getIncomingData()['updatedAt'].toMillis() == request.time.toMillis()
&& getIncomingData()['createdAt'].toMillis() == request.time.toMillis();
}
}
match /members/{memberId} {
allow get: if isAuthenticated()
&& getUserData()['Membership ID'] == memberId;
allow get: if isAuthenticated()
&& hasPermissionLevel(2);
allow list: if isAuthenticated()
&& hasPermissionLevel(2)
&& isWithinQueryLimit(15);
allow create: if isAuthenticated()
&& hasPermissionLevel(2)
&& isValidCreate();
allow update: if isAuthenticated()
&& hasPermissionLevel(2)
&& isValidUpdate();
allow delete: if isAuthenticated()
&& hasPermissionLevel(2)
&& isValidUser();
function isValidCreate() {
let data = getIncomingData();
let allowedKeys = [
'Membership ID',
'Full Name',
'Gender',
'Email',
'Contact Number',
'Home Number',
'Current School',
'Graduating Class',
'Graduating Year',
'Name Of Next-Of-Kin',
'Relationship With Next-Of-Kin',
'Contact Number Of Next-Of-Kin',
'updatedAt',
'createdAt'
];
return data.keys().hasOnly(allowedKeys)
&& data['Membership ID'] is string
&& data['Full Name'] is string
&& data['Gender'] is string
&& (data['Email'] is string || isNull('Email'))
&& data['Contact Number'] is int
&& (data['Home Number'] is int || isNull('Home Number'))
&& (data['Current School'] is string || isNull('Current School'))
&& data['Graduating Class'] is string
&& data['Graduating Year'] is int
&& data['Name Of Next-Of-Kin'] is string
&& data['Relationship With Next-Of-Kin'] is string
&& data['Contact Number Of Next-Of-Kin'] is int
&& data['updatedAt'] is timestamp
&& data['createdAt'] is timestamp
&& data['Membership ID'] == memberId
&& isNotEmpty('Full Name')
&& isOneOf('Gender', ['Male', 'Female'])
&& (isNotEmpty('Email') || isNull('Email'))
&& isValidPhoneNumber('Contact Number')
&& (isValidPhoneNumber('Home Number') || isNull('Home Number'))
&& (isNotEmpty('Current School') || isNull('Current School'))
&& isNotEmpty('Graduating Class')
&& isValidGraduatingClass('Graduating Class')
&& isValidGraduatingYear('Graduating Year')
&& isNotEmpty('Name Of Next-Of-Kin')
&& isNotEmpty('Relationship With Next-Of-Kin')
&& isValidPhoneNumber('Contact Number Of Next-Of-Kin')
&& data['updatedAt'].toMillis() == request.time.toMillis()
&& data['createdAt'].toMillis() == request.time.toMillis();
}
function isValidUpdate() {
let data = getIncomingData();
let affectedKeys = request.resource.data.diff(resource.data).affectedKeys();
let allowedKeys = [
'Full Name',
'Gender',
'Email',
'Contact Number',
'Home Number',
'Current School',
'Graduating Class',
'Graduating Year',
'Name Of Next-Of-Kin',
'Relationship With Next-Of-Kin',
'Contact Number Of Next-Of-Kin',
'updatedAt'
];
return affectedKeys.hasOnly(allowedKeys)
&& data['Full Name'] is string
&& data['Gender'] is string
&& (data['Email'] is string || isNull('Email'))
&& data['Contact Number'] is int
&& (data['Home Number'] is int || isNull('Home Number'))
&& (data['Current School'] is string || isNull('Current School'))
&& data['Graduating Class'] is string
&& data['Graduating Year'] is int
&& data['Name Of Next-Of-Kin'] is string
&& data['Relationship With Next-Of-Kin'] is string
&& data['Contact Number Of Next-Of-Kin'] is int
&& data['updatedAt'] is timestamp
&& isNotEmpty('Full Name')
&& isOneOf('Gender', ['Male', 'Female'])
&& (isNotEmpty('Email') || isNull('Email'))
&& isValidPhoneNumber('Contact Number')
&& (isValidPhoneNumber('Home Number') || isNull('Home Number'))
&& (isNotEmpty('Current School') || isNull('Current School'))
&& isNotEmpty('Graduating Class')
&& isValidGraduatingClass('Graduating Class')
&& isValidGraduatingYear('Graduating Year')
&& isNotEmpty('Name Of Next-Of-Kin')
&& isNotEmpty('Relationship With Next-Of-Kin')
&& isValidPhoneNumber('Contact Number Of Next-Of-Kin')
&& data['updatedAt'].toMillis() == request.time.toMillis();
}
}
match /events/{eventId} {
allow get: if isAuthenticated()
&& hasPermissionLevel(1)
&& isNotEmpty('Membership ID');
allow list: if isAuthenticated()
&& hasPermissionLevel(1)
&& isWithinQueryLimit(15);
allow create: if isAuthenticated()
&& hasPermissionLevel(2)
&& isValidCreate();
function isValidCreate() {
let data = getIncomingData();
let allowedKeys = [
'Event Code',
'Event Year',
'Event Name',
'Event Thumbnail',
'Event Overall In-Charge',
'Event Assistant In-Charge',
'Google Drive',
'Roles',
'Official Event',
'updatedAt',
'createdAt'
];
return data.keys().hasOnly(allowedKeys)
&& data['Event Code'] is int
&& data['Event Year'] is int
&& data['Event Name'] is string
&& data['Event Thumbnail'] is string
&& data['Event Overall In-Charge'] is string
&& data['Event Assistant In-Charge'] is string
&& data['Google Drive'] is string
&& data['Roles'] is list
&& data['Official Event'] is bool
&& data['updatedAt'] is timestamp
&& data['createdAt'] is timestamp
&& data['Event Code'] == eventId
&& isWithinRange(data['Event Year'], 2000, request.time.year())
&& isNotEmpty('Event Name')
&& isNotEmpty('Event Thumbnail')
&& isNotEmpty('Event Overall In-Charge')
&& isNotEmpty('Event Assistant In-Charge')
&& isNotEmpty('Google Drive')
&& isNotEmpty('Roles')
&& data['updatedAt'].toMillis() == request.time.toMillis()
&& data['createdAt'].toMillis() == request.time.toMillis();
}
}
// Events collection contains all the past and upcoming events.
//
// GET If the authenticated user has an access level of 1 (Alumni) or
// higher.
//
// LIST If the authenticated user has an access level of 1 (Alumni) or
// higher and the query limit not exceeding 10 documents at once.
//
// CREATE If the authenticated user has an access level of 2 (Editor) or
// higher.
//
// UPDATE If the authenticated user has an access level of 2 (Editor) or
// higher and having the locked document data not being modified.
//
// DELETE If the authenticated user has an access level of 3 (Administrator)
// or higher.
match /events/{eventId} {
allow get: if isAuthenticated()
&& getAccessLevel() >= 1
&& getUserData()['Membership ID'] != null;
allow list: if isAuthenticated()
&& getAccessLevel() >= 1
&& request.query.limit <= QUERY_LIMIT();
allow create: if isAuthenticated()
&& getAccessLevel() >= 2;
allow update: if isAuthenticated()
&& getAccessLevel() >= 2
&& getIncomingData()['Event Code'] == getExistingData()['Event Code']
&& getIncomingData()['Event Year'] == getExistingData()['Event Year']
&& getIncomingData()['updatedAt'] == getExistingData()['updatedAt']
&& getIncomingData()['createdAt'] == getExistingData()['createdAt'];
allow delete: if isAuthenticated()
&& getAccessLevel() >= 3;
}
// Participations collection contains all records of alumni participation.
// Used for identifying events participated and collating total VIA hours.
//
// GET If the document belongs to the authenticated user or the
// authenticated user has an access level of 2 (Editor) or higher.
//
// LIST If the document belongs to the authenticated user or the
// authenticated user has an access level of 2 (Editor) or higher.
// And the query limit not exceeding 10 documents at once.
//
// CREATE If the authenticated user has an access level of 2 (Editor) or
// higher.
//
// UPDATE If the authenticated user has an access level of 2 (Editor) or
// higher and the locked document data not being modified.
//
// DELETE If the authenticated user has an access level of 3 (Administrator)
// or higher.
match /participations/{participationId} {
allow get: if isAuthenticated()
&& getExistingData()['Membership ID'] == getUserData()['Membership ID'];
allow get: if isAuthenticated()
&& hasPermissionLevel(2);
allow list: if isAuthenticated()
&& getExistingData()['Membership ID'] == getUserData()['Membership ID']
&& isWithinQueryLimit(15);
allow list: if isAuthenticated()
&& hasPermissionLevel(2)
&& isWithinQueryLimit(15);
allow create: if isAuthenticated()
&& hasPermissionLevel(2)
&& isValidCreate();
allow update: if isAuthenticated()
&& hasPermissionLevel(2)
&& isValidUpdate();
allow delete: if isAuthenticated()
&& hasPermissionLevel(2)
&& isValidUser();
function isValidCreate() {
let data = getIncomingData();
let allowedKeys = [
'Participation ID',
'Membership ID',
'Event Code',
'Role',
'VIA Hours',
'updatedAt',
'createdAt'
];
return data.keys().hasOnly(allowedKeys)
&& data['Participation ID'] is string
&& data['Membership ID'] is string
&& data['Event Code'] is int
&& data['Role'] is string
&& (data['VIA Hours'] is int
|| data['VIA Hours'] is float)
&& data['updatedAt'] is timestamp
&& data['createdAt'] is timestamp
&& data['Participation ID'] == participationId
&& isNotEmpty('Membership ID')
&& isNotEmpty('Event Code')
&& isNotEmpty('Role')
&& isNotEmpty('VIA Hours')
&& data['updatedAt'].toMillis() == request.time.toMillis()
&& data['createdAt'].toMillis() == request.time.toMillis();
}
function isValidUpdate() {
let data = getIncomingData();
let affectedKeys = request.resource.data.diff(resource.data).affectedKeys();
let allowedKeys = [
'Role',
'VIA Hours',
'updatedAt'
];
return affectedKeys.hasOnly(allowedKeys)
&& data['Role'] is string
&& data['VIA Hours'] is float
&& isNotEmpty(data['Role'])
&& isNotEmpty(data['VIA Hours'])
&& data['updatedAt'].toMillis() == request.time.toMillis();
}
// allow list: if isAuthenticated()
// && getExistingData()['Membership ID'] == getUserData()['Membership ID']
// && request.query.limit <= QUERY_LIMIT();
// allow get: if isAuthenticated()
// && getAccessLevel() >= 2;
// allow list: if isAuthenticated()
// && getAccessLevel() >= 2
// && request.query.limit <= QUERY_LIMIT();
// allow create: if isAuthenticated()
// && getAccessLevel() >= 2;
// allow update: if isAuthenticated()
// && getAccessLevel() >= 2
// && getIncomingData()['Membership ID'] == getExistingData()['Membership ID']
// && getIncomingData()['Event Code'] == getExistingData()['Event Code']
// && getIncomingData()['updatedAt'] == getExistingData()['updatedAt']
// && getIncomingData()['createdAt'] == getExistingData()['createdAt'];
// allow delete: if isAuthenticated()
// && getAccessLevel() >= 3;
}
match /aggregations/{aggregationId} {
allow get: if isAuthenticated()
&& getAccessLevel() >= 2;
allow list,
create,
update,
delete: if false;
}
function isAuthenticated() {
return request.auth != null
&& request.auth.token.email_verified
&& exists(/databases/$(database)/documents/users/$(request.auth.uid));
}
function hasPermissionLevel(level) {
let currentUser = get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
return currentUser['Access Level'] >= level;
}
function getExistingData() {
return resource.data;
}
function getIncomingData() {
return request.resource.data;
}
function getUserData() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
}
function getAccessLevel() {
return getUserData()['Access Level'];
}
function isNull(key) {
return request.resource.data[key] == null;
}
function isOneOf(key, list) {
return request.resource.data[key] in list;
}
function isNotEmpty(key) {
let data = request.resource.data[key];
return (data is string
&& data.trim() != ''
&& data.trim().size() > 0)
|| (data is int
&& data >= 0)
|| (data is float
&& data >= 0)
|| (data is list
&& data.size() > 0);
}
function isWithinRange(number, start, end) {
return number is int
&& (start <= number) && (number <= end);
}
function isWithinQueryLimit(limit) {
return request.query.limit <= limit;
}
function isValidUser() {
let userExists = exists(/databases/$(database)/documents/users/$(request.auth.uid));
let currentUser = get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
let memberExists = exists(/databases/$(database)/documents/members/$(currentUser['Membership ID']));
return userExists && memberExists;
}
function isValidPhoneNumber(key) {
let data = request.resource.data[key];
return data is int
&& (isWithinRange(data, 81000000, 99999999)
|| isWithinRange(data, 60000000, 69999999));
}
function isValidGraduatingClass(key) {
let classes = [
'6A',
'6B',
'6C',
'6D',
'6E',
'6F',
'6G',
'6H',
'6I',
'6J',
'6 Respect',
'6 Resilience',
'6 Responsibility',
'6 Integrity',
'6 Care',
'6 Harmony'
];
return request.resource.data[key] in classes;
}
function isValidGraduatingYear(key) {
let data = request.resource.data[key];
return isWithinRange(data, 2000, request.time.year());
}
function QUERY_LIMIT() {
return 15;
}
}
}