forked from chat-sdk/chat-sdk-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-rules.json
364 lines (296 loc) · 13.4 KB
/
firebase-rules.json
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
{
"rules": {
// ".read": "auth != null",
// ".write": "auth != null",
"$root": {
// Clients can write a time value indexed by their user ID
// The time must be now
"time": {
"$user_id": {
".read": "auth.uid == $user_id",
".write": "auth.uid == $user_id",
".validate": "newData.val() == now"
}
},
// Clients can put themself online but they must write a time with value set to now
// Any client can read the client list
"online": {
"$user_id": {
".read": "auth != null",
".write": "auth.uid == $user_id",
"time": {
".validate": "newData.val() == now"
},
"uid": {
".validate": "auth.uid == $user_id"
}
}
},
"flagged": {
"messages": {
"$message_id": {
// The user who flagged the message can read and write
".read": "!data.exists() || (data.child('creator-entity-id').val() == auth.uid)",
".write":"!data.exists() || (data.child('creator-entity-id').val() == auth.uid)",
// The message and thread must exist
".validate": "root.child($root + '/threads/' +newData.child('thread').val() + '/messages/' + $message_id).exists()",
"sender-entity-id":{
".validate": "root.child($root+'/users/'+newData.val()).exists()"
},
"creator-enitity-id": {
".validate": "newData.val() == auth.uid"
},
"date": {
".validate": "newData.val() == now"
}
}
}
},
"public-threads": {
".read": "auth != null",
"$thread_id": {
// Only the creator could delete a public chat room
".write":"root.child($root+'/users/'+auth.uid).exists() && (!data.exists() || root.child($root + '/threads/' + $thread_id + '/details/creator-entity-id').val() == auth.uid)",
}
},
"threads": {
"$thread_id": {
// The only thread users can read the thread
// Anyone can read a public thread
".read": "data.child('users/'+auth.uid).exists() || data.child('details/type').val() == 1 || data.child('details/type_v4').val() == 4",
"meta": {
// Any user can make a new thread. Only the creator can change the details
".write": "!data.exists() || (data.child('creator-entity-id').val() == auth.uid)",
},
"details": {
// Any user can make a new thread. Only the creator can change the details
".write": "!data.exists() || (data.child('creator-entity-id').val() == auth.uid)",
"creation-date": {
// This can only be set on creation - the time must be the current time
".validate": "newData.val() == now"
},
"name": {
".validate": "newData.val().length < 100"
},
"creator-entity-id": {
// A room could be created by the server
".validate": "newData.val() == auth.uid"
},
"type": {
// Legacy type value for backwards compatibility
// 0x0 (0) - Group
// 0x1 (1) - Public
".validate": "newData.val() == 0 || newData.val() == 1"
},
"type_v4": {
// Can only be set on thread creation must be
// 0x1 (1) - Group
// 0x2 (2) - 1to1
// 0x4 (4) - Public
// 0x8 (8) -
// 0x16 (22) -
// 0x32 (50) -
".validate": "newData.val() == 1 || newData.val() == 2 || newData.val() == 4 || newData.val() == 8 || newData.val() == 16 || newData.val() == 32"
}
},
"lastMessage": {
".write": "!data.exists() || data.parent().parent().parent().child('users/' + auth.uid).exists()",
"type": {
// Can only be set on message creation must be 0 - 7
".validate": "newData.val() >= 0 && newData.val() <= 7"
},
// It should be the same as the last message
// "JSON": {
// ".validate": "newData.val() == data.parent().parent().child('messages')"
// }
// "user-firebase-id": {
// // Can only be set on message creation - a user can only mark a message with their uid
// ".validate": "newData.val() == auth.uid"
// }
},
"updated": {
// Allow the client to update the thread if they are a member
// or if it's a public thread
".write": "!data.exists() || data.parent().parent().parent().child('users/' + auth.uid).exists()|| data.parent().child('details/type').val() == 1 || data.parent().child('details/type_v4').val() == 4",
"details": {
".validate": "newData.val() == now"
},
"messages": {
".validate": "newData.val() == now"
},
"users": {
//".validate": "newData.val() == now"
}
},
"messages": {
".indexOn": ["date"],
"$message_id": {
// The user must be in the user list.
// Any user can make a new message
// Only the sender can modify a message
// Make sure that if this is a private thread that the user isn't blocked
".write": "data.parent().parent().child('users/' + auth.uid).exists() && (!data.exists() || data.child('user-firebase-id').val() == auth.uid)",
// || root.child($root + '/users/' + auth.uid + '/blocked/' + newData.child('user-firbease-id').val()).exists() == false && false
// ".validate": "data.parent().parent().child('details/type_v4').val() != 2 || !data.parent().parent().child('details/type_v4').exists() || root.child($root + '/users/' + auth.uid + '/blocked/' + newData.child('user-firbease-id').val()).exists()",
// The problem is that we don't know who the message is to
// ".validate": "root.child($root + '/users/' + data.parent().parent().child('users').child('') + auth.uid + '/blocked/').exists()",
"date": {
// Must be current time
".validate": "newData.val() == now"
},
"payload": {
".validate": "newData.val() != null"
},
"type": {
// Can only be set on message creation must be 0 - 7
".validate": "!data.exists() && (newData.val() >= 0 && newData.val() <= 7)"
},
"user-firebase-id": {
// Can only be set on message creation - a user can only mark a message with their uid
".validate": "!data.exists() && newData.val() == auth.uid"
},
"read": {
"$user_id": {
".write": "auth.uid == $user_id && root.child($root+'/threads/'+$thread_id+'/users/'+auth.uid).exists()",
"status": {
//".validate": "newData.val() >= -2 && newData.val() <= 2"
}
}
}
}
},
"typing": {
"$user_id": {
".write": "auth.uid == $user_id"
}
},
"users": {
// TODO: Implement this
// For a user to be added to the thread, they should exist in the users area,
// If this is a public thread, then the user must have an account
// (root.child($root+'/threads/'+$thread_id+'/details/type').val() == 1 &&
// root.child($root+'/users/'+auth.uid).exists()) ||
// If this is a private thread, then only the creator can add a user
// (root.child($root+'/threads/'+$thread_id+'/details/type').val() == 0 &&
// (root.child($root+'/threads/'+$thread_id+'/details/creator-entity-id').val() == auth.uid ||
// root.child($root+'/threads/'+$thread_id+'/users/'+auth.uid).exists()))",
// The user must be a member of the thread to see which users exist
//".read": "root.child($root+'/threads/'+$thread_id+'/users/'+auth.uid).exists()",
// Anyone can add to a public group
// To write to a private group you must be the creator or a member
//".write": "(root.child($root+'/threads/'+$thread_id+'/details/type').val() == 1 &&
// root.child($root+'/users/'+auth.uid).exists()) ||
// (root.child($root+'/threads/'+$thread_id+'/details/type').val() == 0 &&
// (root.child($root+'/threads/'+$thread_id+'/details/creator-entity-id').val() == auth.uid ||
// root.child($root+'/threads/'+$thread_id+'/users/'+auth.uid).exists()))",
"$user_id": {
// Members can add users and the creator can too
// Any user can remove themself
".write": "data.parent().parent().child('users/' + auth.uid).exists() || data.parent().parent().child('details/creator-entity-id').val() == auth.uid || $user_id == auth.uid",
// Add this back in - this is removed because it will prevent us from
// creating a thread with a user who doesn't exist. This is a problem
// for the migration
".validate": "root.child($root+'/users/'+$user_id).exists()",
"status": {
// Only the thread creator can be the owner
".validate": "(newData.val() == 'owner' && data.parent().parent().parent().child('details/creator-entity-id').val() == $user_id) || newData.val() == 'member'"
}
}
}
}
},
"users": {
".indexOn": ["meta/name", "meta/phone", "meta/email", "meta/location", "meta/country-code, meta/username"],
".read": "data.child(auth.uid).exists() || !data.child(auth.uid).exists()",
"$user_id": {
// ".read": "$user_id == auth.uid || data.parent().child(auth.uid).exists()",
"last-online": {
".write": "auth.uid == $user_id",
".validate": "newData.val() == now"
},
"meta": {
".write": "auth.uid == $user_id",
},
"contacts": {
"$contact_user_id": {
".write": "auth.uid == $user_id",
".validate": "data.parent().parent().parent().child($blocked_user_id).exists()"
}
},
"blocked": {
"$blocked_user_id": {
".write": "auth.uid == $user_id",
".validate": "data.parent().parent().parent().child($blocked_user_id).exists()"
}
},
"online": {
".write": "auth.uid == $user_id",
".validate": "newData.val() == true || newData.val() == false"
},
"updated": {
"meta": {
".write": "data.parent().parent().parent().child(auth.uid).exists()",
".validate": "newData.val() == now"
},
"threads": {
".write": "data.parent().parent().parent().child(auth.uid).exists()",
".validate": "newData.val() == now"
}
},
"threads": {
"$thread_id": {
// The writing user should have a user profile
".write": "root.child($root+'/users/'+auth.uid).exists()",
// The thread sholud exist
".validate": "root.child($root+'/threads/'+$thread_id).exists()"
}
}
}
},
"searchIndex": {
// Any authenticated user can read the data
".read": "root.child($root+'/users/'+auth.uid).exists()",
// Index the items to improve search performance
".indexOn": ["name", "email", "phone", "name-lowercase"],
"$user_id": {
// We can only write to the index related to our user
".write": "$user_id == auth.uid",
}
},
// GeoFire Rules or location based chat
"location": {
// Allow anyone to read the GeoFire index
".read": true,
// Index each location's geohash for faster querying
".indexOn": ["g"],
// Schema validation
"$key": {
// Allow anyone to add, update, or remove keys in the GeoFire index
".write": true,
// Key validation
".validate": "newData.hasChildren(['g', 'l']) && newData.getPriority().length <= 22 && newData.getPriority().length > 0",
// Geohash validation
"g": {
".validate": "newData.val() == newData.parent().getPriority()"
},
// Location coordinates validation
"l": {
"0" : {
".validate": "newData.isNumber() && newData.val() >= -90 && newData.val() <= 90"
},
"1" : {
".validate": "newData.isNumber() && newData.val() >= -180 && newData.val() <= 180"
},
"$other": {
".validate": false
}
},
// Don't allow any other keys to be written
"$other": {
".validate": false
}
}
}
}
}
}