-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathfirestore.rules
More file actions
277 lines (264 loc) · 14.3 KB
/
Copy pathfirestore.rules
File metadata and controls
277 lines (264 loc) · 14.3 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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;
// Realtime CRDT updates for collaborative editing (#135). Each doc is
// a small Yjs binary update (base64) emitted by one collaborator's
// editor and consumed by all others. Anyone with read access to the
// parent project can read its updates; only collaborators + owner
// can append updates tagged with their own writer ID.
//
// Every field access on parent() is guarded with `'X' in parent()`
// for the same reason the project-doc rule above guards every
// field access on resource.data: pre-v5 project docs predate
// `commenters` and `viewers`, and `request.auth.uid in null` is
// a CEL evaluation error that fails closed but in a confusing
// way. Guarding short-circuits cleanly before any null reach.
match /updates/{updateID} {
function parent() {
return get(/databases/$(database)/documents/projects/$(project)).data;
}
allow read: if
request.auth != null && (
("mod" in request.auth.token && request.auth.token.mod) ||
('owner' in parent() && parent().owner == request.auth.uid) ||
('collaborators' in parent() && request.auth.uid in parent().collaborators) ||
('commenters' in parent() && request.auth.uid in parent().commenters) ||
('viewers' in parent() && request.auth.uid in parent().viewers)
);
allow create: if
request.auth != null && (
('owner' in parent() && parent().owner == request.auth.uid) ||
('collaborators' in parent() && request.auth.uid in parent().collaborators)
);
// Updates are immutable once written — compaction (Stage 5
// follow-up: Cloud Function) replaces them by writing a new
// snapshot to the parent doc and deleting consumed updates.
allow update: if false;
allow delete: if request.auth != null && (
('owner' in parent() && parent().owner == request.auth.uid) ||
("mod" in request.auth.token && request.auth.token.mod)
);
}
// Presence: one doc per collaborator's clientID, with their caret
// position and a heartbeat lastSeen. Readable by anyone with project
// read access; only the writer can write/delete their own doc.
match /presence/{clientID} {
function parent() {
return get(/databases/$(database)/documents/projects/$(project)).data;
}
allow read: if
request.auth != null && (
('public' in parent() && parent().public) ||
('owner' in parent() && parent().owner == request.auth.uid) ||
('collaborators' in parent() && request.auth.uid in parent().collaborators) ||
('commenters' in parent() && request.auth.uid in parent().commenters) ||
('viewers' in parent() && request.auth.uid in parent().viewers)
);
allow write: if
request.auth != null && (
('owner' in parent() && parent().owner == request.auth.uid) ||
('collaborators' in parent() && request.auth.uid in parent().collaborators)
) && 'userID' in request.resource.data
&& request.resource.data.userID == request.auth.uid;
allow delete: if
request.auth != null && (
('userID' in resource.data && resource.data.userID == request.auth.uid) ||
('owner' in parent() && parent().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 && "public" in gallery && 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 && (("curators" in gallery && uid in gallery.curators) || ("creators" in gallery && uid in gallery.creators));
}
function isRequestorCurator(uid, galleryID) {
let gallery = get(/databases/$(database)/documents/galleries/$(galleryID)).data;
return gallery != null && "curators" in gallery && 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 && (
(request.auth != 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));
}
}
}