-
Notifications
You must be signed in to change notification settings - Fork 56
/
security-rules.bolt
175 lines (146 loc) · 3.6 KB
/
security-rules.bolt
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Helper methods
currentUser() { auth.uid }
isLoggedIn() { auth != null }
isCurrentUser(uid) { isLoggedIn() && currentUser() == uid }
isAdmin() { isLoggedIn() && prior(root).adminUsers[auth.uid] != null }
isThreadOpen(threadId) { prior(root).threads[threadId].locked != true }
isBoardKey(boardKey) { prior(root).boards[boardKey] != null }
isThreadKey(threadKey) { prior(root).threads[threadKey] != null }
isPostKey(postKey) { prior(root).posts[postKey] != null }
// Types
type BoardKey {
validate() { isBoardKey(this) }
}
type ThreadKey {
validate() { isThreadKey(this) }
}
type PostKey {
validate() { isPostKey(this) }
}
type CurrentUserId {
validate() { isLoggedIn() && this === currentUser() }
}
type Timestamp extends Number {
validate() { this <= now }
}
type UserMeta {
displayName: String | Null,
id: CurrentUserId | Null,
image: String | Null
}
type Settings {
theme: String
}
type User {
displayName: String,
lastLoginAt: Timestamp,
profileImageURL: String,
provider: String,
registeredAt: Number | Null,
email: String | Null,
settings: Settings | Null
}
type Post {
body: String | Null,
createdAt: Timestamp | Null,
threadId: String | Null, // TODO: investigate using ThreadKey here
replyTo: PostKey | Null,
edited: Boolean | Null,
editedLast: Timestamp | Null,
editedBy: String | Null,
user: UserMeta | Null
}
type Thread {
title: String,
boardId: BoardKey,
createdAt: Timestamp,
lastPostAt: Timestamp,
locked: Boolean | Null,
user: UserMeta
}
// Admin users
path /adminUsers {
read() { isLoggedIn() }
write() { false }
}
// Boards
path /boards {
read() { true }
/{boardId}/threads/{threadId} is Boolean {
create() { isLoggedIn() }
delete() { isAdmin() }
}
index() { [ "active" ] }
}
// Categories
path /categories {
read() { true }
write() { false }
index() { [ "active" ] }
}
// Posts
path /posts {
read() { true }
/{postId} is Post {
create() { isLoggedIn() && isThreadOpen(this.threadId) }
delete() { isAdmin() }
/body {
write() { isAdmin() || isCurrentUser(prior(this).parent().user.id) }
}
/edited {
write() { isAdmin() || isCurrentUser(prior(this).parent().user.id) }
}
/editedBy {
write() { isAdmin() || isCurrentUser(prior(this).parent().user.id) }
}
/editedLast {
write() { isAdmin() || isCurrentUser(prior(this).parent().user.id) }
}
/likes/{uid} is Boolean {
write() { isCurrentUser(uid) }
}
}
}
// Threads
path /threads {
read() { true }
/{threadId} is Thread {
create() { isLoggedIn() }
update() { isAdmin() }
delete() { isAdmin() }
// updating other fields is only allowed for admin but
// we need to allow updating lastPostAt for logged in users
/lastPostAt {
write() { isLoggedIn() }
}
/posts/{postId} is Boolean {
create() { isLoggedIn() && prior(root).posts[postId] == null }
delete() { isAdmin() }
}
}
}
// Users
// TODO: more fine-grained permissions
path /users/{uid} is User {
read() { true }
write() { isCurrentUser(uid) || isAdmin() }
/posts/{postId} is Boolean {
// TODO: these currently have no effect as write is already allowed
// for current user & admin
create() { isCurrentUser(uid) }
delete() { isAdmin() }
}
/threadsStarted/{threadId} is Boolean {
create() { isThreadKey(threadId) }
}
/likes/{postId} is Boolean {
// TODO: this currently has no effect as write is already allowed
// for current user & admin
write() { isCurrentUser(uid) && isPostKey(postId) }
}
}
// Settings
path /settings {
read() { true }
write() { false }
}