@qikdev/sdk 0.1.170 → 0.1.181

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,3 +1,6 @@
1
+
2
+
3
+
1
4
  /**
2
5
  * Creates a new Access Module instance.
3
6
  * This module provides a number of helper functions for managing and understanding permissions of the current
@@ -8,818 +11,780 @@
8
11
  * @param {QikCore} qik A reference to the parent instance of the QikCore module. This module is usually created by a QikCore instance that passes itself in as the first argument.
9
12
  */
10
13
 
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;
14
+ var QikAccess = function(QikCore) {
15
+
16
+ if (!QikCore.auth) {
17
+ throw new Error(`Can't Instantiate QikAccess before QikAccess has been initialized`);
81
18
  }
82
19
 
83
- if (features[feature]) {
84
- return true;
20
+ if (!QikCore.utils) {
21
+ throw new Error(`Can't Instantiate QikAccess before QikCore.utils has been initialized`);
85
22
  }
86
23
 
87
- // Check the byteflag
88
- const byteflag = organisation.ff;
24
+ //////////////////////////////////
25
+
26
+ var service = {};
27
+
28
+ ///////////////////////////////////////////////
89
29
 
90
- if (service.featureIsIncludedInByteflag(byteflag, "all")) {
91
- return true;
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
92
55
  }
93
56
 
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
- });
57
+ service.featureIsIncludedInByteflag = function(byteflag, feature) {
58
+ return (byteflag & service.featureFlags[feature]) === service.featureFlags[feature];
134
59
  }
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] = [];
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;
162
90
  }
163
- results[permissionString] = [
164
- ...new Set(results[permissionString].concat(allIDs)),
165
- ];
166
- });
91
+
92
+ //Let us know if there are any permissions for this user
93
+ return service.featureIsIncludedInByteflag(byteflag, feature)
167
94
  }
168
95
 
169
96
  ///////////////////////////////////////////////
170
97
 
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
- }
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';
211
107
  }
212
108
 
213
- ////////////////////////
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
+
214
136
 
215
- for (var scopeID in permissions) {
216
- pluckScope(permissions[scopeID]);
217
137
  }
218
138
 
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];
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
+
267
174
  }
268
175
 
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;
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);
295
227
  }
296
228
 
297
- /////////////////////////////////////////////
229
+ ///////////////////////////////////////////////
298
230
 
299
- var userOwners = metaObject.userOwners || [];
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
+ }
300
267
 
301
- var users = QikCore.utils.ids([...userOwners, metaObject.userAuthor]);
302
- var userHash = QikCore.utils.hash(users);
268
+ /////////////////////////
303
269
 
304
- if (userHash[userID]) {
305
- return true;
270
+ return validScopes || []
306
271
  }
307
272
 
308
- /////////////////////////////////////////////
309
273
 
310
- var personaOwners = metaObject.personaOwners || [];
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 || [];
311
301
 
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)
302
+ var users = QikCore.utils.ids([...userOwners, metaObject.userAuthor]);
303
+ var userHash = QikCore.utils.hash(users);
304
+
305
+ if (userHash[userID]) {
306
+ return true;
307
+ }
308
+
309
+ /////////////////////////////////////////////
310
+
311
+ var personaOwners = metaObject.personaOwners || [];
312
+
313
+ var personas = QikCore.utils.ids([...personaOwners, metaObject.personaAuthor]);
314
+ var personaHash = QikCore.utils.hash(personas);
315
+ // console.log('PERSONAS', personas, personaHash, '-', metaObject.personaAuthor, metaObject.personaOwners)
316
+
317
+ if (personaHash[personaID]) {
318
+ return true;
319
+ }
318
320
 
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
- 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
- });
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;
375
346
  }
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;
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
+
439
378
  }
440
379
 
441
- //////////////////////////////////
380
+ ///////////////////////////////////////////////
442
381
 
443
- var type = item.meta.type;
444
- var definition = item.meta.definition || type;
382
+ /**
383
+ * @alias access.canKnowOf
384
+ * @description Checks if a user has any permissions relating to a specified type of content
385
+ * @param {Object} user The user to check
386
+ * @param {String} typeKey The type or definition to check
387
+ * @param {Object} options Additional options and parameters
388
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
389
+ * @example
390
+ * const user = sdk.auth.getCurrentUser();
391
+ * const canAccessImageSection = sdk.access.canKnowOf(user, 'image');
392
+ * const canAccessCustomerUploadSection = sdk.access.canKnowOf(user, 'customerUpload');
393
+ */
394
+ service.canKnowOf = function(user, typeKey, options) {
395
+ options = options || {};
396
+ options.cache = options.cache || {};
397
+
398
+ //Let us know if there are any permissions for this user
399
+ return service.allPermissionTypes(user, true)[typeKey];
400
+ }
445
401
 
446
- //////////////////////////////////
447
402
 
448
- var itemScopes = item.meta.scopes || [];
403
+ ///////////////////////////////////////////////
449
404
 
450
- //////////////////////////////////
405
+ /**
406
+ * @alias access.checkActionAccess
407
+ * @description Helpful function for checking whether a given user has access to do a given action on a specified item
408
+ * @param {Object} user The user to check
409
+ * @param {Object} item The item to check
410
+ * @param {String} ANY_ACTION The 'any' action to check
411
+ * @param {String} OWN_ACTION The 'owned' action to check
412
+ * @param {Object} options Additional options and parameters
413
+ * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
414
+ * @example
415
+ * const user = sdk.auth.getCurrentUser();
416
+ * const canEditItem = sdk.access.checkActionAccess(user, {_id:'61eca4746971e75c1fc670ca', meta:{...}}, 'editany', 'editown');
417
+ * const canViewItem = sdk.access.checkActionAccess(user, {_id:'61eca4746971e75c1fc670ca', meta:{...}}, 'viewany', 'viewown');
418
+ *
419
+ * // Example of looping through a large array of items
420
+ * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
421
+ *
422
+ * // Create an in memory cache object outside of the loop
423
+ * const cache = {}
424
+ *
425
+ * const filtered = largeArrayOfItems.filter(function(item) {
426
+ * return sdk.access.checkActionAccess(user, item, 'editany', 'editown', {cache});
427
+ * })
428
+ */
429
+ service.checkActionAccess = function(user, item, ANY_ACTION, OWN_ACTION, options) {
430
+
431
+ options = options || {};
432
+ options.cache = options.cache || {};
433
+
434
+ //Allow everything if we're a super user
435
+ if (service.isAdministrator(user)) {
436
+ return true;
437
+ }
438
+
439
+ //////////////////////////////////
440
+
441
+ var type = item.meta.type;
442
+ var definition = item.meta.definition || type;
443
+
444
+ //////////////////////////////////
445
+
446
+ var itemScopes = item.meta.scopes || [];
447
+
448
+ //////////////////////////////////
449
+
450
+ var actionAnyScopesHash;
451
+ var anyCacheKey = `access-${user?.cacheKey}-${ANY_ACTION}-${definition}-${type}`
452
+
453
+ if (options.cache[anyCacheKey]) {
454
+ actionAnyScopesHash = options.cache[anyCacheKey];
455
+
456
+ } else {
457
+ //Check if we can view this type of thing in any scopes
458
+ var actionAnyScopes = service.actionableScopes(user, ANY_ACTION, definition, type, options);
451
459
 
452
- var actionAnyScopesHash;
453
- var anyCacheKey = `access-${user?.cacheKey}-${ANY_ACTION}-${definition}-${type}`;
454
-
455
- if (options.cache[anyCacheKey]) {
456
- actionAnyScopesHash = options.cache[anyCacheKey];
457
- } else {
458
- //Check if we can view this type of thing in any scopes
459
- var actionAnyScopes = service.actionableScopes(
460
- user,
461
- ANY_ACTION,
462
- definition,
463
- type,
464
- options,
465
- );
466
-
467
- //Get a fash hash of the scopes
468
- actionAnyScopesHash = QikCore.utils.hash(actionAnyScopes);
469
- options.cache[anyCacheKey] = actionAnyScopesHash;
460
+ //Get a fash hash of the scopes
461
+ actionAnyScopesHash = QikCore.utils.hash(actionAnyScopes);
462
+ options.cache[anyCacheKey] = actionAnyScopesHash;
463
+ }
464
+
465
+ //////////////////////////////////
466
+
467
+ //If we have data about what they can do
468
+ if (actionAnyScopesHash) {
469
+ //Check if the item is in any of the scopes the user can access
470
+ var isInAnyScopes = itemScopes.some(function(scopeID) {
471
+ return actionAnyScopesHash[scopeID];
472
+ })
473
+
474
+ if (isInAnyScopes) {
475
+ return true;
476
+ }
477
+ }
478
+
479
+ //////////////////////////////////
480
+
481
+ //Check if the user owns this content
482
+ var isOwner = service.isOwner(user, item);
483
+
484
+ //If the user is the owner
485
+ if (isOwner) {
486
+ var actionOwnScopesHash;
487
+ var ownCacheKey = `access-${user?.cacheKey}-${OWN_ACTION}-${definition}-${type}`
488
+
489
+ if (options.cache[ownCacheKey]) {
490
+ actionOwnScopesHash = options.cache[ownCacheKey];
491
+ } else {
492
+
493
+ //Check if we can view this type of thing in any scopes
494
+ var actionOwnScopes = service.actionableScopes(user, OWN_ACTION, definition, type, options);
495
+ //Get a fash hash of the scopes
496
+ actionOwnScopesHash = QikCore.utils.hash(actionOwnScopes);
497
+ options.cache[ownCacheKey] = actionOwnScopesHash;
498
+ }
499
+
500
+ //If we have data about what they can do
501
+ if (actionOwnScopesHash) {
502
+ //Check if the item is in any of the scopes the user can access
503
+ var isInOwnScopes = itemScopes.some(function(scopeID) {
504
+ return actionOwnScopesHash[scopeID];
505
+ })
506
+
507
+ if (isInOwnScopes) {
508
+ return true;
509
+ }
510
+ }
511
+ }
512
+
513
+ //////////////////////////////////
514
+
515
+ //No access by default
516
+ return false
470
517
  }
471
518
 
472
- //////////////////////////////////
473
519
 
474
- //If we have data about what they can do
475
- if (actionAnyScopesHash) {
476
- //Check if the item is in any of the scopes the user can access
477
- var isInAnyScopes = itemScopes.some(function (scopeID) {
478
- return actionAnyScopesHash[scopeID];
479
- });
520
+ ///////////////////////////////////////////////
480
521
 
481
- if (isInAnyScopes) {
482
- return true;
483
- }
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];
484
540
  }
485
541
 
486
- //////////////////////////////////
487
542
 
488
- //Check if the user owns this content
489
- var isOwner = service.isOwner(user, item);
490
-
491
- //If the user is the owner
492
- if (isOwner) {
493
- var actionOwnScopesHash;
494
- var ownCacheKey = `access-${user?.cacheKey}-${OWN_ACTION}-${definition}-${type}`;
495
-
496
- if (options.cache[ownCacheKey]) {
497
- actionOwnScopesHash = options.cache[ownCacheKey];
498
- } else {
499
- //Check if we can view this type of thing in any scopes
500
- var actionOwnScopes = service.actionableScopes(
501
- user,
502
- OWN_ACTION,
503
- definition,
504
- type,
505
- options,
506
- );
507
- //Get a fash hash of the scopes
508
- actionOwnScopesHash = QikCore.utils.hash(actionOwnScopes);
509
- options.cache[ownCacheKey] = actionOwnScopesHash;
510
- }
511
-
512
- //If we have data about what they can do
513
- if (actionOwnScopesHash) {
514
- //Check if the item is in any of the scopes the user can access
515
- var isInOwnScopes = itemScopes.some(function (scopeID) {
516
- return actionOwnScopesHash[scopeID];
517
- });
518
-
519
- if (isInOwnScopes) {
520
- return true;
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;
521
566
  }
522
- }
567
+
568
+ var createableScopes = service.actionableScopes(user, 'create', definition, type);
569
+ return createableScopes.length
523
570
  }
524
571
 
525
- //////////////////////////////////
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;
526
614
 
527
- //No access by default
528
- return false;
529
- };
530
-
531
- ///////////////////////////////////////////////
532
-
533
- /**
534
- * @alias access.has
535
- * @description Helpful function for checking whether a given user has a specified permission
536
- * @param {Object} user The user to check
537
- * @param {String} permission The permission the user may have been granted
538
- * @example
539
- * const user = sdk.auth.getCurrentUser();
540
- * const isAllowedToCreateItems = sdk.access.has(user, 'video.create');
541
- */
542
- service.has = function (user, permission) {
543
- //Allow everything if we're a super user
544
- if (service.isAdministrator(user)) {
545
- return true;
546
615
  }
547
616
 
548
- var permissionsLookup = service.hashPermissions(user);
549
- return permissionsLookup[permission];
550
- };
551
-
552
- ///////////////////////////////////////////////
553
-
554
- /**
555
- * @alias access.canCreate
556
- * @description Helpful function for checking whether a given user can create a specified type of item
557
- * @param {Object} user The user to check
558
- * @param {String} definition The defined type key
559
- * @param {String} type The basic type key
560
- * @param {Object} options Additional options and parameters
561
- * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
562
- * @example
563
- * const user = sdk.auth.getCurrentUser();
564
- * const userCanCreateShortFilms = sdk.access.canCreate(user, 'shortFilm', 'video');
565
- */
566
- service.canCreate = function (user, definition, type, options) {
567
- options = options || {};
568
- options.cache = options.cache || {};
569
-
570
- //Allow everything if we're a super user
571
- if (service.isAdministrator(user)) {
572
- return true;
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;
573
660
  }
574
661
 
575
- var createableScopes = service.actionableScopes(
576
- user,
577
- "create",
578
- definition,
579
- type,
580
- );
581
- return createableScopes.length;
582
- };
583
-
584
- ///////////////////////////////////////////////
585
-
586
- /**
587
- * @alias access.canListItem
588
- * @description Helpful function for checking whether a given user can list a specified item
589
- * @param {Object} user The user to check
590
- * @param {Object} item The item to check
591
- * @param {Object} options Additional options and parameters
592
- * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
593
- * @example
594
- * const user = sdk.auth.getCurrentUser();
595
- *
596
- * // Example of looping through a large array of items
597
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
598
- *
599
- * // Create an in memory cache object outside of the loop
600
- * const cache = {}
601
- *
602
- * const filtered = largeArrayOfItems.filter(function(item) {
603
- * return sdk.access.canListItem(user, item, {cache});
604
- * })
605
- */
606
- service.canListItem = function (user, item, options) {
607
- options = options || {};
608
- options.cache = options.cache || {};
609
-
610
- //Check if we can list the item
611
- var canList = service.checkActionAccess(
612
- user,
613
- item,
614
- "listany",
615
- "listown",
616
- options,
617
- );
618
-
619
- if (canList) {
620
- return true;
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;
621
691
  }
622
692
 
623
- //If we can view the item then we should be able to list it also
624
- var canView = service.canViewItem(user, item, options);
625
- if (canView) {
626
- return true;
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);
627
720
  }
628
721
 
629
- return false;
630
- };
631
-
632
- ///////////////////////////////////////////////
633
-
634
- /**
635
- * @alias access.canViewItem
636
- * @description Helpful function for checking whether a given user can view a specified item
637
- * @param {Object} user The user to check
638
- * @param {Object} item The item to check
639
- * @param {Object} options Additional options and parameters
640
- * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
641
- * @example
642
- * const user = sdk.auth.getCurrentUser();
643
- *
644
- * // Example of looping through a large array of items
645
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
646
- *
647
- * // Create an in memory cache object outside of the loop
648
- * const cache = {}
649
- *
650
- * const filtered = largeArrayOfItems.filter(function(item) {
651
- * return sdk.access.canViewItem(user, item, {cache});
652
- * })
653
- */
654
- service.canViewItem = function (user, item, options) {
655
- options = options || {};
656
- options.cache = options.cache || {};
657
-
658
- //Check if we can view the item
659
- var canView = service.checkActionAccess(
660
- user,
661
- item,
662
- "viewany",
663
- "viewOwn",
664
- options,
665
- );
666
- if (canView) {
667
- return true;
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);
668
749
  }
669
750
 
670
- //If we can edit the item then we should be able to view it also
671
- var canEdit = service.canEditItem(user, item, options);
672
- if (canEdit) {
673
- return true;
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);
674
778
  }
675
779
 
676
- return false;
677
- };
678
-
679
- ///////////////////////////////////////////////
680
-
681
- /**
682
- * @alias access.canEditItem
683
- * @description Helpful function for checking whether a given user can edit a specified item
684
- * @param {Object} user The user to check
685
- * @param {Object} item The item to check
686
- * @param {Object} options Additional options and parameters
687
- * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
688
- * @example
689
- * const user = sdk.auth.getCurrentUser();
690
- *
691
- * // Example of looping through a large array of items
692
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
693
- *
694
- * // Create an in memory cache object outside of the loop
695
- * const cache = {}
696
- *
697
- * const filtered = largeArrayOfItems.filter(function(item) {
698
- * return sdk.access.canEditItem(user, item, {cache});
699
- * })
700
- */
701
- service.canEditItem = function (user, item, options) {
702
- options = options || {};
703
- options.cache = options.cache || {};
704
-
705
- var allowed = service.checkActionAccess(
706
- user,
707
- item,
708
- "editany",
709
- "editown",
710
- options,
711
- );
712
- return allowed;
713
- };
714
-
715
- ///////////////////////////////////////////////
716
-
717
- /**
718
- * @alias access.canDeleteItem
719
- * @description Helpful function for checking whether a given user can delete a specified item
720
- * @param {Object} user The user to check
721
- * @param {Object} item The item to check
722
- * @param {Object} options Additional options and parameters
723
- * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
724
- * @example
725
- * const user = sdk.auth.getCurrentUser();
726
- *
727
- * // Example of looping through a large array of items
728
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
729
- *
730
- * // Create an in memory cache object outside of the loop
731
- * const cache = {}
732
- *
733
- * const filtered = largeArrayOfItems.filter(function(item) {
734
- * return sdk.access.canDeleteItem(user, item, {cache});
735
- * })
736
- */
737
- service.canDeleteItem = function (user, item, options) {
738
- options = options || {};
739
- options.cache = options.cache || {};
740
-
741
- return service.checkActionAccess(
742
- user,
743
- item,
744
- "deleteany",
745
- "deleteown",
746
- options,
747
- );
748
- };
749
-
750
- ///////////////////////////////////////////////
751
-
752
- /**
753
- * @alias access.canRestoreItem
754
- * @description Helpful function for checking whether a given user can restore a specified item
755
- * @param {Object} user The user to check
756
- * @param {Object} item The item to check
757
- * @param {Object} options Additional options and parameters
758
- * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
759
- * @example
760
- * const user = sdk.auth.getCurrentUser();
761
- *
762
- * // Example of looping through a large array of items
763
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
764
- *
765
- * // Create an in memory cache object outside of the loop
766
- * const cache = {}
767
- *
768
- * const filtered = largeArrayOfItems.filter(function(item) {
769
- * return sdk.access.canRestoreItem(user, item, {cache});
770
- * })
771
- */
772
- service.canRestoreItem = function (user, item, options) {
773
- options = options || {};
774
- options.cache = options.cache || {};
775
-
776
- return service.checkActionAccess(
777
- user,
778
- item,
779
- "restoreany",
780
- "restoreown",
781
- options,
782
- );
783
- };
784
-
785
- ///////////////////////////////////////////////
786
-
787
- /**
788
- * @alias access.canEraseItem
789
- * @description Helpful function for checking whether a given user can erase a specified item
790
- * @param {Object} user The user to check
791
- * @param {Object} item The item to check
792
- * @param {Object} options Additional options and parameters
793
- * @param {Object} options.cache Provide an existing object to use as an in memory cache, can be useful to increase performance if running this function in a large for/while loop
794
- * @example
795
- * const user = sdk.auth.getCurrentUser();
796
- *
797
- * // Example of looping through a large array of items
798
- * const largeArrayOfItems = [{_id:'...', title...}, {_id:'...', title...}...]
799
- *
800
- * // Create an in memory cache object outside of the loop
801
- * const cache = {}
802
- *
803
- * const filtered = largeArrayOfItems.filter(function(item) {
804
- * return sdk.access.canEraseItem(user, item, {cache});
805
- * })
806
- */
807
- service.canEraseItem = function (user, item, options) {
808
- options = options || {};
809
- options.cache = options.cache || {};
810
-
811
- return service.checkActionAccess(
812
- user,
813
- item,
814
- "eraseany",
815
- "eraseown",
816
- options,
817
- );
818
- };
819
-
820
- return service;
821
- };
780
+
781
+
782
+ return service;
783
+
784
+ }
822
785
 
823
786
  ///////////////////////////////////////////////////////////////////////////////
824
787
 
825
- export default QikAccess;
788
+
789
+
790
+ export default QikAccess;