forked from MPH-Bali/green-village-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirestore.rules
204 lines (160 loc) · 4.8 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
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
service cloud.firestore {
match /databases/{database}/documents {
allow read: if true;
function isSignedIn() {
return request.auth != null;
}
function getDoc(id, col) {
return get(/databases/$ (database)/documents/$(col)/$(id)).data
}
function docExists(id, col) {
return exists(/databases/$ (database)/documents/$(col)/$(id))
}
function data() {
return request.resource.data;
}
function validTimestamp() {
return data().timestamp is timestamp;
}
function isEmployee(id) {
return getDoc(id, 'person').type.employee == true;
}
function isBuyer(id) {
return getDoc(id, 'person').type.buyer == true;
}
match /delivery/{deliveryId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['driver', 'banjar', 'timestamp'])
//driver exists and his role is employee
&&
isEmployee(data().driver.id)
//timestamp
&&
validTimestamp()
//banjar exists
&&
docExists(data().banjar.id, 'banjar')
}
match /sales/{saleId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['buyer', 'materials']) &&
isBuyer(data().buyer.id)
}
match /settings/{settingsId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['name', 'village']) &&
data().name is string &&
data().village is string
}
match /expense/{expenseId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['cost']) &&
data().cost is int
}
match /stock/{stockId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['timestamp', 'material', 'amount']) &&
data().amount is float &&
validTimestamp()
}
match /fee/{feeId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['monthly_fee', 'total_paid', 'paid_until', 'timestamp']) &&
data().monthly_fee is int &&
data().total_paid is int &&
validTimestamp() &&
data().paid_until is timestamp
}
match /banjar/{banjarId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['name']) &&
data().name is string
}
match /material/{materialId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['worker', 'timestamp']) &&
data().keys().hasAny(['organic', 'inorganic']) &&
isEmployee(data().worker.id) &&
validTimestamp()
}
match /materialType/{materialTypeId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['name', 'pricePerKilo']) &&
data().name is string &&
data().pricePerKilo is int;
}
match /houseType/{houseTypeId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['name', 'collectionFee']) &&
data().name is string &&
data().collectionFee is int;
}
match /workerhours/{workerhoursId} {
allow read: if true;
allow create, update:
if data().keys().hasAll(['worker', 'times']) &&
data().times.keys().hasAll(['in', 'out']) &&
isEmployee(data().worker.id) &&
(data().times.in is timestamp || data().times.out is timestamp)
}
match /person/{personId} {
allow read: if true;
function emailIsValid() {
return data().email == null || data().email.matches("\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}\\b")
}
function itemInMapIsTrue(item, map) {
return (item in map.keys()) && map[item] == true;
}
function hasEmployeeType() {
return itemInMapIsTrue('employee', data().type);
}
function isClient() {
return itemInMapIsTrue('client', data().type);
}
function isCommunityManager() {
return itemInMapIsTrue('communityManager', data().role);
}
function isFacilityManager() {
return itemInMapIsTrue('facilityManager', data().role);
}
function isSuperAdmin() {
return itemInMapIsTrue('superAdmin', data().role);
}
function hasOneType() {
return (hasEmployeeType() && !isClient()) || (!hasEmployeeType() && isClient());
}
function hasOneRole() {
return ((isCommunityManager() && !isFacilityManager() && !isSuperAdmin()) ||
(!isCommunityManager() && isFacilityManager() && isSuperAdmin()) ||
(!isCommunityManager() && isFacilityManager() && isSuperAdmin()))
}
function hasNoRole() {
return !('role' in data().keys()) || ((!isCommunityManager() && !isFacilityManager() && !isSuperAdmin()));
}
function hasNoType() {
return !('type' in data().keys()) || (!hasEmployeeType() && !isClient());
}
function hasRoleOrType() {
return (hasOneType() && hasNoRole()) || (hasOneRole() && hasNoType());
}
allow create, update:
if data().keys().hasAny(['role', 'type']) &&
data().keys().hasAll(['name', 'phone']) &&
data().name is string &&
data().address is string &&
data().phone is string &&
emailIsValid() &&
hasRoleOrType();
}
}
}