@qikdev/sdk 1.0.12 → 1.0.14

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qikdev/sdk",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "type": "module",
5
5
  "description": "Promise based Javascript SDK",
6
6
  "main": "./dist/main.js",
@@ -1,11 +1,20 @@
1
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
2
+ * Permission and feature checks worked out locally from the signed-in user's session (no requests).
3
+ * Use them to decide what to show, e.g. whether to offer an Edit button; the server still checks every
4
+ * request itself, so these never grant access. Every check takes the session object
5
+ * (const { session } = sdk.auth.getCurrentUser()), not the whole stored { session, token } object.
6
+ * Permissions are strings of the form '<type or definition key>.<action>', e.g. 'profile.viewany',
7
+ * 'article.create', 'event.editown', granted within scopes. 'any' actions apply to every record in the scope,
8
+ * 'own' actions only to records the user owns or is assigned to. Administrators pass every check.
5
9
  * @alias access
6
10
  * @constructor
7
11
  * @hideconstructor
8
12
  * @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.
13
+ * @example
14
+ * const { session } = sdk.auth.getCurrentUser();
15
+ * if (sdk.access.canEditItem(session, record)) {
16
+ * // show the edit button
17
+ * }
9
18
  */
10
19
 
11
20
  var QikAccess = function (QikCore) {
@@ -28,13 +37,14 @@ var QikAccess = function (QikCore) {
28
37
  ///////////////////////////////////////////////
29
38
 
30
39
  /**
40
+ * Checks whether a feature is switched on for the user's organisation (session.organisation.features).
31
41
  * @alias access.hasFeature
32
- * @description Checks if a user has a specific feature enabled for their organisation
33
- * @param {Object} user The user to check
34
- * @param {String} feature The feature to check is enabled/disabled
42
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
43
+ * @param {String} feature The feature key, e.g. 'forms', 'sms', 'messaging'.
44
+ * @returns {Boolean} True when the feature is on (or the organisation has every feature).
35
45
  * @example
36
- * const user = sdk.auth.getCurrentUser();
37
- * const formsFeatureIsEnabled = sdk.access.hasFeature(user, 'forms');
46
+ * const { session } = sdk.auth.getCurrentUser();
47
+ * const formsEnabled = sdk.access.hasFeature(session, 'forms');
38
48
  */
39
49
  service.hasFeature = function (user, feature) {
40
50
  const features = user?.organisation?.features || {};
@@ -47,11 +57,13 @@ var QikAccess = function (QikCore) {
47
57
  ///////////////////////////////////////////////
48
58
 
49
59
  /**
60
+ * Checks whether the session is an administrator's (userType 'administrator'). Administrators pass every permission check.
50
61
  * @alias access.isAdministrator
51
- * @description Check if a user object is an administrator
52
- * @param {Object} user The user session to check
62
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
63
+ * @returns {Boolean} True for an administrator; a falsy value otherwise (including when no session is given).
53
64
  * @example
54
- * const userIsAnAdministrator = sdk.access.isAdministrator({firstName:'Jeffrey', lastName:'Winger', ...});
65
+ * const { session } = sdk.auth.getCurrentUser();
66
+ * const isAdmin = sdk.access.isAdministrator(session);
55
67
  */
56
68
  service.isAdministrator = function (user) {
57
69
  return user && user?.userType == "administrator";
@@ -60,11 +72,13 @@ var QikAccess = function (QikCore) {
60
72
  ///////////////////////////////////////////////
61
73
 
62
74
  /**
75
+ * Collects the ids of a scope and every scope nested inside it (through `children`).
63
76
  * @alias access.getAllDescendants
64
- * @description Get all children of a specified scope
65
- * @param {Object} scope A scope object with child scopes
77
+ * @param {Object} scope A scope with nested `children` arrays, e.g. an entry of session.permissions.
78
+ * @returns {Array<String>} The scope's own id followed by the ids of all its descendants, without duplicates.
66
79
  * @example
67
- * const childScopes = sdk.access.getAllDescendants({title:'Global', children:[{title:'Australia', ...}, {title:'New Zealand', ...}]});
80
+ * const ids = sdk.access.getAllDescendants({ _id: '61eca4746971e75c1fc670cf', children: [{ _id: '61eca4746971e75c1fc670ca', children: [] }] });
81
+ * // ['61eca4746971e75c1fc670cf', '61eca4746971e75c1fc670ca']
68
82
  */
69
83
  service.getAllDescendants = function (scope) {
70
84
  //Start by including the scope id. A Set because the same scope can be
@@ -89,11 +103,15 @@ var QikAccess = function (QikCore) {
89
103
  ///////////////////////////////////////////////
90
104
 
91
105
  /**
106
+ * Builds a lookup of every permission the user holds and the scopes it applies in, including the scopes
107
+ * nested under the ones it was granted in. The result is remembered for a few seconds per session.
92
108
  * @alias access.hashPermissions
93
- * @description Get a lookup of all permissions a user has been granted
94
- * @param {Object} user A user session object
109
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
110
+ * @returns {Object} An object mapping each permission string to an array of scope ids, e.g. { 'profile.viewany': ['61eca...', ...] }.
95
111
  * @example
96
- * const permissions = sdk.access.hashPermissions({firstName:'Mighty', lastName:'Mouse', permissionSets:[...], ...});
112
+ * const { session } = sdk.auth.getCurrentUser();
113
+ * const lookup = sdk.access.hashPermissions(session);
114
+ * const scopesWhereICanCreateArticles = lookup['article.create'] || [];
97
115
  */
98
116
  // Memo for hashPermissions, keyed on the session's cacheKey.
99
117
  //
@@ -175,14 +193,14 @@ var QikAccess = function (QikCore) {
175
193
  ///////////////////////////////////////////////
176
194
 
177
195
  /**
196
+ * Lists the ids of every scope the user holds any permission in, including nested scopes.
178
197
  * @alias access.allUserScopes
179
- * @description Return an array of ids of all scopes that a given user has been granted a permission in
180
- * @param {Object} user A user session object
198
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
199
+ * @returns {Array<String>} Scope ids.
181
200
  * @example
182
- * const permissions = sdk.access.allUserScopes({firstName:'Mighty', lastName:'Mouse', permissionSets:[...], ...});
183
- *
184
- * // Would result in:
185
- * ['61eca4746971e75c1fc670cf', '61eca4746971e75c1fc670ca', '77eca4746971e75c1fc670cf'],
201
+ * const { session } = sdk.auth.getCurrentUser();
202
+ * const scopeIDs = sdk.access.allUserScopes(session);
203
+ * // ['61eca4746971e75c1fc670cf', '61eca4746971e75c1fc670ca', ...]
186
204
  */
187
205
  service.allUserScopes = function (user) {
188
206
  var collected = {};
@@ -225,20 +243,20 @@ var QikAccess = function (QikCore) {
225
243
  ///////////////////////////////////////////////
226
244
 
227
245
  /**
246
+ * Lists the scopes in which the user may perform an action on a type of record, e.g. where they may create
247
+ * articles. Checks the permission for the definition key and for the base type key and combines the scopes.
248
+ * For administrators, every scope they hold a permission in.
228
249
  * @alias access.actionableScopes
229
- * @description Return an array of all scopes that a given user can perform a specified action in
230
- * @param {Object} user A user session object
231
- * @param {String} action The action
232
- * @param {String} definition The defined type of item the action will be performed on
233
- * @param {String} type The basic type of item the action will be performed on
234
- * @param {Object} options Additional options and parameters
250
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
251
+ * @param {String} action The action: 'create', 'viewany', 'viewown', 'listany', 'editany', 'editown', 'deleteany', 'deleteown', 'restoreany', 'eraseany'...
252
+ * @param {String} [definition] The definition key, when checking a defined type (e.g. 'jobApplication'); pass null or undefined for a base type.
253
+ * @param {String} [type] The base type key (e.g. 'article', 'profile').
254
+ * @param {Object} [options] Not used; accepted for compatibility.
255
+ * @returns {Array<String>} Scope ids (may contain duplicates). Empty when the user can't do it anywhere, or when neither definition nor type is given.
235
256
  * @example
236
- * const user = sdk.auth.getCurrentUser();
237
- * const canCreateInScopes = sdk.access.actionableScopes(user, 'create', 'car', 'article');
238
- * const canDeleteInScopes = sdk.access.actionableScopes(user, 'delete', 'profile');
239
- *
240
- * // Would result in something like:
241
- * ['61eca4746971e75c1fc670cf', '61eca4746971e75c1fc670ca', '77eca4746971e75c1fc670cf'],
257
+ * const { session } = sdk.auth.getCurrentUser();
258
+ * const createScopes = sdk.access.actionableScopes(session, 'create', 'jobApplication', 'object');
259
+ * const deleteScopes = sdk.access.actionableScopes(session, 'deleteany', null, 'profile');
242
260
  */
243
261
  service.actionableScopes = function (
244
262
  user,
@@ -281,16 +299,16 @@ var QikAccess = function (QikCore) {
281
299
  ///////////////////////////////////////////////
282
300
 
283
301
  /**
302
+ * Checks whether the user counts as an owner of a record: they created it (meta.userAuthor / meta.personaAuthor),
303
+ * are listed as an owner (meta.userOwners / meta.personaOwners), or one of their profiles is in its `assignees`.
304
+ * Ownership is what 'own' permissions (e.g. 'article.editown') apply to.
284
305
  * @alias access.isOwner
285
- * @description Return whether or not a user is considered an Owner of a specified item
286
- * @param {Object} user The user to check
287
- * @param {Object} item The item
306
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
307
+ * @param {Object} item The record, with its `meta` (and `assignees` if it has them).
308
+ * @returns {Boolean} True when the user owns the record.
288
309
  * @example
289
- * const user = sdk.auth.getCurrentUser();
290
- * const item = {title:'A piece of content', meta:{personaAuthor:'61eca4746971e75c1fc670ca'}, ...}
291
- *
292
- * // Returns true if the user owns the item
293
- * const isTheOwner = sdk.access.isOwner(user, item);
310
+ * const { session } = sdk.auth.getCurrentUser();
311
+ * const mine = sdk.access.isOwner(session, { title: 'A post', meta: { personaAuthor: '61eca4746971e75c1fc670ca' } });
294
312
  */
295
313
  service.isOwner = function (user, item) {
296
314
  var userID = QikCore.utils.id(user);
@@ -332,16 +350,14 @@ var QikAccess = function (QikCore) {
332
350
  };
333
351
 
334
352
  /**
353
+ * Checks whether one of the user's profiles (session.profiles) is in the record's `assignees`, e.g. a workflow card assigned to them.
335
354
  * @alias access.isAssignee
336
- * @description Return whether or not a user is an assignee of a specified item
337
- * @param {Object} user The user to check
338
- * @param {Object} item The item
355
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
356
+ * @param {Object} contentItem The record, with an `assignees` array of profile ids or objects.
357
+ * @returns {Boolean} True when the user is an assignee.
339
358
  * @example
340
- * const user = sdk.auth.getCurrentUser();
341
- * const item = {title:'A workflow card', assignees:['61eca4746971e75c1fc670ca'], ...}
342
- *
343
- * // Returns true if the user owns the item
344
- * const isAssignee = sdk.access.isAssignee(user, item);
359
+ * const { session } = sdk.auth.getCurrentUser();
360
+ * const assignedToMe = sdk.access.isAssignee(session, { title: 'Follow up', assignees: ['61eca4746971e75c1fc670ca'] });
345
361
  */
346
362
  service.isAssignee = function (user, contentItem) {
347
363
  // Check the user profile ids
@@ -368,15 +384,14 @@ var QikAccess = function (QikCore) {
368
384
  ///////////////////////////////////////////////
369
385
 
370
386
  /**
387
+ * Lists every permission string the user has been granted, across all scopes.
371
388
  * @alias access.allPermissions
372
- * @description Returns an array of all permissions a given user has access to
373
- * @param {Object} user The user to check
389
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
390
+ * @returns {Array<String>} Permission strings (the same one can appear once per scope it was granted in); empty when no session is given.
374
391
  * @example
375
- * const user = sdk.auth.getCurrentUser();
376
- * const permissions = sdk.access.allPermissions(user);
377
- *
378
- * // Returns
379
- * ['image.viewany', 'testimonial.viewany', 'testimonial.viewfield.title', ...]
392
+ * const { session } = sdk.auth.getCurrentUser();
393
+ * const permissions = sdk.access.allPermissions(session);
394
+ * // ['image.viewany', 'testimonial.viewany', 'testimonial.viewfield.title', ...]
380
395
  */
381
396
  service.allPermissions = function (user) {
382
397
  var allPermissions = Object.values(user?.permissions || {})
@@ -391,19 +406,15 @@ var QikAccess = function (QikCore) {
391
406
  ///////////////////////////////////////////////
392
407
 
393
408
  /**
409
+ * Lists the type and definition keys the user holds at least one permission for.
394
410
  * @alias access.allPermissionTypes
395
- * @description Returns an array of content types a given user has permission to interact with
396
- * @param {Object} user The user to check
397
- * @param {Boolean} asHash Whether to retrieve the result as a keyed object hash, by default
398
- * the response will be an array
411
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
412
+ * @param {Boolean} [asHash] Return an object { key: true } instead of an array.
413
+ * @returns {Array<String>|Object} The keys (the array can repeat keys), or an object keyed by them.
399
414
  * @example
400
- * const user = sdk.auth.getCurrentUser();
401
- *
402
- * // Returns ['image', 'testimonial']
403
- * const types = sdk.access.allPermissionTypes(user);
404
- *
405
- * // Returns {image:true, testimonial:true}
406
- * const types = sdk.access.allPermissionTypes(user, true);
415
+ * const { session } = sdk.auth.getCurrentUser();
416
+ * const types = sdk.access.allPermissionTypes(session); // ['image', 'testimonial', ...]
417
+ * const lookup = sdk.access.allPermissionTypes(session, true); // { image: true, testimonial: true }
407
418
  */
408
419
  service.allPermissionTypes = function (user, asHash) {
409
420
  if (asHash) {
@@ -422,16 +433,15 @@ var QikAccess = function (QikCore) {
422
433
  ///////////////////////////////////////////////
423
434
 
424
435
  /**
436
+ * Checks whether the user holds any permission at all for a type or definition, e.g. to decide whether to show its menu entry.
425
437
  * @alias access.canKnowOf
426
- * @description Checks if a user has any permissions relating to a specified type of content
427
- * @param {Object} user The user to check
428
- * @param {String} typeKey The type or definition to check
429
- * @param {Object} options Additional options and parameters
430
- * @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
438
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
439
+ * @param {String} typeKey The type or definition key.
440
+ * @param {Object} [options] Not used; accepted for compatibility.
441
+ * @returns {Boolean|undefined} true when they hold a permission for it, otherwise undefined.
431
442
  * @example
432
- * const user = sdk.auth.getCurrentUser();
433
- * const canAccessImageSection = sdk.access.canKnowOf(user, 'image');
434
- * const canAccessCustomerUploadSection = sdk.access.canKnowOf(user, 'customerUpload');
443
+ * const { session } = sdk.auth.getCurrentUser();
444
+ * const showImages = !!sdk.access.canKnowOf(session, 'image');
435
445
  */
436
446
  service.canKnowOf = function (user, typeKey, options) {
437
447
  options = options || {};
@@ -444,28 +454,21 @@ var QikAccess = function (QikCore) {
444
454
  ///////////////////////////////////////////////
445
455
 
446
456
  /**
457
+ * The general check behind canViewItem, canEditItem and the others: may the user perform an action on a
458
+ * specific record? True when the user is an administrator, when they hold the 'any' action for the
459
+ * record's definition or type in one of the record's scopes, or when they own the record and hold the
460
+ * 'own' action in one of its scopes. For scope records the scope's own path is used as its scopes.
447
461
  * @alias access.checkActionAccess
448
- * @description Helpful function for checking whether a given user has access to do a given action on a specified item
449
- * @param {Object} user The user to check
450
- * @param {Object} item The item to check
451
- * @param {String} ANY_ACTION The 'any' action to check
452
- * @param {String} OWN_ACTION The 'owned' action to check
453
- * @param {Object} options Additional options and parameters
454
- * @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
462
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
463
+ * @param {Object} item The record, with meta.type, meta.definition and meta.scopes (and owner fields).
464
+ * @param {String} ANY_ACTION The action that applies to any record, e.g. 'editany'.
465
+ * @param {String} OWN_ACTION The action that applies to records the user owns, e.g. 'editown'.
466
+ * @param {Object} [options] Options.
467
+ * @param {Object} [options.cache] An object to reuse across calls (e.g. {} created before a loop) so repeated checks for the same type are faster.
468
+ * @returns {Boolean} True when allowed. False when the record has no meta.type.
455
469
  * @example
456
- * const user = sdk.auth.getCurrentUser();
457
- * const canEditItem = sdk.access.checkActionAccess(user, {_id:'61eca4746971e75c1fc670ca', meta:{...}}, 'editany', 'editown');
458
- * const canViewItem = sdk.access.checkActionAccess(user, {_id:'61eca4746971e75c1fc670ca', meta:{...}}, 'viewany', 'viewown');
459
- *
460
- * // Example of looping through a large array of items
461
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
462
- *
463
- * // Create an in memory cache object outside of the loop
464
- * const cache = {}
465
- *
466
- * const filtered = largeArrayOfItems.filter(function(item) {
467
- * return sdk.access.checkActionAccess(user, item, 'editany', 'editown', {cache});
468
- * })
470
+ * const { session } = sdk.auth.getCurrentUser();
471
+ * const canEdit = sdk.access.checkActionAccess(session, record, 'editany', 'editown');
469
472
  */
470
473
  service.checkActionAccess = function (
471
474
  user,
@@ -592,13 +595,16 @@ var QikAccess = function (QikCore) {
592
595
  ///////////////////////////////////////////////
593
596
 
594
597
  /**
598
+ * Checks whether the user holds a specific permission in at least one scope.
595
599
  * @alias access.has
596
- * @description Helpful function for checking whether a given user has a specified permission
597
- * @param {Object} user The user to check
598
- * @param {String} permission The permission the user may have been granted
600
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
601
+ * @param {String} permission The permission string, e.g. 'video.create' or 'profile.viewany'.
602
+ * @returns {Boolean|Array<String>} true for administrators; otherwise the array of scope ids the permission applies in (truthy) or undefined when it isn't held.
599
603
  * @example
600
- * const user = sdk.auth.getCurrentUser();
601
- * const isAllowedToCreateItems = sdk.access.has(user, 'video.create');
604
+ * const { session } = sdk.auth.getCurrentUser();
605
+ * if (sdk.access.has(session, 'video.create')) {
606
+ * // show the upload button
607
+ * }
602
608
  */
603
609
  service.has = function (user, permission) {
604
610
  //Allow everything if we're a super user
@@ -613,16 +619,16 @@ var QikAccess = function (QikCore) {
613
619
  ///////////////////////////////////////////////
614
620
 
615
621
  /**
622
+ * Checks whether the user may create records of a type in at least one scope.
616
623
  * @alias access.canCreate
617
- * @description Helpful function for checking whether a given user can create a specified type of item
618
- * @param {Object} user The user to check
619
- * @param {String} definition The defined type key
620
- * @param {String} type The basic type key
621
- * @param {Object} options Additional options and parameters
622
- * @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
624
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
625
+ * @param {String} [definition] The definition key when creating a defined type; null or undefined for a base type.
626
+ * @param {String} [type] The base type key.
627
+ * @param {Object} [options] Not used; accepted for compatibility.
628
+ * @returns {Boolean|Number} true for administrators; otherwise the number of scopes they can create in (0 = no).
623
629
  * @example
624
- * const user = sdk.auth.getCurrentUser();
625
- * const userCanCreateShortFilms = sdk.access.canCreate(user, 'shortFilm', 'video');
630
+ * const { session } = sdk.auth.getCurrentUser();
631
+ * const canCreateShortFilms = !!sdk.access.canCreate(session, 'shortFilm', 'video');
626
632
  */
627
633
  service.canCreate = function (user, definition, type, options) {
628
634
  options = options || {};
@@ -645,24 +651,19 @@ var QikAccess = function (QikCore) {
645
651
  ///////////////////////////////////////////////
646
652
 
647
653
  /**
654
+ * Checks whether the user may see a record in lists: 'listany'/'listown' permission, or anything that lets them view it. Administrators always may.
648
655
  * @alias access.canListItem
649
- * @description Helpful function for checking whether a given user can list a specified item
650
- * @param {Object} user The user to check
651
- * @param {Object} item The item to check
652
- * @param {Object} options Additional options and parameters
653
- * @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
656
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
657
+ * @param {Object} item The record, with meta.type, meta.definition and meta.scopes (and owner fields).
658
+ * @param {Object} [options] Options.
659
+ * @param {Object} [options.cache] An object to reuse across calls (e.g. {} created before a loop) so repeated checks for the same type are faster.
660
+ * @returns {Boolean} True when allowed.
654
661
  * @example
655
- * const user = sdk.auth.getCurrentUser();
656
- *
657
- * // Example of looping through a large array of items
658
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
662
+ * const { session } = sdk.auth.getCurrentUser();
659
663
  *
660
- * // Create an in memory cache object outside of the loop
661
- * const cache = {}
662
- *
663
- * const filtered = largeArrayOfItems.filter(function(item) {
664
- * return sdk.access.canListItem(user, item, {cache});
665
- * })
664
+ * // Checking many items: share one cache object across the loop
665
+ * const cache = {};
666
+ * const allowed = items.filter((item) => sdk.access.canListItem(session, item, { cache }));
666
667
  */
667
668
  service.canListItem = function (user, item, options) {
668
669
  options = options || {};
@@ -693,24 +694,19 @@ var QikAccess = function (QikCore) {
693
694
  ///////////////////////////////////////////////
694
695
 
695
696
  /**
697
+ * Checks whether the user may open a record: 'viewany' permission in one of its scopes, 'viewown' if they own it, or permission to edit it. Administrators always may.
696
698
  * @alias access.canViewItem
697
- * @description Helpful function for checking whether a given user can view 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
699
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
700
+ * @param {Object} item The record, with meta.type, meta.definition and meta.scopes (and owner fields).
701
+ * @param {Object} [options] Options.
702
+ * @param {Object} [options.cache] An object to reuse across calls (e.g. {} created before a loop) so repeated checks for the same type are faster.
703
+ * @returns {Boolean} True when allowed.
702
704
  * @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 = {}
705
+ * const { session } = sdk.auth.getCurrentUser();
710
706
  *
711
- * const filtered = largeArrayOfItems.filter(function(item) {
712
- * return sdk.access.canViewItem(user, item, {cache});
713
- * })
707
+ * // Checking many items: share one cache object across the loop
708
+ * const cache = {};
709
+ * const allowed = items.filter((item) => sdk.access.canViewItem(session, item, { cache }));
714
710
  */
715
711
  service.canViewItem = function (user, item, options) {
716
712
  options = options || {};
@@ -721,7 +717,7 @@ var QikAccess = function (QikCore) {
721
717
  user,
722
718
  item,
723
719
  "viewany",
724
- "viewOwn",
720
+ "viewown",
725
721
  options,
726
722
  );
727
723
  if (canView) {
@@ -740,24 +736,19 @@ var QikAccess = function (QikCore) {
740
736
  ///////////////////////////////////////////////
741
737
 
742
738
  /**
739
+ * Checks whether the user may edit a record: 'editany' in one of its scopes, or 'editown' if they own it. Administrators always may.
743
740
  * @alias access.canEditItem
744
- * @description Helpful function for checking whether a given user can edit a specified item
745
- * @param {Object} user The user to check
746
- * @param {Object} item The item to check
747
- * @param {Object} options Additional options and parameters
748
- * @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
741
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
742
+ * @param {Object} item The record, with meta.type, meta.definition and meta.scopes (and owner fields).
743
+ * @param {Object} [options] Options.
744
+ * @param {Object} [options.cache] An object to reuse across calls (e.g. {} created before a loop) so repeated checks for the same type are faster.
745
+ * @returns {Boolean} True when allowed.
749
746
  * @example
750
- * const user = sdk.auth.getCurrentUser();
747
+ * const { session } = sdk.auth.getCurrentUser();
751
748
  *
752
- * // Example of looping through a large array of items
753
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
754
- *
755
- * // Create an in memory cache object outside of the loop
756
- * const cache = {}
757
- *
758
- * const filtered = largeArrayOfItems.filter(function(item) {
759
- * return sdk.access.canEditItem(user, item, {cache});
760
- * })
749
+ * // Checking many items: share one cache object across the loop
750
+ * const cache = {};
751
+ * const allowed = items.filter((item) => sdk.access.canEditItem(session, item, { cache }));
761
752
  */
762
753
  service.canEditItem = function (user, item, options) {
763
754
  options = options || {};
@@ -777,24 +768,19 @@ var QikAccess = function (QikCore) {
777
768
  ///////////////////////////////////////////////
778
769
 
779
770
  /**
771
+ * Checks whether the user may delete (trash) a record: 'deleteany' in one of its scopes, or 'deleteown' if they own it. Administrators always may.
780
772
  * @alias access.canDeleteItem
781
- * @description Helpful function for checking whether a given user can delete a specified item
782
- * @param {Object} user The user to check
783
- * @param {Object} item The item to check
784
- * @param {Object} options Additional options and parameters
785
- * @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
773
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
774
+ * @param {Object} item The record, with meta.type, meta.definition and meta.scopes (and owner fields).
775
+ * @param {Object} [options] Options.
776
+ * @param {Object} [options.cache] An object to reuse across calls (e.g. {} created before a loop) so repeated checks for the same type are faster.
777
+ * @returns {Boolean} True when allowed.
786
778
  * @example
787
- * const user = sdk.auth.getCurrentUser();
788
- *
789
- * // Example of looping through a large array of items
790
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
779
+ * const { session } = sdk.auth.getCurrentUser();
791
780
  *
792
- * // Create an in memory cache object outside of the loop
793
- * const cache = {}
794
- *
795
- * const filtered = largeArrayOfItems.filter(function(item) {
796
- * return sdk.access.canDeleteItem(user, item, {cache});
797
- * })
781
+ * // Checking many items: share one cache object across the loop
782
+ * const cache = {};
783
+ * const allowed = items.filter((item) => sdk.access.canDeleteItem(session, item, { cache }));
798
784
  */
799
785
  service.canDeleteItem = function (user, item, options) {
800
786
  options = options || {};
@@ -812,24 +798,19 @@ var QikAccess = function (QikCore) {
812
798
  ///////////////////////////////////////////////
813
799
 
814
800
  /**
801
+ * Checks whether the user may restore a trashed record: 'restoreany' in one of its scopes, or 'restoreown' if they own it. Administrators always may.
815
802
  * @alias access.canRestoreItem
816
- * @description Helpful function for checking whether a given user can restore a specified item
817
- * @param {Object} user The user to check
818
- * @param {Object} item The item to check
819
- * @param {Object} options Additional options and parameters
820
- * @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
803
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
804
+ * @param {Object} item The record, with meta.type, meta.definition and meta.scopes (and owner fields).
805
+ * @param {Object} [options] Options.
806
+ * @param {Object} [options.cache] An object to reuse across calls (e.g. {} created before a loop) so repeated checks for the same type are faster.
807
+ * @returns {Boolean} True when allowed.
821
808
  * @example
822
- * const user = sdk.auth.getCurrentUser();
823
- *
824
- * // Example of looping through a large array of items
825
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
826
- *
827
- * // Create an in memory cache object outside of the loop
828
- * const cache = {}
809
+ * const { session } = sdk.auth.getCurrentUser();
829
810
  *
830
- * const filtered = largeArrayOfItems.filter(function(item) {
831
- * return sdk.access.canRestoreItem(user, item, {cache});
832
- * })
811
+ * // Checking many items: share one cache object across the loop
812
+ * const cache = {};
813
+ * const allowed = items.filter((item) => sdk.access.canRestoreItem(session, item, { cache }));
833
814
  */
834
815
  service.canRestoreItem = function (user, item, options) {
835
816
  options = options || {};
@@ -847,24 +828,19 @@ var QikAccess = function (QikCore) {
847
828
  ///////////////////////////////////////////////
848
829
 
849
830
  /**
831
+ * Checks whether the user may permanently erase a record: 'eraseany' in one of its scopes, or 'eraseown' if they own it. Administrators always may.
850
832
  * @alias access.canEraseItem
851
- * @description Helpful function for checking whether a given user can erase a specified item
852
- * @param {Object} user The user to check
853
- * @param {Object} item The item to check
854
- * @param {Object} options Additional options and parameters
855
- * @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
833
+ * @param {Object} user The session to check: the `session` part of sdk.auth.getCurrentUser(), not the whole { session, token } object.
834
+ * @param {Object} item The record, with meta.type, meta.definition and meta.scopes (and owner fields).
835
+ * @param {Object} [options] Options.
836
+ * @param {Object} [options.cache] An object to reuse across calls (e.g. {} created before a loop) so repeated checks for the same type are faster.
837
+ * @returns {Boolean} True when allowed.
856
838
  * @example
857
- * const user = sdk.auth.getCurrentUser();
858
- *
859
- * // Example of looping through a large array of items
860
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
861
- *
862
- * // Create an in memory cache object outside of the loop
863
- * const cache = {}
839
+ * const { session } = sdk.auth.getCurrentUser();
864
840
  *
865
- * const filtered = largeArrayOfItems.filter(function(item) {
866
- * return sdk.access.canEraseItem(user, item, {cache});
867
- * })
841
+ * // Checking many items: share one cache object across the loop
842
+ * const cache = {};
843
+ * const allowed = items.filter((item) => sdk.access.canEraseItem(session, item, { cache }));
868
844
  */
869
845
  service.canEraseItem = function (user, item, options) {
870
846
  options = options || {};