-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore.rules
46 lines (38 loc) · 1.47 KB
/
firestore.rules
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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Verifies if the field 'key' is the same before and after the request
function isUnchanged(key) {
return (key in request.resource.data)
&& (key in resource.data)
&& (resource.data[key] == request.resource.data[key]);
}
// Verifies if the user is authenticated
function isUserAuthenticated() {
return request.auth != null;
}
// Restaurants collection
match /restaurants/{restaurantId} {
// Any authenticated user can read
allow read: if isUserAuthenticated();
// Any authenticated user can create
allow create: if isUserAuthenticated();
// Any authenticated user can update only if no new fields are added and 'name' is unchanged
allow update: if isUserAuthenticated()
&& (request.resource.data.keys() == resource.data.keys())
&& isUnchanged("name");
// Deletes are not allowed
allow delete: if false;
// Ratings subcollection
match /ratings/{ratingId} {
// Any authenticated user can read
allow read: if isUserAuthenticated();
// Any authenticated user can create if 'userId' matches their uid
allow create: if isUserAuthenticated()
&& request.resource.data.userId == request.auth.uid;
// Deletes and updates are not allowed
allow update, delete: if false;
}
}
}
}