-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathfirestore.rules
More file actions
210 lines (199 loc) · 10.7 KB
/
firestore.rules
File metadata and controls
210 lines (199 loc) · 10.7 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /projects/{project} {
function isInGalleryNotRestricted(uid, galleryID) {
let gallery = get(/databases/$(database)/documents/galleries/$(galleryID)).data;
return gallery != null && (gallery.public || uid in gallery.creators || uid in gallery.curators) &&
(!("restrictedGallery" in resource.data) || !resource.data.restrictedGallery);
}
function isInGalleryRestricted(uid, galleryID) {
let gallery = get(/databases/$(database)/documents/galleries/$(galleryID)).data;
return gallery != null && "restrictedGallery" in resource.data && resource.data.restrictedGallery && uid in gallery.curators;
}
function isCuratorOfProjectInGallery(uid, galleryID) {
let gallery = get(/databases/$(database)/documents/galleries/$(galleryID)).data;
return gallery != null && (uid in gallery.curators);
}
allow create: if request.auth != null;
// Projects are readable if:
// 1) They are public
// 2) The requestor is the owner or a collaborator
// 3) The requestor is a curator or creator of the gallery that the project is in, or that gallery is public, and access to the project has not been restricted by its owner
// 4) The requestor is a curator of the gallery that the project is in and access to the project is restricted
allow read: if
(resource.data != null && 'public' in resource.data && resource.data.public) ||
(resource.data != null && request.auth != null &&
(
("mod" in request.auth.token && request.auth.token.mod) ||
('owner' in resource.data && resource.data.owner == request.auth.uid) ||
('collaborators' in resource.data && request.auth.uid in resource.data.collaborators) ||
('commenters' in resource.data && request.auth.uid in resource.data.commenters) ||
('viewers' in resource.data && request.auth.uid in resource.data.viewers) ||
('gallery' in resource.data && resource.data.gallery != null && (isInGalleryRestricted(request.auth.uid, resource.data.gallery) || isInGalleryNotRestricted(request.auth.uid, resource.data.gallery)))
)
);
allow update:
if request.auth != null &&
(
resource.data.owner == request.auth.uid ||
request.auth.uid in resource.data.collaborators ||
("mod" in request.auth.token && request.auth.token.mod) ||
(resource.data.gallery != null && isCuratorOfProjectInGallery(request.auth.uid, resource.data.gallery))
);
allow delete: if request.auth != null && resource.data.owner == request.auth.uid;
}
match /creators/{uid} {
allow read, write: if request.auth != null && request.auth.uid == uid;
}
match /galleries/{gallery} {
allow create: if request.auth != null;
// Public galleries can be read, as can private ones if the creator is a curator, creator, or a how to viewer.
allow read: if (resource.data != null && "public" in resource.data && resource.data.public) || (
request.auth != null && (
("curators" in resource.data && request.auth.uid in resource.data.curators) ||
("creators" in resource.data && request.auth.uid in resource.data.creators) ||
(
"howToExpandedVisibility" in resource.data &&
"howToViewersFlat" in resource.data &&
resource.data.howToExpandedVisibility &&
request.auth.uid in resource.data.howToViewersFlat
)
)
);
allow update:
if request.auth != null &&
// Curators can update anything
// Creators can't update public status or creator or curator list.
(
request.auth.uid in resource.data.curators ||
(
request.auth.uid in resource.data.creators &&
request.resource.data.public == resource.data.public &&
request.resource.data.curators == resource.data.curators &&
request.resource.data.creators == resource.data.creators
)
);
allow delete: if request.auth != null && request.auth.uid in resource.data.curators;
}
match /chats/{chat} {
// Anyone logged in can create chats
allow create: if request.auth != null;
// Participants can read, update, and delete chats
allow read, update, delete: if request.auth != null && (resource == null || (resource.data != null && request.auth.uid in resource.data.participants));
}
match /classes/{class} {
function isTeacher() {
return "teacher" in request.auth.token && request.auth.token.teacher;
}
// Only teachers can create classes
allow create: if request.auth != null && isTeacher();
// Students and teachers can read classes
allow get: if request.auth != null && resource.data != null && (
("learners" in resource.data && request.auth.uid in resource.data.learners) ||
("teachers" in resource.data && request.auth.uid in resource.data.teachers)
);
allow list: if request.auth != null;
// Only teachers of a class can update and delete classes
allow update, delete: if request.auth != null && isTeacher() && (resource.data != null && request.auth.uid in resource.data.teachers);
}
match /characters/{character} {
// Anyone logged in can create characters
allow create: if request.auth != null;
// Allowed if it doesn't exist or this is the owner asking, Owners or ownerless characters can be read
allow read:
if resource == null ||
("public" in resource.data && resource.data.public) ||
(request.auth != null &&
(
("owner" in resource.data && request.auth.uid == resource.data.owner) ||
("collaborators" in resource.data && request.auth.uid in resource.data.collaborators)
)
);
// Only owners can update or delete.
allow update: if request.auth.uid == resource.data.owner || ("collaborators" in resource.data && request.auth.uid in resource.data.collaborators);
allow delete: if request.auth.uid == resource.data.owner;
}
match /feedback/{id} {
// Anyone can see feedback
allow read: if true;
// Anyone logged in can create feedback
allow create: if request.auth != null;
// Only the creator can read, update, or delete their feedback; anyone can vote on the feedback though
allow update, delete: if request.auth != null && (request.auth.uid == resource.data.creator || ("mod" in request.auth.token && request.auth.token.mod) || request.resource.data.diff(resource.data).affectedKeys().hasOnly(["votes"]));
}
match /howtos/{id} {
function isGalleryPublic(galleryID) {
let gallery = get(/databases/$(database)/documents/galleries/$(galleryID)).data;
return gallery != null && gallery.public;
}
function isRequestorOwnerCollaborator(uid) {
return resource.data != null && (("creator" in resource.data && uid == resource.data.creator) || ("collaborators" in resource.data && uid in resource.data.collaborators));
}
function isRequestorCuratorCollaborator(uid, galleryID) {
let gallery = get(/databases/$(database)/documents/galleries/$(galleryID)).data;
return gallery != null && (uid in gallery.curators || uid in gallery.creators);
}
function isRequestorCurator(uid, galleryID) {
let gallery = get(/databases/$(database)/documents/galleries/$(galleryID)).data;
return gallery != null && (uid in gallery.curators);
}
function isRequestorViewer(uid) {
return
"scopeOverwrite" in resource.data &&
!resource.data.scopeOverwrite &&
"viewersFlat" in resource.data &&
uid in resource.data.viewersFlat;
}
allow create: if request.auth != null && "galleryId" in request.resource.data && isRequestorCuratorCollaborator(request.auth.uid, request.resource.data.galleryId);
// Published how-tos are readable if:
// 1) the gallery the how-to is in is public
// 2) the requestor is the owner or a collaborator
// 3) the requestor is a curator or collaborator of the gallery the how-to is in
// 4) the requestor is a viewer of the gallery the how-to is in
// Unpublished how-tos (drafts) are only readable by the creator and collaborators
allow read:
if resource.data != null && (
("mod" in request.auth.token && request.auth.token.mod) ||
('isPublic' in resource.data && resource.data.isPublic) ||
isRequestorOwnerCollaborator(request.auth.uid) ||
('galleryId' in resource.data && isGalleryPublic(resource.data.galleryId)) ||
("published" in resource.data && resource.data.published && (
('galleryId' in resource.data && isRequestorCuratorCollaborator(request.auth.uid, resource.data.galleryId)) ||
isRequestorViewer(request.auth.uid)
))
);
allow update:
// How-tos can be updated if:
// 0) a moderator
// 1) owner or collaborator
// 2) curator of the gallery the how-to is in
// we also allow updating the social interactions of a how-to as long as the user can view it (viewer, owner, collaborator, gallery curator or collaborator)
// and updating xcoord, ycoord by how-to owner/collaborator and gallery curator/collaborator
if request.auth != null &&
(
("mod" in request.auth.token && request.auth.token.mod) ||
isRequestorOwnerCollaborator(request.auth.uid) ||
("galleryId" in request.resource.data && isRequestorCurator(request.auth.uid, request.resource.data.galleryId)) ||
('galleryId' in request.resource.data &&
(
(
isRequestorCuratorCollaborator(request.auth.uid, request.resource.data.galleryId) ||
isRequestorOwnerCollaborator(request.auth.uid)
) &&
request.resource.data.diff(resource.data).affectedKeys().hasOnly(["xcoord", "ycoord"])
) ||
(
request.resource.data.diff(resource.data).affectedKeys().hasOnly(["social"]) &&
(
isRequestorViewer(request.auth.uid) ||
isRequestorCuratorCollaborator(request.auth.uid, request.resource.data.galleryId) ||
isRequestorOwnerCollaborator(request.auth.uid)
)
)
)
);
allow delete: if request.auth != null && (resource.data.creator == request.auth.uid || isRequestorCurator(request.auth.uid, resource.data.galleryId));
}
}
}