-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore.rules
41 lines (35 loc) · 1.53 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function isLoggedIn() {
return request.auth != null
}
function isCalm() {
return getAfter(/databases/$(database)/documents/users/$(request.auth.uid)).data.last_operation
> get(/databases/$(database)/documents/users/$(request.auth.uid)).data.last_operation + duration.value(500, 'ms')
}
function isMine() {
return isLoggedIn() && request.auth.uid == resource.data.authorUid
}
function isAddingReaction() {
return request.resource.data.diff(resource.data).affectedKeys().hasOnly(['reactions'])
&& request.resource.data.reactions.size() == resource.data.reactions.size() + 1;
}
function isRemovingReaction() {
return request.resource.data.diff(resource.data).affectedKeys().hasOnly(['reactions'])
&& resource.data.reactions.removeAll(request.resource.data.reactions)[0].authorUid == request.auth.uid
&& request.resource.data.reactions.size() == resource.data.reactions.size() - 1;
}
// Allow public read access, but only content owners can write
match /messages/{document} {
allow read: if true
allow create: if isLoggedIn() && isCalm()
allow update: if (isAddingReaction() || isRemovingReaction() || isMine()) && isLoggedIn() && isCalm()
allow delete: if isMine() && isCalm()
}
match /users/{user} {
allow read: if request.auth.uid == resource.data.uid
allow write: if isLoggedIn()
}
}
}