-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirestore.rules
190 lines (162 loc) · 5.88 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /SERVERS/{serv} {
allow list: if true;
}
function allowInputs(inputs) {
return request.resource.data.keys().hasOnly(inputs);
}
function check(inputs) {
return (
request.resource.data.keys().hasOnly(inputs)
&& request.resource.data.keys().hasAll(inputs)
);
}
function same(key) {
return request.resource.data[key] == resource.data[key];
}
function getOwner(home) {
return get(/databases/$(database)/documents/homes/$(home)).data.owner
}
match /relations/{relation} {
function isOwner() {
return getOwner(relation.split('@')[0]) == request.auth.uid;
}
function isAdmin() {
return (
isOwner()
|| get(/databases/$(database)/documents/relations/$(relation.split('@')[0] + '@' + request.auth.uid)).data.isAdmin == true
);
}
function hasValidInvitation() {
let exp = get(/databases/$(database)/documents/homes/$(request.resource.data.home)/invitations/$(request.resource.data.invitation)).data.expiresOn.split('-');
return request.time.toMillis() < timestamp.date(int(exp[0]), int(exp[1]), int(exp[2])).toMillis();
}
function isAdminInHome(home) {
return (
get(/databases/$(database)/documents/homes/$(home)).data.owner == request.auth.uid
|| get(/databases/$(database)/documents/relations/$(home + '@' + request.auth.uid)).data.isAdmin == true
);
}
// It's user's relation
// or user is admin in home
allow list, get: if (
resource.data.user == request.auth.uid
|| isAdminInHome(resource.data.home)
);
allow create: if (
resource == null
&& request.resource.data.user == request.auth.uid
&& relation == request.resource.data.home + '@' + request.resource.data.user
&& (
(
isOwner()
&& check(['home', 'user', 'displayName', 'isAdmin'])
&& request.resource.data.isAdmin != false
&& request.auth.token.email_verified
) || (
hasValidInvitation()
&& check(['home', 'user', 'displayName', 'invitation'])
)
)
);
// If user is owner/admin of relation's home
allow update: if (
isAdmin()
&& allowInputs([
'home', 'user', 'displayName', 'groups',
'invitation', 'isAdmin', 'notifications'
])
&& same('home') && same('user')
&& !('invitation' in resource.data) || same('invitation')
);
// It is user's relation and user isn't owner of the home
allow delete: if request.auth.uid == relation.split('@')[1] && !isOwner();
// User is admin and deletes a user who isn't himself
allow delete: if request.auth.uid != relation.split('@')[1] && isAdmin();
// Home doesn't exists
allow delete: if !exists(/database/$(database)/documents/homes/$(relation.split('@')[0]));
}
match /homes/{home} {
function getHome() {
return get(/databases/$(database)/documents/homes/$(home)).data;
}
function relation() {
return get(/databases/$(database)/documents/relations/$(home + '@' + request.auth.uid)).data;
}
function isOwner() {
return getHome().owner == request.auth.uid;
}
function isAdmin() {
return isOwner() || relation().isAdmin == true
}
allow get: if true;
allow create: if (
resource == null
&& check(['icon', 'name', 'owner'])
&& request.auth.token.email_verified
&& request.resource.data.owner == request.auth.uid
&& home.matches('^([0-9]|[a-z])*$')
);
allow update: if (
isAdmin()
&& check(['icon', 'name', 'owner', 'server'])
&& same('owner')
);
match /groups/{group} {
allow read: if (
group in relation().groups
|| isAdmin()
);
allow write: if (
group.matches('^([0-9]|[a-z]|[A-Z])*$')
&& (
request.method == 'delete'
|| (
check(['name', 'displayName', 'pages'])
&& request.resource.data.name != 'global'
)
)
&& isAdmin()
);
}
match /pages/{page} {
allow get: if (
page in get(/databases/$(database)/documents/homes/$(home)/groups/$(relation().groups[0])).data.pages
|| page in get(/databases/$(database)/documents/homes/$(home)/groups/$(relation().groups[1])).data.pages
|| page in get(/databases/$(database)/documents/homes/$(home)/groups/$(relation().groups[2])).data.pages
);
allow list: if isAdmin();
allow write: if (
page.matches('^([0-9]|[a-z]|[A-Z])*$')
&& isAdmin()
&& (check(['icon', 'name', 'content']) || request.method == 'delete')
);
}
match /invitations/{invit} {
allow list: if isAdmin();
allow create, update: if (
invit.matches('^([0-9]|[a-z]|[A-Z])*$')
&& isAdmin()
&& check(['name', 'createdOn', 'expiresOn'])
&& (request.method != 'update' || same('createdOn'))
);
allow delete: if isAdmin();
}
match /coordinators/main {
allow get: if isAdmin();
allow write: if (
isAdmin()
&& check(['secret', 'createdDate', 'createdBy', 'lastDate'])
&& request.resource.data.createdBy == request.auth.uid
);
}
}
match /users/{userID} {
match /pushTokens/{tokenID} {
allow read, write: if request.auth.uid == userID;
}
}
}
}