@qikdev/sdk 1.0.12 → 1.0.13
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/dist/main.js +796 -759
- package/package.json +1 -1
- package/src/api/qik.access.js +177 -201
- package/src/api/qik.api.js +55 -28
- package/src/api/qik.auth.js +181 -113
- package/src/api/qik.cache.js +29 -7
- package/src/api/qik.content.js +275 -147
- package/src/api/qik.core.js +19 -28
- package/src/api/qik.files.js +48 -38
- package/src/api/qik.filter.js +148 -207
- package/src/api/qik.geo.js +59 -42
- package/src/api/qik.socket.js +117 -43
- package/src/api/qik.storage.js +17 -8
- package/src/api/qik.system.js +6 -5
- package/src/api/qik.utils.js +244 -199
- package/src/version.js +1 -1
package/package.json
CHANGED
package/src/api/qik.access.js
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
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
|
-
* @
|
|
33
|
-
* @param
|
|
34
|
-
* @
|
|
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
|
|
37
|
-
* const
|
|
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
|
-
* @
|
|
52
|
-
* @
|
|
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
|
|
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
|
-
* @
|
|
65
|
-
* @
|
|
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
|
|
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
|
-
* @
|
|
94
|
-
* @
|
|
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
|
|
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
|
-
* @
|
|
180
|
-
* @
|
|
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
|
|
183
|
-
*
|
|
184
|
-
* //
|
|
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
|
-
* @
|
|
230
|
-
* @param
|
|
231
|
-
* @param
|
|
232
|
-
* @param
|
|
233
|
-
* @param
|
|
234
|
-
* @
|
|
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
|
|
237
|
-
* const
|
|
238
|
-
* const
|
|
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
|
-
* @
|
|
286
|
-
* @param
|
|
287
|
-
* @
|
|
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
|
|
290
|
-
* const
|
|
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
|
-
* @
|
|
337
|
-
* @param
|
|
338
|
-
* @
|
|
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
|
|
341
|
-
* const
|
|
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
|
-
* @
|
|
373
|
-
* @
|
|
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
|
|
376
|
-
* const permissions =
|
|
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
|
-
* @
|
|
396
|
-
* @param
|
|
397
|
-
* @
|
|
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
|
|
401
|
-
*
|
|
402
|
-
* //
|
|
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
|
-
* @
|
|
427
|
-
* @param
|
|
428
|
-
* @param
|
|
429
|
-
* @
|
|
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
|
|
433
|
-
* const
|
|
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
|
-
* @
|
|
449
|
-
* @param
|
|
450
|
-
* @param
|
|
451
|
-
* @param
|
|
452
|
-
* @param
|
|
453
|
-
* @param
|
|
454
|
-
* @
|
|
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
|
|
457
|
-
* const
|
|
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
|
-
* @
|
|
597
|
-
* @param
|
|
598
|
-
* @
|
|
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
|
|
601
|
-
*
|
|
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
|
-
* @
|
|
618
|
-
* @param
|
|
619
|
-
* @param
|
|
620
|
-
* @param
|
|
621
|
-
* @
|
|
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
|
|
625
|
-
* const
|
|
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
|
-
* @
|
|
650
|
-
* @param
|
|
651
|
-
* @param
|
|
652
|
-
* @param
|
|
653
|
-
* @
|
|
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
|
|
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
|
-
* //
|
|
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
|
-
* @
|
|
698
|
-
* @param
|
|
699
|
-
* @param
|
|
700
|
-
* @param
|
|
701
|
-
* @
|
|
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
|
|
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
|
-
*
|
|
712
|
-
*
|
|
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
|
-
"
|
|
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
|
-
* @
|
|
745
|
-
* @param
|
|
746
|
-
* @param
|
|
747
|
-
* @param
|
|
748
|
-
* @
|
|
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
|
|
747
|
+
* const { session } = sdk.auth.getCurrentUser();
|
|
751
748
|
*
|
|
752
|
-
* //
|
|
753
|
-
* const
|
|
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
|
-
* @
|
|
782
|
-
* @param
|
|
783
|
-
* @param
|
|
784
|
-
* @param
|
|
785
|
-
* @
|
|
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
|
|
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
|
-
* //
|
|
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
|
-
* @
|
|
817
|
-
* @param
|
|
818
|
-
* @param
|
|
819
|
-
* @param
|
|
820
|
-
* @
|
|
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
|
|
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
|
-
*
|
|
831
|
-
*
|
|
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
|
-
* @
|
|
852
|
-
* @param
|
|
853
|
-
* @param
|
|
854
|
-
* @param
|
|
855
|
-
* @
|
|
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
|
|
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
|
-
*
|
|
866
|
-
*
|
|
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 || {};
|