-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirestore.rules
190 lines (177 loc) · 6.42 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
service cloud.firestore {
match /databases/{database}/documents {
match /items/{id} {
allow get: if resource == null || isUser(getDoc().createdBy);
allow list: if isUser(getDoc().createdBy);
allow create: if isItem(getDocInc(), id);
allow update: if isItem(getDocInc(), id);
allow delete: if isUser(getDoc().createdBy);
}
match /tags/{id} {
allow get: if resource == null || isUser(getDoc().createdBy);
allow list: if isUser(getDoc().createdBy);
allow create: if isTag(getDocInc(), id);
allow update: if isTag(getDocInc(), id);
allow delete: if false;
}
match /notifications/{id} {
allow get: if resource == null || isUser(getDoc().userId);
allow list: if isUser(getDoc().userId);
allow create: if isUser(getDocInc().userId);
allow update: if isNotification(getDocInc(), id);
allow delete: if isUser(getDoc().userId);
}
match /roadmapBricks/{id} {
allow get: if resource == null || isAuthenticated();
allow list: if isAuthenticated();
allow create: if isRoadmapBrick(getDocInc(), id);
allow update: if isRoadmapBrick(getDocInc(), id);
allow delete: if canDeleteRoadmapBrick(getDoc());
}
match /users/{id} {
allow get: if resource == null || isUser(id);
allow list: if false;
allow create: if false;
allow update: if isUserDoc(getDocInc(), id);
allow delete: if false;
}
match /counterItems/{id} {
allow get: if resource == null || isUser(id);
}
match /counterNewFinished/{id} {
allow get: if resource == null || isUser(getDoc().userId);
}
}
}
function isItem(doc, id) {
return
// doc.size() == 7
// && doc.url is string && doc.url.size() > 0
// && ((doc.title is string && doc.title.size() > 0) || doc.title == null)
// && ((doc.description is string && doc.description.size() > 0) || doc.description == null)
// && doc.type is string && doc.type in ["video", "article", "profile", "website"]
// && doc.status is string && doc.status in ["new", "inProgress", "done"]
// &&
isUser(doc.createdBy)
// && doc.createdAt is timestamp
}
function isTag(doc, id) {
return
// doc.size() == 7
// && doc.url is string && doc.url.size() > 0
// && ((doc.title is string && doc.title.size() > 0) || doc.title == null)
// && ((doc.description is string && doc.description.size() > 0) || doc.description == null)
// && doc.type is string && doc.type in ["video", "article", "profile", "website"]
// && doc.status is string && doc.status in ["new", "inProgress", "done"]
// &&
isUser(doc.createdBy)
// && doc.createdAt is timestamp
}
function isNotification(doc, id) {
return
// doc.size() == 7
// && doc.url is string && doc.url.size() > 0
// && ((doc.title is string && doc.title.size() > 0) || doc.title == null)
// && ((doc.description is string && doc.description.size() > 0) || doc.description == null)
// && doc.type is string && doc.type in ["video", "article", "profile", "website"]
// && doc.status is string && doc.status in ["new", "inProgress", "done"]
// &&
isUser(doc.userId)
// && doc.createdAt is timestamp
}
function isRoadmapBrick(doc, id) {
return true;
// doc.size() == 7
// && doc.url is string && doc.url.size() > 0
// && ((doc.title is string && doc.title.size() > 0) || doc.title == null)
// && ((doc.description is string && doc.description.size() > 0) || doc.description == null)
// && doc.type is string && doc.type in ["video", "article", "profile", "website"]
// && doc.status is string && doc.status in ["new", "inProgress", "done"]
// &&
// isUser(doc.createdBy)
// && doc.createdAt is timestamp
}
function isUserDoc(doc, id) {
return
// doc.size() == 7
// && doc.url is string && doc.url.size() > 0
// && ((doc.title is string && doc.title.size() > 0) || doc.title == null)
// && ((doc.description is string && doc.description.size() > 0) || doc.description == null)
// && doc.type is string && doc.type in ["video", "article", "profile", "website"]
// && doc.status is string && doc.status in ["new", "inProgress", "done"]
// &&
isUser(id)
// && doc.createdAt is timestamp
}
function canDeleteRoadmapBrick(doc) {
return isAdmin() || (isUser(doc.createdBy) && doc.type != "feature" && doc.status == "new");
}
function isAdmin() {
return getAuth().uid == "carcBWjBqlNUY9V2ekGQAZdwlTf2"; // Anton's id
}
// RESOURCE DATA ACCESS - START
// Returns existing document data
function getDoc(){
return resource.data
}
// Returns incoming document data (for create & update operations)
function getDocInc(){
return request.resource.data
}
// RESOURCE DATA ACCESS - END
// AUTH - START
function getAuth() {
return request.auth
}
function isAuthenticated() {
return getAuth() != null;
}
function isUser(userId) {
return getAuth().uid == userId;
}
// AUTH - END
// USER ACCESS FUNCTIONS - START
function getUserRoleInOrganization(organizationId){
return frGet(/organizations/$(organizationId)).roles[getAuth().uid];
}
function getUserRoleInLocation(locationId){
return frGet(/locations/$(locationId)).roles[getAuth().uid];
}
function isUserInOrganization(organizationId) {
return getUserRoleInOrganization(organizationId) in ['powerUser', 'user'];
}
function isUserInLocation(locationId) {
return getUserRoleInLocation(locationId) in ['powerUser', 'user'];
}
function doesUserHasRoleInOrganization(organizationId, role) {
return getUserRoleInOrganization(organizationId) == role;
}
function doesUserHasRoleInLocation(locationId, role) {
return getUserRoleInLocation(locationId) == role;
}
function doesUserHasAccess(organizationId, locationId) {
return isUserInOrganization(organizationId) && isUserInLocation(locationId)
}
// USER ACCESS FUNCTIONS - END
// LIST PARAMS - START
function reqLimitForList() {
return request.query.limit <= 20
}
// LIST PARAMS - END
// HELPERS - START
function frGet(path) {
return get(/databases/$(database)/documents/$(path)).data;
}
function frGetAfter(path) {
return getAFter(/databases/$(database)/documents/$(path)).data;
}
function frExists(path) {
return exists(/databases/$(database)/documents/$(path));
}
function isCreatedAtValid (entity) {
return entity.createdAt is timestamp && entity.createdAt <= request.time;
}
function isCreatedByValid (entity) {
return entity.createdBy is string && entity.createdBy == getAuth().uid;
}
// HELPERS - END