-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
60 lines (49 loc) · 2.31 KB
/
firestore.rules
File metadata and controls
60 lines (49 loc) · 2.31 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Helper function to check if the user is authenticated
function isAuthenticated() {
return request.auth != null;
}
// Helper function to check if the user is the owner of the document
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
// Rules for farmer listings collection
match /farmerListings/{listingId} {
// Allow read if the user is authenticated
// This enables customers to view available listings
allow read: if isAuthenticated();
// Allow create if the user is authenticated and sets themselves as the farmer
allow create: if isAuthenticated() &&
request.resource.data.farmerId == request.auth.uid;
// Allow update if the user is the owner (farmer) or updating specific fields as a customer
allow update: if isOwner(resource.data.farmerId) ||
(isAuthenticated() &&
request.resource.data.diff(resource.data).affectedKeys()
.hasOnly(['status', 'buyer', 'revenue']));
// Allow delete only if the user is the owner
allow delete: if isOwner(resource.data.farmerId);
}
// Rules for mill products collection
match /millProducts/{productId} {
// Allow read if the user is authenticated
// This enables customers to view available products
allow read: if isAuthenticated();
// Allow create if the user is authenticated and sets themselves as the producer
allow create: if isAuthenticated() &&
request.resource.data.producerId == request.auth.uid;
// Allow update if the user is the owner (producer) or updating specific fields as a customer
allow update: if isOwner(resource.data.producerId) ||
(isAuthenticated() &&
request.resource.data.diff(resource.data).affectedKeys()
.hasOnly(['status', 'quantity']));
// Allow delete only if the user is the owner
allow delete: if isOwner(resource.data.producerId);
}
// Default deny for all other collections
match /{document=**} {
allow read, write: if false;
}
}
}