-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
63 lines (56 loc) · 2.2 KB
/
firestore.rules
File metadata and controls
63 lines (56 loc) · 2.2 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Users can read and write their own profile
match /users/{userId} {
allow read, write: if request.auth != null && request.auth.uid == userId;
// Allow admins to read all user profiles
allow read: if request.auth != null &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
// Vendors collection - public read, authenticated write
match /vendors/{vendorId} {
allow read: if true;
allow create: if request.auth != null;
allow update, delete: if request.auth != null &&
(request.auth.uid == resource.data.userId ||
isAdmin());
// Vendor products - public read, vendor/admin write
match /products/{productId} {
allow read: if true;
allow create: if request.auth != null;
allow update, delete: if request.auth != null &&
(request.auth.uid == get(/databases/$(database)/documents/vendors/$(vendorId)).data.userId ||
isAdmin());
}
}
// Global products collection (for easier querying) - public read
match /products/{productId} {
allow read: if true;
allow write: if request.auth != null;
}
// Hero slides - public read, admin write
match /hero-slides/{slideId} {
allow read: if true;
allow write: if request.auth != null && isAdmin();
}
// Featured products - public read, admin write
match /featured-products/{productId} {
allow read: if true;
allow write: if request.auth != null && isAdmin();
}
// Orders - users can read/write their own orders
match /orders/{orderId} {
allow read, write: if request.auth != null &&
(request.auth.uid == resource.data.customerId ||
request.auth.uid == resource.data.vendorId ||
isAdmin());
}
// Helper function to check if user is admin
function isAdmin() {
return request.auth != null &&
exists(/databases/$(database)/documents/users/$(request.auth.uid)) &&
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
}
}
}