@qikdev/sdk 1.0.2 → 1.0.5

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