-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
94 lines (78 loc) · 3.48 KB
/
firestore.rules
File metadata and controls
94 lines (78 loc) · 3.48 KB
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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
/** VALIDATION FUNCTIONS **/
// check how ID is formed
function checkDId( dId) {
return ( dId != null && int(dId) is int && int(dId) > 0);
}
// check if exist document with same ID
function checkIdAsId( dId) {
return !(exists(/databases/$(database)/documents/Drink/$(dId)));
}
function checkTitle( title) {
return (title != null && title.size() < 120);
}
function checkDescription( description) {
return (description != null && description.size() < 120);
}
/** VALIDATION RULES **/
match /{Drink}/{document=**} {
/** RULES FOR allow read WITH CONVENIENCE METHOD - LOW GRANULARITY **/
/** NO authentication required **/
allow read: if request.auth != null;
/** RULES FOR allow write WITH STANDARD METHODS - HIGH GRANULARITY **/
/** authentication required **/
//validate when create new drink
allow create: if request.auth.token.email_verified == true
&& checkIdAsId( request.resource.data.dId) == true
&& checkDId( request.resource.data.dId) == true
&& checkTitle(request.resource.data.title) == true
&& checkDescription (request.resource.data.description) == true
;
// validate when update drink
allow update: if request.auth.token.email_verified == true
&& (request.resource.data.diff( resource.data).affectedKeys()
.hasOnly(['title', 'description']))
&& request.resource.data.title != null ?
checkTitle( request.resource.data.title) : true
&& request.resource.data.description != null ?
checkDescription( request.resource.data.description) : true
// validate when delete drink
allow delete: if request.auth.token.email_verified == true;
}
//-- BobaTea -------------------------------------------------------------------------------------
function checkTIdAsId( tId) {
return !(exists(/databases/$(database)/documents/BobaTea/$(tId)));
}
function checkTId( tId) {
return ( tId != null && int(tId) is int && int(tId) > 0);
}
function checkDrink( d) {
return ( d != null && d.checkDId);
}
function checkBoba( b) {
return ( b != null);
}
function checkRate (r){
//return (( 0 <= int(r) && int(r) <= 5));
return ( r != null)
}
match /{BobaTea}/{document=**} {
/** RULES FOR allow read WITH CONVENIENCE METHOD - LOW GRANULARITY **/
/** NO authentication required **/
allow read: if request.auth != null;
/** RULES FOR allow write WITH STANDARD METHODS - HIGH GRANULARITY **/
/** authentication required **/
//validate when create new bobatea
allow create: if request.auth != null
&& checkTIdAsId( request.resource.data.tId) == true
&& checkTId( request.resource.data.tId) == true
&& checkTitle(request.resource.data.title) == true
//&& checkDrink(request.resource.data.drink) == true
&& checkBoba(request.resource.data.drink) == true;
allow update: if request.auth != null
&& checkRate (request.resource.data.rating) == true;
}
}
}