@qikdev/sdk 0.1.168 → 0.1.169

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