-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore.rules
81 lines (67 loc) · 3.17 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function requireFields(object, required) {
return object.keys().hasAll(required) && object.keys().hasOnly(required);
}
match /users/{userId} {
allow get: if request.auth != null && request.auth.uid == userId;
function isAuthorized() {
return request.auth.uid != null
&& request.auth.uid == userId;
}
function isChoosingFiatCurrency() {
return isAuthorized()
&& requireFields(request.resource.data, ['fiat_currency_symbol'])
&& request.resource.data.fiat_currency_symbol in get(/databases/$(database)/documents/system/fiat).data.currencies.keys();
}
function isChangingFiatCurrency() {
return isAuthorized()
&& request.resource.data.diff(resource.data).affectedKeys().hasOnly(['fiat_currency_symbol'])
&& request.resource.data.fiat_currency_symbol in get(/databases/$(database)/documents/system/fiat).data.currencies.keys();
}
function hasCompletedGuide() {
return isAuthorized()
&& request.resource.data.diff(resource.data).addedKeys().hasAll(['has_completed_guide'])
&& request.resource.data.diff(resource.data).addedKeys().hasOnly(['has_completed_guide'])
&& request.resource.data.has_completed_guide is bool
&& request.resource.data.has_completed_guide;
}
function isEditingFavourites() {
return isAuthorized()
&& request.resource.data.diff(resource.data).affectedKeys().hasOnly(['favourite_currency_ids']);
}
allow create: if isChoosingFiatCurrency();
allow update: if isChangingFiatCurrency() || isEditingFavourites() || hasCompletedGuide();
match /portfolio/{cryptoId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
match /buy_records/{recordId} {
allow read: if request.auth != null && request.auth.uid == userId;
function isAddingNewRecord() {
return isAuthorized()
&& requireFields(request.resource.data, ['buy_price', 'amount', 'timestamp', 'fiat_currency_symbol'])
&& request.resource.data.buy_price is float
&& request.resource.data.amount is float
&& request.resource.data.timestamp == request.time
&& request.resource.data.fiat_currency_symbol in get(/databases/$(database)/documents/system/fiat).data.currencies.keys();
}
function isEdittingExistingRecord() {
return isAuthorized()
&& request.resource.data.diff(resource.data).affectedKeys().hasOnly(['buy_price', 'amount'])
&& request.resource.data.buy_price is float
&& request.resource.data.amount is float;
}
allow create: if isAddingNewRecord();
allow update: if isEdittingExistingRecord();
allow delete: if request.auth != null && request.auth.uid == userId;
}
}
}
match /system/fiat {
allow get: if request.auth != null;
}
match /system/crypto {
allow get: if request.auth != null;
}
}
}