-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfirestore.rules
112 lines (89 loc) · 3.38 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
service cloud.firestore {
match /databases/{database}/documents {
// ##########################
// ##### GENERAL UTILS ######
// return current user object
function currentUser() {
return request.auth;
}
// get requesting users data
function getUserData() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
}
// incoming data object
function incomingData() {
return request.resource.data;
}
// ##### GENERAL UTILS ######
// ##########################
// check if user is signed in
function signedIn() {
return currentUser() != null;
}
// check if user has signed campaign
function hasNotSignedCampaign() {
return getUserData().signedCampaignId == null;
}
// check if user exists
function userExists(userId) {
return exists(/databases/$(database)/documents/users/$(userId));
}
// check if email exists in users collection
function emailExists() {
return getUserData().email == incomingData().email;
}
// check if user is admin of requested campaign
function userIsAdminOfRequestedCampaign() {
return getUserData().createdCampaignId == incomingData().signedCampaignId;
}
// check if request is made by campaign owner
// need more robust rules here
function isCampaignOwner(campaignId) {
return getUserData().createdCampaignId == campaignId;
}
// check if it's the user's own data
function isUsersData(userId) {
return currentUser().uid == userId;
}
// ###########################
// ##### DOCUMENT RULES ######
match /users/{userId} {
// User can read, update, or delete their own data
// Signature user data is stored in campaigns/signatures
allow create: if signedIn()
&& !userExists(userId)
&& isUsersData(userId) || signedIn()
&& !emailExists()
&& userIsAdminOfRequestedCampaign();
allow read, update, delete: if signedIn() && isUsersData(userId);
}
match /campaigns/{campaignId} {
// Anyone can read campaigns, including unauthenticated users
allow read;
// Only authenticated users can create a campaign and only one campaign
allow create: if signedIn()
&& getUserData().createdCampaignId == null;
// Only authenticated users and campaign owner can update or delete campaign
allow update, delete: if signedIn()
&& isCampaignOwner(campaignId);
match /signatures/{signatureId} {
// Anyone can read signatures, including unauthenticated users
allow read;
// Only authenticated users can sign a campaign and only one campaign
allow create: if signedIn() && hasNotSignedCampaign();
// Only authenticated users can update or delete their own signature
allow update, delete: if signedIn() && isUsersData(signatureId);
}
}
match /wasteProviders/{wasteProvider} {
//Anyone can read waste providers, including unauthenticated users
allow read;
// Maybe add 'appAdmin' boolean field to user?
// allow write: if getUserData().appAdmin == true;
// No one can write waste provider data at this time
allow write: if false;
}
// ##### DOCUMENT RULES ######
// ###########################
}
}