This repository was archived by the owner on Sep 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
48 lines (39 loc) · 1.56 KB
/
firestore.rules
File metadata and controls
48 lines (39 loc) · 1.56 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /email-subscriptions/{documents=**} {
allow read, write: if true;
}
match /{document=**} {
allow read, write: if request.auth != null;
}
match /users/{id} {
allow read, delete: if request.auth != null && request.auth.uid == id;
allow create: if request.auth != null;
allow update: if request.auth != null &&
(request.auth.uid == id ||
request.resource.data.diff(resource.data).affectedKeys().hasOnly(['organizations']) &&
isOrgRemovalFromOrgOwner());
}
match /organizations/{orgId} {
allow delete: if request.auth != null && get(resource.data.owner).id == request.auth.uid;
allow update: if request.auth != null &&
(resource.data.owner.id == request.auth.uid || isLeavingOrganization());
}
function isLeavingOrganization() {
return request.resource.data.diff(resource.data).affectedKeys().hasOnly(['members']) &&
request.resource.data.members.hasOnly([userDocRefFromAuth()]);
}
function isOrgRemovalFromOrgOwner() {
let removedOrganizations = resource.data.organizations.removeAll(request.resource.data.organizations);
return removedOrganizations.size() == 1 &&
get(removedOrganizations[0]).owner == userDocRefFromAuth();
}
function userDocRefFromAuth() {
return '/users/' + request.auth.uid;
}
function organizationDocRef() {
return '/organizations/' + resource.data.organizations.id;
}
}
}