-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfirestore.rules
More file actions
75 lines (66 loc) · 2.29 KB
/
Copy pathfirestore.rules
File metadata and controls
75 lines (66 loc) · 2.29 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
// Master admins: Only Antonio and Robert can truly delete documents
function canDelete() {
return isSignedIn()
&& 'email' in request.auth.token
&& request.auth.token.email in [
"antonioalx66@gmail.com",
"antonio@affogato.co",
"robert@affogato.co"
];
}
function isSuperAdmin() {
return canDelete() || (
isSignedIn()
&& 'roles' in request.auth.token
&& request.auth.token.roles.hasAny(["ADMIN", "OWNER", "SUPERADMIN"])
);
}
function isEmailInCoop(coopId) {
return exists(/databases/$(database)/documents/permissions/$(coopId))
&& 'user' in get(/databases/$(database)/documents/permissions/$(coopId)).data
&& request.auth.token.email in get(/databases/$(database)/documents/permissions/$(coopId)).data.user;
}
function hasCoopPermission() {
return isSignedIn()
&& 'email' in request.auth.token
&& (
isEmailInCoop("proexo") ||
isEmailInCoop("comsa") ||
isEmailInCoop("copranil") ||
isEmailInCoop("copracnil") ||
isEmailInCoop("cafepsa") ||
isEmailInCoop("commovel")
);
}
function canWrite() {
return isSuperAdmin() || hasCoopPermission();
}
// Permissions collection:
// Anyone can read
// Only Antonio & Robert can modify permissions
match /permissions/{permId} {
allow read: if true;
allow write: if canDelete();
}
// Business collections (farms, farmers, varieties, batches, certifications, companies, etc.)
// Public read for everyone (unauthenticated and authenticated)
// Create & update: SuperAdmins and authorized cooperatives (including PROEXO)
// Delete: strictly restricted to Antonio & Robert only
match /{collection}/{docId} {
allow read: if true;
allow create, update: if collection != 'permissions' && canWrite();
allow delete: if canDelete();
match /{subDoc=**} {
allow read: if true;
allow create, update: if collection != 'permissions' && canWrite();
allow delete: if canDelete();
}
}
}
}