Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
76 lines (72 loc) · 2.87 KB
/
Copy pathfirestore.rules
File metadata and controls
76 lines (72 loc) · 2.87 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
rules_version = '2';
service cloud.firestore {
match /databases/(default)/documents/book_data/{multiSegment=**} {
allow read;
allow list;
allow update: if isSignedIn() && isAdmin(request.auth.uid);
allow create: if isSignedIn() && isAdmin(request.auth.uid);
}
// Rule 1: Users cannot write or read from any other table
match /databases/(default)/documents/page_comments/{comment} {
// Rule 2: User can Read all comments even if not logged in
allow read;
allow list;
// Rule 3: User can Write comments only if they are logged in and if their write
// is attributed TO the user that is logged in
// Rule 4: User can only edit comments they wrote if they are logged in
// Rule 6: Users can only delete their own comments
allow update: if isSignedIn()
&& (
isUser(resource.data.author_uid)
||
addingChildComment()
)
&& commentNotTooLong(); //gotta figure out how to allow users who comment on a comment to update that comment's "is_parent" field
allow create: if isSignedIn() && commentNotTooLong();
allow delete: if isSignedIn() && isUser(resource.data.author_uid)
// TODO Rule 5: Users cannot change their own comment like value but can only increment others if they haven't already liked their own
// allow write: if isSignedIn()
// && (
// (isUser(resource.data.author_uid) && notChangingLikeValue())
// ||
// (onlyIncrementingLikeValue() && hasntLikedYet())
// );
}
match /databases/(default)/documents/users/{userId} {
allow create: if isSignedIn()
&& validDisplayName();
// && notChangingCommentsLiked(); For when adding likes
allow update: if isSignedIn()
&& isUser(userId)
&& validDisplayName();
// && notChangingCommentsLiked(); For when adding likes
allow delete: if isSignedIn()
&& isUser(resource.id);
allow read;
allow list;
}
function validDisplayName() {
return validDisplayNameLength() && validDisplayNameContent();
}
function validDisplayNameLength() {
return request.resource.data.display_name.size() > 0 && request.resource.data.display_name.size() < 21;
}
function validDisplayNameContent() {
return request.resource.data.display_name.matches('^[a-z|A-Z|0-9|\\-|_]+$')
}
function isUser(userId) {
return request.auth.uid == userId
}
function isSignedIn() {
return request.auth != null;
}
function addingChildComment() {
return ((request.resource.data.diff(resource.data).affectedKeys() == ["has_children"].toSet()) || (request.resource.data.diff(resource.data).affectedKeys() == [].toSet()))
}
function commentNotTooLong() {
return request.resource.data.content == null || request.resource.data.content.size() <= 350
}
function isAdmin(uid) {
return request.auth.token.admin == true
}
}