@qikdev/sdk 0.1.169 → 0.1.180

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/.gitlab-ci.yml +24 -0
  2. package/dist/main.js +1 -1
  3. package/jsdoc.conf +10 -0
  4. package/package.json +5 -24
  5. package/public/docs/access.html +3826 -0
  6. package/public/docs/api.html +1356 -0
  7. package/public/docs/auth.html +2180 -0
  8. package/public/docs/cache.html +351 -0
  9. package/public/docs/content.html +2560 -0
  10. package/public/docs/files.html +779 -0
  11. package/public/docs/filter.html +1355 -0
  12. package/public/docs/fonts/Montserrat/Montserrat-Bold.eot +0 -0
  13. package/public/docs/fonts/Montserrat/Montserrat-Bold.ttf +0 -0
  14. package/public/docs/fonts/Montserrat/Montserrat-Bold.woff +0 -0
  15. package/public/docs/fonts/Montserrat/Montserrat-Bold.woff2 +0 -0
  16. package/public/docs/fonts/Montserrat/Montserrat-Regular.eot +0 -0
  17. package/public/docs/fonts/Montserrat/Montserrat-Regular.ttf +0 -0
  18. package/public/docs/fonts/Montserrat/Montserrat-Regular.woff +0 -0
  19. package/public/docs/fonts/Montserrat/Montserrat-Regular.woff2 +0 -0
  20. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
  21. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.svg +978 -0
  22. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
  23. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
  24. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
  25. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
  26. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +1049 -0
  27. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
  28. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
  29. package/public/docs/fonts/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
  30. package/public/docs/index.html +96 -0
  31. package/public/docs/qik.access.js.html +790 -0
  32. package/public/docs/qik.api.js.html +511 -0
  33. package/public/docs/qik.auth.js.html +1006 -0
  34. package/public/docs/qik.cache.js.html +131 -0
  35. package/public/docs/qik.content.js.html +918 -0
  36. package/public/docs/qik.core.js.html +301 -0
  37. package/public/docs/qik.files.js.html +219 -0
  38. package/public/docs/qik.filter.js.html +611 -0
  39. package/public/docs/qik.socket.js.html +416 -0
  40. package/public/docs/qik.system.js.html +117 -0
  41. package/public/docs/qik.utils.js.html +1467 -0
  42. package/public/docs/scripts/collapse.js +20 -0
  43. package/public/docs/scripts/linenumber.js +25 -0
  44. package/public/docs/scripts/nav.js +12 -0
  45. package/public/docs/scripts/polyfill.js +4 -0
  46. package/public/docs/scripts/prettify/Apache-License-2.0.txt +202 -0
  47. package/public/docs/scripts/prettify/lang-css.js +2 -0
  48. package/public/docs/scripts/prettify/prettify.js +28 -0
  49. package/public/docs/scripts/search.js +83 -0
  50. package/public/docs/socket.html +810 -0
  51. package/public/docs/styles/jsdoc.css +765 -0
  52. package/public/docs/styles/prettify.css +79 -0
  53. package/public/docs/system.html +197 -0
  54. package/src/api/qik.access.js +790 -0
  55. package/src/api/qik.api.js +431 -0
  56. package/src/api/qik.auth.js +938 -0
  57. package/src/api/qik.cache.js +63 -0
  58. package/src/api/qik.content.js +858 -0
  59. package/src/api/qik.core.js +246 -0
  60. package/src/api/qik.files.js +150 -0
  61. package/src/api/qik.filter.js +543 -0
  62. package/src/api/qik.geo.js +895 -0
  63. package/src/api/qik.socket.js +348 -0
  64. package/src/api/qik.system.js +49 -0
  65. package/src/api/qik.utils.js +1410 -0
  66. package/src/index.js +20 -0
  67. package/src/version.js +1 -0
  68. package/test/index.spec.js +41 -0
  69. package/test/mocha.opts +0 -0
  70. package/dist/sdk.js +0 -2
  71. package/dist/sdk.js.LICENSE.txt +0 -8
@@ -0,0 +1,790 @@
1
+
2
+
3
+
4
+ /**
5
+ * Creates a new Access Module instance.
6
+ * This module provides a number of helper functions for managing and understanding permissions of the current
7
+ * application or user
8
+ * @alias access
9
+ * @constructor
10
+ * @hideconstructor
11
+ * @param {QikCore} qik A reference to the parent instance of the QikCore module. This module is usually created by a QikCore instance that passes itself in as the first argument.
12
+ */
13
+
14
+ var QikAccess = function(QikCore) {
15
+
16
+ if (!QikCore.auth) {
17
+ throw new Error(`Can't Instantiate QikAccess before QikAccess has been initialized`);
18
+ }
19
+
20
+ if (!QikCore.utils) {
21
+ throw new Error(`Can't Instantiate QikAccess before QikCore.utils has been initialized`);
22
+ }
23
+
24
+ //////////////////////////////////
25
+
26
+ var service = {};
27
+
28
+ ///////////////////////////////////////////////
29
+
30
+ service.featureFlags = {
31
+ "all": 1,
32
+ "impersonation": 2,
33
+ "products": 4,
34
+ "checkin": 8,
35
+ "events": 16,
36
+ "ticketing": 32,
37
+ "emails": 64,
38
+ "sms": 128,
39
+ "images": 256,
40
+ "actions": 512,
41
+ "timers": 1024,
42
+ "listenertriggers": 2048,
43
+ "builder": 4096,
44
+ "scopes": 8192,
45
+ "components": 16384,
46
+ "video": 32768,
47
+ "audio": 65536,
48
+ "files": 131072,
49
+ "developer": 262144,
50
+ "notifications": 524288,
51
+ "payments": 1048576,
52
+ "comments": 2097152,
53
+ "workflows": 4194304,
54
+ "forms": 8388608
55
+ }
56
+
57
+ service.featureIsIncludedInByteflag = function(byteflag, feature) {
58
+ return (byteflag & service.featureFlags[feature]) === service.featureFlags[feature];
59
+ }
60
+
61
+ ///////////////////////////////////////////////
62
+
63
+ /**
64
+ * @alias access.hasFeature
65
+ * @description Checks if a user has a specific feature enabled for their organisation
66
+ * @param {Object} user The user to check
67
+ * @param {String} feature The feature to check is enabled/disabled
68
+ * @example
69
+ * const user = sdk.auth.getCurrentUser();
70
+ * const formsFeatureIsEnabled = sdk.access.hasFeature(user, 'forms');
71
+ */
72
+ service.hasFeature = function(user, feature) {
73
+
74
+ const organisation = user?.organisation || {};
75
+ const features = organisation.features || {};
76
+
77
+ if(features.all) {
78
+ return true;
79
+ }
80
+
81
+ if(features[feature]) {
82
+ return true;
83
+ }
84
+
85
+ // Check the byteflag
86
+ const byteflag = organisation.ff;
87
+
88
+ if(service.featureIsIncludedInByteflag(byteflag, 'all')) {
89
+ return true;
90
+ }
91
+
92
+ //Let us know if there are any permissions for this user
93
+ return service.featureIsIncludedInByteflag(byteflag, feature)
94
+ }
95
+
96
+ ///////////////////////////////////////////////
97
+
98
+ /**
99
+ * @alias access.isAdministrator
100
+ * @description Check if a user object is an administrator
101
+ * @param {Object} user The user session to check
102
+ * @example
103
+ * const userIsAnAdministrator = sdk.access.isAdministrator({firstName:'Jeffrey', lastName:'Winger', ...});
104
+ */
105
+ service.isAdministrator = function(user) {
106
+ return user && user?.userType == 'administrator';
107
+ }
108
+
109
+ ///////////////////////////////////////////////
110
+
111
+ /**
112
+ * @alias access.getAllDescendants
113
+ * @description Get all children of a specified scope
114
+ * @param {Object} scope A scope object with child scopes
115
+ * @example
116
+ * const childScopes = sdk.access.getAllDescendants({title:'Global', children:[{title:'Australia', ...}, {title:'New Zealand', ...}]});
117
+ */
118
+ service.getAllDescendants = function(scope) {
119
+
120
+ //Start by including the scope id
121
+ var results = [];
122
+
123
+ addScope(scope);
124
+
125
+ function addScope(entry) {
126
+ //Add this scope to the results
127
+ var id = QikCore.utils.id(entry);
128
+ results.push(id);
129
+
130
+ (entry.children || []).forEach(function(child) {
131
+ addScope(child);
132
+ })
133
+ }
134
+ return results;
135
+
136
+
137
+ }
138
+
139
+ ///////////////////////////////////////////////
140
+
141
+ /**
142
+ * @alias access.hashPermissions
143
+ * @description Get a lookup of all permissions a user has been granted
144
+ * @param {Object} user A user session object
145
+ * @example
146
+ * const permissions = sdk.access.hashPermissions({firstName:'Mighty', lastName:'Mouse', permissionSets:[...], ...});
147
+ */
148
+ service.hashPermissions = function(user) {
149
+
150
+ var results = {};
151
+
152
+ for (var key in (user?.permissions || {})) {
153
+
154
+ //Get the permission set
155
+ var permissionSet = user?.permissions[key];
156
+
157
+ //Get all Scope ids from the permission set
158
+ var allIDs = service.getAllDescendants(permissionSet);
159
+
160
+ //For each permission defined in the set
161
+ (permissionSet.permissions || []).forEach(function(permissionString) {
162
+ //Include the top scope
163
+ if (!results[permissionString]) {
164
+ results[permissionString] = []
165
+ }
166
+ results[permissionString] = [...new Set(results[permissionString].concat(allIDs))]
167
+ });
168
+ }
169
+
170
+ ///////////////////////////////////////////////
171
+
172
+ return results;
173
+
174
+ }
175
+
176
+
177
+ ///////////////////////////////////////////////
178
+
179
+ /**
180
+ * @alias access.allUserScopes
181
+ * @description Return an array of ids of all scopes that a given user has been granted a permission in
182
+ * @param {Object} user A user session object
183
+ * @example
184
+ * const permissions = sdk.access.allUserScopes({firstName:'Mighty', lastName:'Mouse', permissionSets:[...], ...});
185
+ *
186
+ * // Would result in:
187
+ * ['61eca4746971e75c1fc670cf', '61eca4746971e75c1fc670ca', '77eca4746971e75c1fc670cf'],
188
+ */
189
+ service.allUserScopes = function(user) {
190
+
191
+ var collected = {};
192
+ var permissions = user?.permissions || {};
193
+
194
+ ////////////////////////
195
+
196
+ function pluckScope(scopeObject) {
197
+
198
+ if (!scopeObject) {
199
+ return;
200
+ }
201
+
202
+ var scopeID = QikCore.utils.id(scopeObject);
203
+ if (!scopeID) {
204
+ return;
205
+ }
206
+
207
+ //Add to the collection
208
+ collected[scopeID] = 1;
209
+
210
+ //If there are children, then recursively pluck the ids
211
+ if (scopeObject.children && scopeObject.children.length) {
212
+ scopeObject.children.forEach(function(scope) {
213
+ pluckScope(scope);
214
+ })
215
+ }
216
+ }
217
+
218
+ ////////////////////////
219
+
220
+ for (var scopeID in permissions) {
221
+ pluckScope(permissions[scopeID]);
222
+ }
223
+
224
+ ////////////////////////
225
+
226
+ return Object.keys(collected);
227
+ }
228
+
229
+ ///////////////////////////////////////////////
230
+
231
+ /**
232
+ * @alias access.actionableScopes
233
+ * @description Return an array of all scopes that a given user can perform a specified action in
234
+ * @param {Object} user A user session object
235
+ * @param {String} action The action
236
+ * @param {String} definition The defined type of item the action will be performed on
237
+ * @param {String} type The basic type of item the action will be performed on
238
+ * @param {Object} options Additional options and parameters
239
+ * @example
240
+ * const user = sdk.auth.getCurrentUser();
241
+ * const canCreateInScopes = sdk.access.actionableScopes(user, 'create', 'car', 'article');
242
+ * const canDeleteInScopes = sdk.access.actionableScopes(user, 'delete', 'profile');
243
+ *
244
+ * // Would result in something like:
245
+ * ['61eca4746971e75c1fc670cf', '61eca4746971e75c1fc670ca', '77eca4746971e75c1fc670cf'],
246
+ */
247
+ service.actionableScopes = function(user, action, definition, type, options) {
248
+ options = options || {}
249
+
250
+ /////////////////////////
251
+
252
+ var validScopes = [];
253
+
254
+ if (user?.userType == 'administrator') {
255
+ validScopes = service.allUserScopes(user, options)
256
+ } else {
257
+ var permissionsLookup = service.hashPermissions(user);
258
+
259
+ var checkTypeName = definition || type;
260
+ if (!checkTypeName) {
261
+ return validScopes;
262
+ }
263
+
264
+ var stringKey = `${checkTypeName}.${action}`;
265
+ validScopes = permissionsLookup[stringKey]
266
+ }
267
+
268
+ /////////////////////////
269
+
270
+ return validScopes || []
271
+ }
272
+
273
+
274
+ ///////////////////////////////////////////////
275
+
276
+ /**
277
+ * @alias access.isOwner
278
+ * @description Return whether or not a user is considered an Owner of a specified item
279
+ * @param {Object} user The user to check
280
+ * @param {Object} item The item
281
+ * @example
282
+ * const user = sdk.auth.getCurrentUser();
283
+ * const item = {title:'A piece of content', meta:{personaAuthor:'61eca4746971e75c1fc670ca'}, ...}
284
+ *
285
+ * // Returns true if the user owns the item
286
+ * const isTheOwner = sdk.access.isOwner(user, item);
287
+ */
288
+ service.isOwner = function(user, item) {
289
+ var userID = QikCore.utils.id(user);
290
+ var personaID = QikCore.utils.id(user?.persona);
291
+
292
+
293
+ var metaObject = item.meta;
294
+ if (!metaObject) {
295
+ return false;
296
+ }
297
+
298
+ /////////////////////////////////////////////
299
+
300
+ var userOwners = metaObject.userOwners || [];
301
+
302
+ var users = QikCore.utils.ids([...userOwners, metaObject.userAuthor]);
303
+ var userHash = QikCore.utils.hash(users);
304
+
305
+ if (userHash[userID]) {
306
+ return true;
307
+ }
308
+
309
+ /////////////////////////////////////////////
310
+
311
+ var personaOwners = metaObject.personaOwners || [];
312
+
313
+ var personas = QikCore.utils.ids([...personaOwners, metaObject.personaAuthor]);
314
+ var personaHash = QikCore.utils.hash(personas);
315
+ // console.log('PERSONAS', personas, personaHash, '-', metaObject.personaAuthor, metaObject.personaOwners)
316
+
317
+ if (personaHash[personaID]) {
318
+ return true;
319
+ }
320
+
321
+ }
322
+
323
+
324
+ ///////////////////////////////////////////////
325
+
326
+ /**
327
+ * @alias access.allPermissions
328
+ * @description Returns an array of all permissions a given user has access to
329
+ * @param {Object} user The user to check
330
+ * @example
331
+ * const user = sdk.auth.getCurrentUser();
332
+ * const permissions = sdk.access.allPermissions(user);
333
+ *
334
+ * // Returns
335
+ * ['image.viewany', 'testimonial.viewany', 'testimonial.viewfield.title', ...]
336
+ */
337
+ service.allPermissions = function(user) {
338
+
339
+ var allPermissions = Object.values(user?.permissions)
340
+ .map(function(permissionScope) {
341
+ return permissionScope.permissions;
342
+ })
343
+ .flat();
344
+
345
+ return allPermissions;
346
+ }
347
+
348
+ ///////////////////////////////////////////////
349
+
350
+ /**
351
+ * @alias access.allPermissionTypes
352
+ * @description Returns an array of content types a given user has permission to interact with
353
+ * @param {Object} user The user to check
354
+ * @param {Boolean} asHash Whether to retrieve the result as a keyed object hash, by default
355
+ * the response will be an array
356
+ * @example
357
+ * const user = sdk.auth.getCurrentUser();
358
+ *
359
+ * // Returns ['image', 'testimonial']
360
+ * const types = sdk.access.allPermissionTypes(user);
361
+ *
362
+ * // Returns {image:true, testimonial:true}
363
+ * const types = sdk.access.allPermissionTypes(user, true);
364
+ */
365
+ service.allPermissionTypes = function(user, asHash) {
366
+ if (asHash) {
367
+ return service.allPermissions(user).reduce(function(set, string) {
368
+ var type = string.split('.')[0];
369
+ set[type] = true;
370
+ return set;
371
+ }, {});
372
+ } else {
373
+ return service.allPermissions(user).map(function(string) {
374
+ return string.split('.')[0];
375
+ });
376
+ }
377
+
378
+ }
379
+
380
+ ///////////////////////////////////////////////
381
+
382
+ /**
383
+ * @alias access.canKnowOf
384
+ * @description Checks if a user has any permissions relating to a specified type of content
385
+ * @param {Object} user The user to check
386
+ * @param {String} typeKey The type or definition to check
387
+ * @param {Object} options Additional options and parameters
388
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
389
+ * @example
390
+ * const user = sdk.auth.getCurrentUser();
391
+ * const canAccessImageSection = sdk.access.canKnowOf(user, 'image');
392
+ * const canAccessCustomerUploadSection = sdk.access.canKnowOf(user, 'customerUpload');
393
+ */
394
+ service.canKnowOf = function(user, typeKey, options) {
395
+ options = options || {};
396
+ options.cache = options.cache || {};
397
+
398
+ //Let us know if there are any permissions for this user
399
+ return service.allPermissionTypes(user, true)[typeKey];
400
+ }
401
+
402
+
403
+ ///////////////////////////////////////////////
404
+
405
+ /**
406
+ * @alias access.checkActionAccess
407
+ * @description Helpful function for checking whether a given user has access to do a given action on a specified item
408
+ * @param {Object} user The user to check
409
+ * @param {Object} item The item to check
410
+ * @param {String} ANY_ACTION The 'any' action to check
411
+ * @param {String} OWN_ACTION The 'owned' action to check
412
+ * @param {Object} options Additional options and parameters
413
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
414
+ * @example
415
+ * const user = sdk.auth.getCurrentUser();
416
+ * const canEditItem = sdk.access.checkActionAccess(user, {_id:'61eca4746971e75c1fc670ca', meta:{...}}, 'editany', 'editown');
417
+ * const canViewItem = sdk.access.checkActionAccess(user, {_id:'61eca4746971e75c1fc670ca', meta:{...}}, 'viewany', 'viewown');
418
+ *
419
+ * // Example of looping through a large array of items
420
+ * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
421
+ *
422
+ * // Create an in memory cache object outside of the loop
423
+ * const cache = {}
424
+ *
425
+ * const filtered = largeArrayOfItems.filter(function(item) {
426
+ * return sdk.access.checkActionAccess(user, item, 'editany', 'editown', {cache});
427
+ * })
428
+ */
429
+ service.checkActionAccess = function(user, item, ANY_ACTION, OWN_ACTION, options) {
430
+
431
+ options = options || {};
432
+ options.cache = options.cache || {};
433
+
434
+ //Allow everything if we're a super user
435
+ if (service.isAdministrator(user)) {
436
+ return true;
437
+ }
438
+
439
+ //////////////////////////////////
440
+
441
+ var type = item.meta.type;
442
+ var definition = item.meta.definition || type;
443
+
444
+ //////////////////////////////////
445
+
446
+ var itemScopes = item.meta.scopes || [];
447
+
448
+ //////////////////////////////////
449
+
450
+ var actionAnyScopesHash;
451
+ var anyCacheKey = `access-${user?.cacheKey}-${ANY_ACTION}-${definition}-${type}`
452
+
453
+ if (options.cache[anyCacheKey]) {
454
+ actionAnyScopesHash = options.cache[anyCacheKey];
455
+
456
+ } else {
457
+ //Check if we can view this type of thing in any scopes
458
+ var actionAnyScopes = service.actionableScopes(user, ANY_ACTION, definition, type, options);
459
+
460
+ //Get a fash hash of the scopes
461
+ actionAnyScopesHash = QikCore.utils.hash(actionAnyScopes);
462
+ options.cache[anyCacheKey] = actionAnyScopesHash;
463
+ }
464
+
465
+ //////////////////////////////////
466
+
467
+ //If we have data about what they can do
468
+ if (actionAnyScopesHash) {
469
+ //Check if the item is in any of the scopes the user can access
470
+ var isInAnyScopes = itemScopes.some(function(scopeID) {
471
+ return actionAnyScopesHash[scopeID];
472
+ })
473
+
474
+ if (isInAnyScopes) {
475
+ return true;
476
+ }
477
+ }
478
+
479
+ //////////////////////////////////
480
+
481
+ //Check if the user owns this content
482
+ var isOwner = service.isOwner(user, item);
483
+
484
+ //If the user is the owner
485
+ if (isOwner) {
486
+ var actionOwnScopesHash;
487
+ var ownCacheKey = `access-${user?.cacheKey}-${OWN_ACTION}-${definition}-${type}`
488
+
489
+ if (options.cache[ownCacheKey]) {
490
+ actionOwnScopesHash = options.cache[ownCacheKey];
491
+ } else {
492
+
493
+ //Check if we can view this type of thing in any scopes
494
+ var actionOwnScopes = service.actionableScopes(user, OWN_ACTION, definition, type, options);
495
+ //Get a fash hash of the scopes
496
+ actionOwnScopesHash = QikCore.utils.hash(actionOwnScopes);
497
+ options.cache[ownCacheKey] = actionOwnScopesHash;
498
+ }
499
+
500
+ //If we have data about what they can do
501
+ if (actionOwnScopesHash) {
502
+ //Check if the item is in any of the scopes the user can access
503
+ var isInOwnScopes = itemScopes.some(function(scopeID) {
504
+ return actionOwnScopesHash[scopeID];
505
+ })
506
+
507
+ if (isInOwnScopes) {
508
+ return true;
509
+ }
510
+ }
511
+ }
512
+
513
+ //////////////////////////////////
514
+
515
+ //No access by default
516
+ return false
517
+ }
518
+
519
+
520
+ ///////////////////////////////////////////////
521
+
522
+ /**
523
+ * @alias access.has
524
+ * @description Helpful function for checking whether a given user has a specified permission
525
+ * @param {Object} user The user to check
526
+ * @param {String} permission The permission the user may have been granted
527
+ * @example
528
+ * const user = sdk.auth.getCurrentUser();
529
+ * const isAllowedToCreateItems = sdk.access.has(user, 'video.create');
530
+ */
531
+ service.has = function(user, permission) {
532
+
533
+ //Allow everything if we're a super user
534
+ if (service.isAdministrator(user)) {
535
+ return true;
536
+ }
537
+
538
+ var permissionsLookup = service.hashPermissions(user);
539
+ return permissionsLookup[permission];
540
+ }
541
+
542
+
543
+ ///////////////////////////////////////////////
544
+
545
+ /**
546
+ * @alias access.canCreate
547
+ * @description Helpful function for checking whether a given user can create a specified type of item
548
+ * @param {Object} user The user to check
549
+ * @param {String} definition The defined type key
550
+ * @param {String} type The basic type key
551
+ * @param {Object} options Additional options and parameters
552
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
553
+ * @example
554
+ * const user = sdk.auth.getCurrentUser();
555
+ * const userCanCreateShortFilms = sdk.access.canCreate(user, 'shortFilm', 'video');
556
+ */
557
+ service.canCreate = function(user, definition, type, options) {
558
+
559
+ options = options || {}
560
+ options.cache = options.cache || {}
561
+
562
+
563
+ //Allow everything if we're a super user
564
+ if (service.isAdministrator(user)) {
565
+ return true;
566
+ }
567
+
568
+ var createableScopes = service.actionableScopes(user, 'create', definition, type);
569
+ return createableScopes.length
570
+ }
571
+
572
+ ///////////////////////////////////////////////
573
+
574
+ /**
575
+ * @alias access.canListItem
576
+ * @description Helpful function for checking whether a given user can list a specified item
577
+ * @param {Object} user The user to check
578
+ * @param {Object} item The item to check
579
+ * @param {Object} options Additional options and parameters
580
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
581
+ * @example
582
+ * const user = sdk.auth.getCurrentUser();
583
+ *
584
+ * // Example of looping through a large array of items
585
+ * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
586
+ *
587
+ * // Create an in memory cache object outside of the loop
588
+ * const cache = {}
589
+ *
590
+ * const filtered = largeArrayOfItems.filter(function(item) {
591
+ * return sdk.access.canListItem(user, item, {cache});
592
+ * })
593
+ */
594
+ service.canListItem = function(user, item, options) {
595
+ options = options || {};
596
+ options.cache = options.cache || {}
597
+
598
+
599
+ //Check if we can list the item
600
+ var canList = service.checkActionAccess(user, item, 'listany', 'listown', options);
601
+
602
+
603
+ if (canList) {
604
+ return true;
605
+ }
606
+
607
+ //If we can view the item then we should be able to list it also
608
+ var canView = service.canViewItem(user, item, options);
609
+ if (canView) {
610
+ return true;
611
+ }
612
+
613
+ return false;
614
+
615
+ }
616
+
617
+
618
+ ///////////////////////////////////////////////
619
+
620
+ /**
621
+ * @alias access.canViewItem
622
+ * @description Helpful function for checking whether a given user can view a specified item
623
+ * @param {Object} user The user to check
624
+ * @param {Object} item The item to check
625
+ * @param {Object} options Additional options and parameters
626
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
627
+ * @example
628
+ * const user = sdk.auth.getCurrentUser();
629
+ *
630
+ * // Example of looping through a large array of items
631
+ * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
632
+ *
633
+ * // Create an in memory cache object outside of the loop
634
+ * const cache = {}
635
+ *
636
+ * const filtered = largeArrayOfItems.filter(function(item) {
637
+ * return sdk.access.canViewItem(user, item, {cache});
638
+ * })
639
+ */
640
+ service.canViewItem = function(user, item, options) {
641
+
642
+ options = options || {}
643
+ options.cache = options.cache || {}
644
+
645
+
646
+
647
+ //Check if we can view the item
648
+ var canView = service.checkActionAccess(user, item, 'viewany', 'viewOwn', options);
649
+ if (canView) {
650
+ return true;
651
+ }
652
+
653
+ //If we can edit the item then we should be able to view it also
654
+ var canEdit = service.canEditItem(user, item, options);
655
+ if (canEdit) {
656
+ return true;
657
+ }
658
+
659
+ return false;
660
+ }
661
+
662
+
663
+ ///////////////////////////////////////////////
664
+
665
+ /**
666
+ * @alias access.canEditItem
667
+ * @description Helpful function for checking whether a given user can edit a specified item
668
+ * @param {Object} user The user to check
669
+ * @param {Object} item The item to check
670
+ * @param {Object} options Additional options and parameters
671
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
672
+ * @example
673
+ * const user = sdk.auth.getCurrentUser();
674
+ *
675
+ * // Example of looping through a large array of items
676
+ * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
677
+ *
678
+ * // Create an in memory cache object outside of the loop
679
+ * const cache = {}
680
+ *
681
+ * const filtered = largeArrayOfItems.filter(function(item) {
682
+ * return sdk.access.canEditItem(user, item, {cache});
683
+ * })
684
+ */
685
+ service.canEditItem = function(user, item, options) {
686
+ options = options || {}
687
+ options.cache = options.cache || {}
688
+
689
+ var allowed = service.checkActionAccess(user, item, 'editany', 'editown', options);
690
+ return allowed;
691
+ }
692
+
693
+ ///////////////////////////////////////////////
694
+
695
+ /**
696
+ * @alias access.canDeleteItem
697
+ * @description Helpful function for checking whether a given user can delete a specified item
698
+ * @param {Object} user The user to check
699
+ * @param {Object} item The item to check
700
+ * @param {Object} options Additional options and parameters
701
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
702
+ * @example
703
+ * const user = sdk.auth.getCurrentUser();
704
+ *
705
+ * // Example of looping through a large array of items
706
+ * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
707
+ *
708
+ * // Create an in memory cache object outside of the loop
709
+ * const cache = {}
710
+ *
711
+ * const filtered = largeArrayOfItems.filter(function(item) {
712
+ * return sdk.access.canDeleteItem(user, item, {cache});
713
+ * })
714
+ */
715
+ service.canDeleteItem = function(user, item, options) {
716
+ options = options || {}
717
+ options.cache = options.cache || {}
718
+
719
+ return service.checkActionAccess(user, item, 'deleteany', 'deleteown', options);
720
+ }
721
+
722
+ ///////////////////////////////////////////////
723
+
724
+ /**
725
+ * @alias access.canRestoreItem
726
+ * @description Helpful function for checking whether a given user can restore a specified item
727
+ * @param {Object} user The user to check
728
+ * @param {Object} item The item to check
729
+ * @param {Object} options Additional options and parameters
730
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
731
+ * @example
732
+ * const user = sdk.auth.getCurrentUser();
733
+ *
734
+ * // Example of looping through a large array of items
735
+ * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
736
+ *
737
+ * // Create an in memory cache object outside of the loop
738
+ * const cache = {}
739
+ *
740
+ * const filtered = largeArrayOfItems.filter(function(item) {
741
+ * return sdk.access.canRestoreItem(user, item, {cache});
742
+ * })
743
+ */
744
+ service.canRestoreItem = function(user, item, options) {
745
+ options = options || {}
746
+ options.cache = options.cache || {}
747
+
748
+ return service.checkActionAccess(user, item, 'restoreany', 'restoreown', options);
749
+ }
750
+
751
+ ///////////////////////////////////////////////
752
+
753
+ /**
754
+ * @alias access.canEraseItem
755
+ * @description Helpful function for checking whether a given user can erase a specified item
756
+ * @param {Object} user The user to check
757
+ * @param {Object} item The item to check
758
+ * @param {Object} options Additional options and parameters
759
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
760
+ * @example
761
+ * const user = sdk.auth.getCurrentUser();
762
+ *
763
+ * // Example of looping through a large array of items
764
+ * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
765
+ *
766
+ * // Create an in memory cache object outside of the loop
767
+ * const cache = {}
768
+ *
769
+ * const filtered = largeArrayOfItems.filter(function(item) {
770
+ * return sdk.access.canEraseItem(user, item, {cache});
771
+ * })
772
+ */
773
+ service.canEraseItem = function(user, item, options) {
774
+ options = options || {}
775
+ options.cache = options.cache || {}
776
+
777
+ return service.checkActionAccess(user, item, 'eraseany', 'eraseown', options);
778
+ }
779
+
780
+
781
+
782
+ return service;
783
+
784
+ }
785
+
786
+ ///////////////////////////////////////////////////////////////////////////////
787
+
788
+
789
+
790
+ export default QikAccess;