-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
128 lines (101 loc) · 5.18 KB
/
Copy pathfirestore.rules
File metadata and controls
128 lines (101 loc) · 5.18 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// ─── Helpers ────────────────────────────────────────────────────────────
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function getUserData() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data;
}
function isAdmin() {
return isAuthenticated() && getUserData().role == 'ADMIN';
}
function isClient() {
return isAuthenticated() && getUserData().role == 'CLIENT';
}
// ─── users/{uid} ────────────────────────────────────────────────────────
// El propio usuario puede leer/actualizar su perfil.
// Solo el admin puede leer todos los perfiles.
// La creación la hace el propio usuario al registrarse (Google SSO).
match /users/{userId} {
allow read: if isOwner(userId) || isAdmin();
allow create: if isOwner(userId)
&& request.resource.data.keys().hasAll(['firstName', 'lastName', 'phone', 'profilePhoto', 'role', 'address', 'createdAt'])
&& request.resource.data.role == 'CLIENT'; // nuevo usuario siempre CLIENT
allow update: if isOwner(userId) || isAdmin();
allow delete: if isAdmin();
}
// ─── items/{itemId} ─────────────────────────────────────────────────────
// Cualquier usuario autenticado puede ver el menú.
// Solo el admin puede crear/editar/borrar ítems.
match /items/{itemId} {
allow read: if isAuthenticated();
allow create,
update,
delete: if isAdmin();
}
// ─── orders/{orderId} ───────────────────────────────────────────────────
// Cliente: puede crear órdenes propias y cancelarlas mientras estén PENDING.
// Admin: puede leer todas y cambiar cualquier estado.
match /orders/{orderId} {
allow read: if isAuthenticated()
&& (resource.data.clientId == request.auth.uid || isAdmin());
allow create: if isClient()
&& request.resource.data.clientId == request.auth.uid
&& request.resource.data.status == 'PENDING';
allow update: if isAdmin()
|| (
isClient()
&& resource.data.clientId == request.auth.uid
&& resource.data.status == 'PENDING'
&& request.resource.data.status == 'CANCELLED'
);
allow delete: if isAdmin();
}
// ─── orderDetail/{detailId} ─────────────────────────────────────────────
// Los detalles siguen los permisos de su orden padre.
// Cliente puede actualizar el rating una vez entregado el pedido.
match /orderDetail/{detailId} {
allow read: if isAuthenticated()
&& (
get(/databases/$(database)/documents/orders/$(resource.data.orderId)).data.clientId
== request.auth.uid
|| isAdmin()
);
allow create: if isClient();
allow update: if isAdmin()
|| (
isClient()
&& get(/databases/$(database)/documents/orders/$(resource.data.orderId)).data.clientId
== request.auth.uid
&& get(/databases/$(database)/documents/orders/$(resource.data.orderId)).data.status
== 'DELIVERED'
);
allow delete: if isAdmin();
}
// ─── cartItem/{cartItemId} ──────────────────────────────────────────────
// Cada cliente gestiona solo sus propios ítems del carrito.
match /cartItem/{cartItemId} {
allow read: if isAuthenticated()
&& resource.data.clientId == request.auth.uid;
allow create: if isClient()
&& request.resource.data.clientId == request.auth.uid;
allow update,
delete: if isAuthenticated()
&& resource.data.clientId == request.auth.uid;
}
// ─── storage/{path} ─────────────────────────────────────────────────────
// Metadatos de imágenes subidas a Firebase Storage.
// Solo el admin escribe; cualquier autenticado puede leer la URL.
match /storage/{path} {
allow read: if isAuthenticated();
allow create,
update,
delete: if isAdmin();
}
}
}