-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncFunction.js
571 lines (539 loc) · 21.5 KB
/
SyncFunction.js
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* Copyright © Mapotempo, 2017
*
* This file is part of Mapotempo.
*
* Mapotempo is free software. You can redistribute it and/or
* modify since you respect the terms of the GNU Affero General
* Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* Mapotempo is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Mapotempo. If not, see:
* <http://www.gnu.org/licenses/agpl.html>
*/
// #############################################################################
// #############################################################################
// ## ____ ____ ##
// ## / ___| _ _ _ __ ___ / ___| __ _| |_ _____ ____ _ _ _ ##
// ## \___ \| | | | '_ \ / __| | | _ / _` | __/ _ \ \ /\ / / _` | | | | ##
// ## ___) | |_| | | | | (__ | |_| | (_| | || __/\ V V / (_| | |_| | ##
// ## |____/ \__, |_| |_|\___| \____|\__,_|\__\___| \_/\_/ \__,_|\__, | ##
// ## |___/ |___/ ##
// ## _____ _ _ ##
// ## | ___| _ _ __ ___| |_(_) ___ _ __ ##
// ## | |_ | | | | '_ \ / __| __| |/ _ \| '_ \ ##
// ## | _|| |_| | | | | (__| |_| | (_) | | | | ##
// ## |_| \__,_|_| |_|\___|\__|_|\___/|_| |_| ##
// ## ##
// ## ______ ##
// ## .-" "-. ##
// ## / \ ##
// ## | | ##
// ## |, .-. .-. ,| ##
// ## | )(__/ \__)( | ##
// ## |/ /\ \| ##
// ## (_ ^^ _) ##
// ## \__|IIIIII|__/ ##
// ## | \IIIIII/ | ##
// ## \ / ##
// ## `--------` ##
// ## ##
// #############################################################################
// ## ##
// ## DESCRIPTION : The sync Gateway function ensure the document ##
// ## synchronisation managment and security. This function ##
// ## set all the necessary to ensure mobiles can perform ##
// ## correctly. HANDLE WITH CAUTION !! ##
// ## ##
// #############################################################################
/*---------------------------------------------------------------------
| DOCUMENT-TYPE | DESCRIPTION |
-----------------------------------------------------------------------
| company | describe a company |
| user | describe a user company, refer a sync_user |
| mission | describe a mission |
| mission_placeholder | describe a mission date, this document is |
| | used to maintain missions user's channel |
| | acces. |
| mission_status | describe a status mission at a specific |
| | date with a specific mission_status_type. |
| mission_status_type | describe a status type possible for a |
| | company. |
| mission_status_action | describe previous and after |
| | mission_status_type action. |
| track | contains multiple locations history. |
| current_location | describe the current_location for a user. |
-----------------------------------------------------------------------*/
function sync_func(doc, oldDoc) {
// FIXME some time _deleted doc havn't type
// For the moment only return and don't process this case
if(doc._deleted && !doc.type && !oldDoc) {
return;
}
// ########################
// ########################
// ## ##
// ## CONST DEFINITION ##
// ## ##
// ########################
// ########################
// TYPES CONST
var COMPANY = 'company';
var USER = 'user';
var USER_PREFERENCE = 'user_settings';
var MISSION = 'mission';
var MISSIONS_PLACEHOLDER = 'missions_placeholder';
var MISSION_STATUS = 'mission_status';
var MISSION_STATUS_TYPE = 'mission_status_type';
var MISSION_STATUS_ACTION = 'mission_status_action';
var MISSION_STATUS = 'mission_status';
var USER_TRACK = 'user_track';
var USER_CURRENT_LOCATION = 'user_current_location';
var METADATA = 'metadata';
// TYPES DRIVER
var TYPES_DRIVER = {
company: company,
user: user,
user_settings: user_settings,
user_current_location: user_current_location,
user_track: user_track,
mission: mission,
missions_placeholder: missions_placeholder,
mission_status: mission_status,
mission_status_type: mission_status_type,
mission_status_action: mission_status_action,
mission_status: '',
metadata: ''
}
// ACTIONS CONST
var CREATING = 'creating';
var UPDATING = 'updating';
var DELETING = 'deleting';
// SEPARATOR
var ROLE_SEPARATOR = '.';
var CHANNEL_SEPARATOR = ':';
// ######################
// ######################
// ## ##
// ## PREPARE PARAMS ##
// ## ##
// ######################
// ######################
// *****************************************************************************************************************************************
// * params : 'type', 'action' and 'role' are accessible for all the functions *
// * type -> type is a string format extrat from doc.type, it is MANDATORY and it must be one of the key in TYPES_DRIVER *
// * action -> 'action' is the current action perfomed by the user on the document *
// * company_id -> company_id is a id string format, we can't assure in the sync function that the company really exist in the bucket ... *
// * role -> role is a string format as that 'company_id:type:action' if the action needed a role to be performed it is this *
// *****************************************************************************************************************************************
var params = {
type: 'unknow', // The type of document
action: 'unknow', // The action being performed
role: 'unknow', // The role need to perform the action
company_id: 'unknow' // The company id
}
params.type = checkAndGetType(doc, oldDoc);
params.action = checkAndGetAction(doc, oldDoc);
params.company_id = getCompanyID(doc, oldDoc, params.type);
params.role = getRoleNeed(params.company_id, params.type, params.action);
// #######################
// #######################
// ## ##
// ## CALL THE DRIVER ##
// ## ##
// #######################
// #######################
// Check role and company
requireRole(params.role);
checkCompanyID(doc, oldDoc, params.type);
TYPES_DRIVER[params.type](doc, oldDoc, params);
// #######################################
// #######################################
// ## ##
// ## TYPE DRIVER FUNCTION DEFINITION ##
// ## ##
// #######################################
// #######################################
// ###############
// COMPANY MANAGER
// ###############
function company(doc, oldDoc, params) {
var companyChannel = makeCompanyChannel(params.company_id);
switch (params.action) {
case CREATING:
case UPDATING:
checkName(doc, oldDoc);
break;
case DELETING:
break;
default:
}
// Add current doc in all channels
channel([companyChannel]);
}
// ##############
// USER MANAGER
// ##############
function user(doc, oldDoc, params) {
var sync_user = checkSyncUser(doc, oldDoc);
doc.channels = [];
var channelUser = makeUserChannel(sync_user);
doc.channels.push(channelUser);
var companyChannel = makeCompanyChannel(params.company_id);
doc.channels.push(companyChannel);
var missionStatusTypeChannel = makeMissionStatusTypeChannel(params.company_id);
doc.channels.push(missionStatusTypeChannel);
var missionStatusActionChannel = makeMissionStatusActionChannel(params.company_id);
doc.channels.push(missionStatusActionChannel);
switch (params.action) {
case CREATING:
case UPDATING:
channel([channelUser]);
var userRoles = makeUserRoles(doc, oldDoc, params.company_id);
role([sync_user], userRoles);
access([sync_user], [channelUser]);
access([sync_user], [companyChannel]);
access([sync_user], [missionStatusTypeChannel]);
access([sync_user], [missionStatusActionChannel]);
break;
case DELETING:
break;
default:
}
}
// ######################
// CURRENT LOCATION MANAGER
// ######################
function user_current_location(doc, oldDoc, params) {
var sync_user = checkSyncUser(doc, oldDoc);
switch (params.action) {
case UPDATING:
case CREATING:
var channelUser = makeUserChannel(sync_user);
channel([channelUser]);
access([sync_user], [channelUser]);
case DELETING:
default:
}
}
// #############################
// CURRENT USER SETTINGS MANAGER
// #############################
function user_settings(doc, oldDoc, params) {
var sync_user = checkSyncUser(doc, oldDoc);
switch (params.action) {
case UPDATING:
case CREATING:
var channelUser = makeUserChannel(sync_user);
channel([channelUser]);
access([sync_user], [channelUser]);
case DELETING:
default:
}
}
// ######################
// TRACK MANAGER
// ######################
function user_track(doc, oldDoc, params) {
}
// ###############
// MISSION MANAGER
// ###############
function mission(doc, oldDoc, params) {
// Check owners
var sync_user = checkSyncUser(doc, oldDoc);
requireUser(sync_user);
// Check date and make channels
var date = checkDate(doc, oldDoc);
var syncUserChannels = makeMissionChannels(sync_user, date);
switch (params.action) {
case CREATING:
case UPDATING:
checkLocation(doc, oldDoc);
checkName(doc, oldDoc);
access([sync_user], [syncUserChannels]);
break
case DELETING:
default:
}
// Add current doc in all channels
channel([syncUserChannels]);
}
// ###########################
// MISSION PLACEHOLDER MANAGER
//
// The missions placeholder is used to maintain missions user's channel
// access even that it where are all removed.
// See => https://github.com/couchbase/sync_gateway/issues/1484
// ###########################
function missions_placeholder(doc, oldDoc, params) {
// Check owners
var sync_user = checkSyncUser(doc, oldDoc);
requireUser(sync_user);
// Check date and make channels
var date = checkDate(doc, oldDoc);
var syncUserChannels = makeMissionChannels(sync_user, date);
switch (params.action) {
case CREATING:
case UPDATING:
access([sync_user], [syncUserChannels]);
break
case DELETING:
default:
}
// Add current doc in all channels
channel([syncUserChannels]);
}
// ######################
// MISSION STATUS MANAGER
// ######################
function mission_status(doc, oldDoc, params) {
}
// ######################
// MISSION STATUS TYPE MANAGER
// ######################
function mission_status_type(doc, oldDoc, params) {
var missionStatusTypeChannel = makeMissionStatusTypeChannel(params.company_id);
// Add current doc in all channels
channel([missionStatusTypeChannel]);
}
// ######################
// MISSION STATUS ACTION MANAGER
// ######################
function mission_status_action(doc, oldDoc, params) {
var missionStatusActionChannel = makeMissionStatusActionChannel(params.company_id);
// Add current doc in all channels
channel([missionStatusActionChannel]);
}
// ######################
// DEFAULT MANAGER
// ######################
function default_manager(doc, oldDoc, params) {
// TODO
}
// ###################################
// ###################################
// ## ##
// ## Function Checker definition ##
// ## ##
// ###################################
// ###################################
// ####################
// Check Document Field
// ####################
function checkSyncUser(doc, oldDoc) {
// Make sure that the user propery exists:
var sync_user = oldDoc ? oldDoc.sync_user : doc.sync_user;
if (!sync_user) {
throw ({
forbidden: 'Document must have user'
});
}
if (Array.isArray(user)) {
throw ({
forbidden: 'It can only have one user'
});
}
return sync_user;
}
function checkOwners(doc, oldDoc) {
// Make sure that the owner propery exists:
var owners = oldDoc ? oldDoc.owners : doc.owners;
if (!owners) {
throw ({
forbidden: 'Document must have owners'
});
}
if (!owners.length > 0) {
throw ({
forbidden: 'Document must have at least one owner'
});
}
return owners;
}
function checkDate(doc, oldDoc) {
// Make sure that the checkDeliveryDate propery exists:
var date = doc ? doc.date : oldDoc.date;
// On test le cas ou date est null pour prendre celui du oldDoc (pour la suppression)
date = (!date && oldDoc) ? oldDoc.date : date;
if (!date) {
throw ({
forbidden: 'Document must have a date'
});
}
if (isNaN(Date.parse(date)))
throw ({
forbidden: 'Document must have a date ISO8601 valid format'
});
return date;
}
function checkLocation(doc, oldDoc) {
// Make sure that the location propery exists in the new doc :
var location = doc.location;
if (location) {
if (isNaN(location.lon) || isNaN(location.lat))
throw ({
forbidden: 'location lat and lon must be a float values'
});
else
return location;
} else
throw ({
forbidden: 'Document must have a location'
});
return location;
}
// REMOVE checkAddress function, this isn't mandatory
/*
function checkAddress(doc, oldDoc) {
// Make sure that the address propery exists in the new doc :
var address = doc.address;
if (address) {
if (!address.street)
throw ({
forbidden: 'street field in address is mandatory'
});
if (!address.postalcode)
throw ({
forbidden: 'postalcode field in address is mandatory'
});
if (!address.city)
throw ({
forbidden: 'city field in address is mandatory'
});
if (!address.state)
throw ({
forbidden: 'state field in address is mandatory'
});
if (!address.country)
throw ({
forbidden: 'country field in address is mandatory'
});
} else
throw ({
forbidden: 'Document must have an address'
});
return address;
}
*/
function checkName(doc, oldDoc) {
if (!doc.name || typeof(doc.name) !== 'string') {
throw ({
forbidden: 'Document must have a name'
});
}
}
// ###############
// Channel Factory
// ###############
function makeCompanyChannel(company_id) {
return COMPANY + CHANNEL_SEPARATOR + company_id;
}
function makeMissionStatusTypeChannel(company_id) {
return MISSION_STATUS_TYPE + CHANNEL_SEPARATOR + company_id;
}
function makeMissionStatusActionChannel(company_id) {
return MISSION_STATUS_ACTION + CHANNEL_SEPARATOR + company_id;
}
function makeUserChannel(user) {
return USER + CHANNEL_SEPARATOR + user;
}
function makeUserCurrentLocationChannel(owner) {
return USER_CURRENT_LOCATION + CHANNEL_SEPARATOR + owner;
}
function makeMissionChannels(sync_user, date) {
// Date format yyyyMMdd for channel
var timestamp = Date.parse(date);
if (isNaN(timestamp))
throw ({
forbidden: 'Document must have a date ISO8601 valid format'
});
var date = new Date(timestamp);
var channel_date = '' + date.getFullYear() +
('0' + (date.getMonth() + 1)).slice(-2) +
('0' + date.getDate()).slice(-2)
// Create channel patern [type:owner:yyyyMMdd]
var sync_user_channel = MISSION + CHANNEL_SEPARATOR + sync_user + CHANNEL_SEPARATOR + channel_date;
return sync_user_channel;
}
// ######################
// Action and Type Helper
// ######################
function checkAndGetType(doc, oldDoc) {
var type = oldDoc ? oldDoc.type : doc.type;
if (!type || !TYPES_DRIVER[type]) {
throw ({
forbidden: 'Unknown document type'
});
}
if (oldDoc && doc) {
if (oldDoc.type !== doc.type) {
// Check is not a delete action
if (doc._deleted !== true) {
throw ({
forbidden: 'Document type cannot be modify'
});
}
}
}
return type;
}
function checkAndGetAction(doc, oldDoc) {
if (!oldDoc)
return CREATING;
else if (doc._deleted)
return DELETING;
else
return UPDATING;
}
// #################
// company_id Helper
// #################
function getCompanyID(doc, oldDoc, type) {
if (type === 'company')
return oldDoc ? oldDoc._id : doc._id;
else
return oldDoc ? oldDoc.company_id : doc.company_id;
}
function checkCompanyID(doc, oldDoc, type) {
if (type === 'company')
return;
var company_id = oldDoc ? oldDoc.company_id : doc.company_id;
if (!company_id) {
throw ({
forbidden: 'Document must have a company_id'
});
}
// Make sure that nobody can modify company id.
if (doc && oldDoc)
if (doc.company_id && oldDoc.company_id)
if (doc.company_id != oldDoc.company_id) {
throw ({
forbidden: 'Document ID cannot be modify'
});
}
}
// ###########
// Role Helper
// ###########
function getRoleNeed(company_id, type, action) {
return company_id + ROLE_SEPARATOR + type + ROLE_SEPARATOR + action;
}
function makeUserRoles(doc, oldDoc, company_id) {
var roles = oldDoc ? oldDoc.roles : doc.roles;
var res = [];
if(roles && Array.isArray(roles)) {
for (var i = 0; i < roles.length; i++) {
res[i] = 'role:' + company_id + ROLE_SEPARATOR + roles[i];
}
} else {
res[0] = 'role:default';
}
return res;
}
}