-
Notifications
You must be signed in to change notification settings - Fork 1
/
firestore.rules
147 lines (119 loc) · 4.39 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
/* Tickers */
match /tickers/{ISIN} {
allow read;
allow write: if false;
}
match /tickers/{ISIN}/data/{serviceId} {
allow read;
allow write: if false;
}
match /tickers/{ISIN}/timeseries/{ts} {
allow read;
allow write: if false;
}
/* Users */
match /users/{userId} {
allow read;
allow create: if authenticated();
allow delete: if isValidUser(userId);
allow update: if isValidUser(userId);
}
match /users/{userId}/invites/{referralCode} {
allow read, update, create: if authenticated() && isOwner(userId);
allow delete: if false;
}
match /users/{userId}/stream/{token} {
allow read: if authenticated() && isOwner(userId);
allow write: if false;
}
match /usernames/{username} {
allow read;
allow create: if isValidUsername(username);
allow delete: if isValidUsername(username);
allow update: if isValidUsername(username);
}
/* Groups */
match /groups/{groupName} {
allow read, create: if authenticated();
allow update: if isGroupMember(groupName);
}
match /groups/{groupName}/investors/{username} {
allow read, update: if isGroupMember(groupName);
allow create: if isValidUser(request.auth.uid);
}
match /groups/{groupName}/trades/{tradeId} {
allow read, create: if authenticated() && isGroupMember(groupName);
allow delete: if false;
allow update: if false;
}
match /groups/{groupName}/holdings/{assetId} {
allow read: if isGroupMember(groupName);
allow write: if false;
}
match /tradesEvents/{eventId}{
allow read: if authenticated() && matchingAlpacaAccountId(resource.data.accountId);
allow write: if false;
}
match /accountsEvents/{eventId}{
allow read: if authenticated() && matchingAlpacaAccountId(resource.data.accountId);
allow write: if false;
}
match /transfersEvents/{eventId}{
allow read: if authenticated() && matchingAlpacaAccountId(resource.data.accountId);
allow write: if false;
}
match /nonTradingActivityEvents/{eventId}{
allow read: if authenticated() && matchingAlpacaAccountId(resource.data.accountId);
allow write: if false;
}
match /journalsEvents/{eventId}{
allow read, write: if false;
}
match /marketCalendar/{date} {
allow read;
allow write;
}
/* Collection Groups */
match /data/{serviceId} {
allow read;
allow update: if authenticated();
allow create: if authenticated();
}
match /funding/{fundsId} {
allow read;
allow write: if false;
}
/* Helper functions */
function isOwner(userId) {
return request.auth.uid == userId;
}
function authenticated() {
return request.auth != null;
}
function isValidUser(userId) {
let username = get(/databases/$(database)/documents/users/$(request.auth.uid)).data.username;
let createdValidUsername = existsAfter(/databases/$(database)/documents/usernames/$(username));
return isOwner(userId) && createdValidUsername;
}
function isValidUsername(username) {
let isValidLength = username.size() >= 3 && username.size() <= 15;
let isValidUserDoc = getAfter(/databases/$(database)/documents/users/$(request.auth.uid));
let sameUsername = get(/databases/$(database)/documents/users/$(request.auth.uid)).data.username == username;
return isValidLength && isValidUserDoc && sameUsername;
}
function isGroupMember(groupName) {
let username = get(/databases/$(database)/documents/users/$(request.auth.uid)).data.username;
let investorExists = exists(/databases/$(database)/documents/groups/$(groupName)/investors/$(username));
return investorExists;
}
function matchingAlpacaAccountId(accountId) {
let alpacaAccountId = get(/databases/$(database)/documents/users/$(request.auth.uid)).data.alpacaAccountId;
return alpacaAccountId == accountId;
}
}
}
}