@qikdev/sdk 0.1.181 → 0.1.183

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