This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
firestore.rules
66 lines (53 loc) · 1.74 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
rules_version = '2';
// Prevent a field from being updated with a new value
function notUpdating(field) {
return !(field in request.resource.data) || resource.data[field] == request.resource.data[field];
}
// [WRITE] Data that is sent to a Firestore document
// e.g incomingData().userId
// e.g isUser(incomingData().userId)
function incomingData() {
return request.resource.data;
}
// [READ] Data that exists on the Firestore document
function existingData() {
return resource.data;
}
function userExists() {
return exists(/databases/$(database)/documents/users/$(request.auth.uid));
}
function isAuth() {
return request.auth != null && request.auth.uid != null;
}
function isUser(uid) {
return request.auth.uid == uid;
}
function isAdmin() {
return isAuth() && request.auth.token.admin == true;
}
function isSuperAdmin() {
return isAuth() && request.auth.token.super == true;
}
function isKycAuditor() {
return isAuth() && request.auth.token.kycAuditor == true;
}
function userRoles(userId) {
return get(/databases/$(database)/documents/users/$(userId)).data.roles;
}
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read: if isUser(userId) || isAdmin() || isSuperAdmin();
allow write: if isUser(userId) && notUpdating('admin') && notUpdating('super') && notUpdating('kycAuditor') || isAdmin() || isSuperAdmin();
allow create: if isAuth();
}
match /admin/{database} {
allow read: if true;
allow update: if isAdmin() || isSuperAdmin();
allow delete, create: if isSuperAdmin();
}
match /audit/{database} {
allow read: if isSuperAdmin();
}
}
}