-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.rules
63 lines (49 loc) · 2.04 KB
/
firebase.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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /threads/{threadID} {
allow read;
allow update: if resource.data.ownerUID == request.auth.uid && diffKeys().hasOnly(["description"]) && checkContentField("description", 10000);
match /posts/{postID} {
allow read;
match /comments/{commentID} {
allow read;
}
}
}
match /users/{userID} {
allow read;
allow create: if canCreateUser(userID);
allow update: if diffKeys().hasOnly(["description"]) && hasValidUserData(userID);
match /votes/{voteID} {
allow read: if request.auth.uid == userID;
}
}
match /{path=**}/posts/{postID} {
allow read;
}
function diffKeys() {
return request.resource.data.diff(resource.data).affectedKeys();
}
function checkContentField(field, maxLength) {
return request.resource.data[field] is string && request.resource.data[field].size() < maxLength;
}
function isNow() {
return request.time == request.resource.data.createdAt;
}
function hasValidUserData(userID) {
let isOwner = request.auth.uid == userID;
let onlyContains = request.resource.data.keys().hasOnly(["createdAt", "name", "description"]);
return onlyContains && isOwner && checkContentField("description", 10000);
}
function canCreateUser(userID) {
return isNow() && hasValidUserData(userID) && isValidUsername(request.resource.data.name);
}
function isValidUsername(value) {
let username = value.lower();
let isValidLength = username.size() >= 3 && username.size() <= 24;
let hasValidChars = !username.matches("[^\\w-]");
return username is string && isValidLength && hasValidChars;
}
}
}