-
Notifications
You must be signed in to change notification settings - Fork 217
/
firestore.rules
96 lines (70 loc) · 3.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
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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
// Required for collection group queries
match /posts/{postId} {
allow read;
}
match /users/{userId} {
allow read;
allow create: if isValidUser(userId);
}
match /usernames/{username} {
allow read;
allow create: if isValidUsername(username);
}
match /users/{userId}/posts/{postId} {
allow read;
allow create: if canCreatePost(userId);
allow update: if canUpdatePost(userId) || canIncrementHearts(userId, postId);
allow delete: if request.auth.uid == userId;
}
match /users/{userId}/posts/{postId}/hearts/{heartId} {
allow read;
allow write: if request.auth.uid == heartId;
}
// Users
function isValidUser(userId) {
let isOwner = request.auth.uid == userId;
let username = request.resource.data.username;
let createdValidUsername = existsAfter(/databases/$(database)/documents/usernames/$(username));
return isOwner && createdValidUsername;
}
function isValidUsername(username) {
let isOwner = request.auth.uid == request.resource.data.uid;
let isValidLength = username.size() >= 3 && username.size() <= 15;
let isValidUserDoc = getAfter(/databases/$(database)/documents/users/$(request.auth.uid)).data.username == username;
return isOwner && isValidLength && isValidUserDoc;
}
// Posts
function canCreatePost(userId) {
let isOwner = request.auth.uid == userId;
let isNow = request.time == request.resource.data.createdAt;
let isValidContent = request.resource.data.content.size() < 20000 && request.resource.data.heartCount == 0;
let username = get(/databases/$(database)/documents/users/$(request.auth.uid)).data.username;
let usernameMatches = username == request.resource.data.username;
return isOwner && isNow && isValidContent && usernameMatches;
}
function canUpdatePost(userId) {
let isOwner = request.auth.uid == userId;
let isNow = request.time == request.resource.data.updatedAt;
let isValidContent = request.resource.data.content.size() < 20000;
let doesNotUpdateForbidden = !request.resource.data.diff(resource.data).affectedKeys().hasAny(['uid', 'username', 'heartCount']);
return isOwner && isNow && isValidContent && doesNotUpdateForbidden;
}
// Hearts
function canIncrementHearts(userId, postId) {
let hasValidFields = request.resource.data.diff(resource.data).affectedKeys().hasOnly(['heartCount']);
let currentUser = request.auth.uid;
let heartDocExistsAfter = existsAfter(/databases/$(database)/documents/users/$(userId)/posts/$(postId)/hearts/$(currentUser));
let heartDocExists = exists(/databases/$(database)/documents/users/$(userId)/posts/$(postId)/hearts/$(currentUser));
let heartDocAdded= !heartDocExists && heartDocExistsAfter;
let heartDocRemoved = heartDocExists && !heartDocExistsAfter;
let countChange = request.resource.data.heartCount - resource.data.heartCount;
let validChange = countChange == 1 || countChange == -1;
return hasValidFields && validChange && ( countChange == 1 ? heartDocAdded : heartDocRemoved );
}
}
}
}