@cocreate/authorize 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-authorize/compare/v1.1.0...v1.2.0) (2023-06-02)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * html formating ([0882b10](https://github.com/CoCreate-app/CoCreate-authorize/commit/0882b10bd6ad41909b8f834a4828620a5253331d))
7
+
8
+
9
+ ### Features
10
+
11
+ * Added support for utils package ([c05d7da](https://github.com/CoCreate-app/CoCreate-authorize/commit/c05d7dae1ec85687f753737a790021ff01b90450))
12
+
1
13
  # [1.1.0](https://github.com/CoCreate-app/CoCreate-authorize/compare/v1.0.0...v1.1.0) (2023-06-01)
2
14
 
3
15
 
package/docs/index.html CHANGED
@@ -1,103 +1,232 @@
1
1
  <!DOCTYPE html>
2
2
  <html lang="en">
3
3
  <head>
4
- <meta charset="utf-8">
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
- <meta name="viewport" content="width=device-width, initial-scale=1">
7
- <title>CoCreate-authorize Documentation | CoCreateJS </title>
8
- <link rel="icon" type="image/png" sizes="32x32" href="https://cocreate.app/images/favicon.ico">
9
- <meta name="description" content="A simple HTML5 and pure javascript component. Easy configuration using data-attributes and highly styleable." />
10
- <meta name="keywords" content="helper classes, utility classes, css framework, css library, inline style classes" />
4
+ <meta charset="utf-8" />
5
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
7
+ <title>CoCreate-authorize Documentation | CoCreateJS</title>
8
+ <link
9
+ rel="icon"
10
+ type="image/png"
11
+ sizes="32x32"
12
+ href="https://cocreate.app/images/favicon.ico" />
13
+ <meta
14
+ name="description"
15
+ content="A simple HTML5 and pure javascript component. Easy configuration using data-attributes and highly styleable." />
16
+ <meta
17
+ name="keywords"
18
+ content="helper classes, utility classes, css framework, css library, inline style classes" />
11
19
  <meta name="robots" content="index,follow" />
12
-
20
+
13
21
  <!-- CoCreate CSS -->
14
- <link rel="stylesheet" href="https://cdn.cocreate.app/latest/CoCreate.min.css" type="text/css" />
15
-
16
- <link rel="manifest" href="/manifest.json" />
17
- </head>
22
+ <link
23
+ rel="stylesheet"
24
+ href="https://cdn.cocreate.app/latest/CoCreate.min.css"
25
+ type="text/css" />
26
+
27
+ <link rel="manifest" href="/manifest.json" />
28
+ </head>
18
29
 
19
30
  <body>
20
31
  <div collection="" document_id="" name="" id="cocreate-authorize">
21
- <div class="display:flex flex-wrap:wrap justify-content:space-between margin:10px">
32
+ <div
33
+ class="display:flex flex-wrap:wrap justify-content:space-between margin:10px">
22
34
  <div class="display:flex align-items:center">
23
35
  <h2>CoCreate-authorize</h2>
24
36
  </div>
25
- <div class="display:flex align-items:center font-size:20px" share-height="600" share-width="500" share-media="https://via.placeholder.com/300/09f/fff.png">
26
- <a href="https://github.com/CoCreate-app/CoCreate-authorize" target="_blank" class="margin-right:15px"><i class="fab fa-github"></i></a>
27
- <a class="margin-right:15px share" share-network='twitter' title="Share on twitter"><i class="fab fa-twitter"></i></a>
28
- <a class="margin-right:15px share" share-network='facebook' title="Share on Facebook"><i class="fab fa-facebook"></i></a>
29
- <a class="margin-right:15px share" share-network='instagram' title="Share on instagram"><i class="fab fa-instagram"></i></a>
30
- <a class="margin-right:15px share" share-network='share' title="Share on share"><i class="fas fa-share-alt"></i></a>
37
+ <div
38
+ class="display:flex align-items:center font-size:20px"
39
+ share-height="600"
40
+ share-width="500"
41
+ share-media="https://via.placeholder.com/300/09f/fff.png">
42
+ <a
43
+ href="https://github.com/CoCreate-app/CoCreate-authorize"
44
+ target="_blank"
45
+ class="margin-right:15px"
46
+ ><i class="fab fa-github"></i
47
+ ></a>
48
+ <a
49
+ class="margin-right:15px share"
50
+ share-network="twitter"
51
+ title="Share on twitter"
52
+ ><i class="fab fa-twitter"></i
53
+ ></a>
54
+ <a
55
+ class="margin-right:15px share"
56
+ share-network="facebook"
57
+ title="Share on Facebook"
58
+ ><i class="fab fa-facebook"></i
59
+ ></a>
60
+ <a
61
+ class="margin-right:15px share"
62
+ share-network="instagram"
63
+ title="Share on instagram"
64
+ ><i class="fab fa-instagram"></i
65
+ ></a>
66
+ <a
67
+ class="margin-right:15px share"
68
+ share-network="share"
69
+ title="Share on share"
70
+ ><i class="fas fa-share-alt"></i
71
+ ></a>
31
72
  </div>
32
73
  </div>
33
- <p class="max-width:500px margin:20px_10px">A simple HTML5, CSS and pure javascript component. Easy configuration using data-attributes and highly styleable.</h1>
74
+ <h1 class="max-width:500px margin:20px_10px">
75
+ A simple HTML5, CSS and pure javascript component. Easy
76
+ configuration using data-attributes and highly styleable.
77
+ </h1>
34
78
  <div id="authorize-section" class="display:flex flex-wrap:wrap">
35
- <div class="flex-grow:1 min-width:300px width:50% padding:20px_10px">
36
- <h2 class="border-bottom:1px_solid_lightgrey padding:5px_0px">Install</h2>
37
- <pre class="margin-top:15px"><code class="language-javascript">npm install cocreate-authorize</code></pre>
79
+ <div
80
+ class="flex-grow:1 min-width:300px width:50% padding:20px_10px">
81
+ <h2
82
+ class="border-bottom:1px_solid_lightgrey padding:5px_0px">
83
+ Install
84
+ </h2>
85
+ <pre
86
+ class="margin-top:15px"><code class="language-javascript">npm install cocreate-authorize</code></pre>
38
87
  <p class="padding:10px_0px">Or you can use cdn link:</p>
39
88
  <pre><code class="language-javascript">https://cdn.cocreate.app/js/CoCreate-authorize.min.js</code></pre>
40
-
41
- <h2 class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">Usage</h2>
89
+
90
+ <h2
91
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
92
+ Usage
93
+ </h2>
42
94
  <p class="padding:10px_0px">authorize usage content</p>
43
95
  <pre><code class="language-html">&lt;div&gt;&lt;/div&gt;</code></pre>
44
-
45
- <h2 class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">Reference</h2>
46
- <p class="padding:10px_0px">This is authorize reference content</p>
96
+
97
+ <h2
98
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
99
+ Reference
100
+ </h2>
101
+ <p class="padding:10px_0px">
102
+ This is authorize reference content
103
+ </p>
47
104
  <pre><code class="language-javascript">&lt;div&gt;&lt;/div&gt;</code></pre>
48
- <p class="padding:10px_0px">This is authorize reference content</p>
105
+ <p class="padding:10px_0px">
106
+ This is authorize reference content
107
+ </p>
49
108
 
50
- <h2 class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">Attributes</h2>
51
- <ul class="list-style-type:none ">
52
- <li class="padding:15px_0px border-bottom:1px_solid_lightgrey">
53
- <h4><span>authorize</span> <span class="cocreate-badge success">string</span> <span class="cocreate-badge warning">optional</span></h4>
109
+ <h2
110
+ class="border-bottom:1px_solid_lightgrey margin-top:20px padding:5px_0px">
111
+ Attributes
112
+ </h2>
113
+ <ul class="list-style-type:none">
114
+ <li
115
+ class="padding:15px_0px border-bottom:1px_solid_lightgrey">
116
+ <h4>
117
+ <span>authorize</span>
118
+ <span class="cocreate-badge success"
119
+ >string</span
120
+ >
121
+ <span class="cocreate-badge warning"
122
+ >optional</span
123
+ >
124
+ </h4>
54
125
  <p>authorize-attribute</p>
55
126
  </li>
56
- <li class="padding:15px_0px border-bottom:1px_solid_lightgrey">
57
- <h4><span>authorize</span> <span class="cocreate-badge success">string</span> <span class="cocreate-badge warning">optional</span></h4>
127
+ <li
128
+ class="padding:15px_0px border-bottom:1px_solid_lightgrey">
129
+ <h4>
130
+ <span>authorize</span>
131
+ <span class="cocreate-badge success"
132
+ >string</span
133
+ >
134
+ <span class="cocreate-badge warning"
135
+ >optional</span
136
+ >
137
+ </h4>
58
138
  <p>authorize-attribute</p>
59
139
  </li>
60
140
  </ul>
61
141
  </div>
62
-
63
- <div class="flex-grow:1 min-width:300px width:50% padding:0px_10px margin:20px_0px border-bottom:1px_solid_lightgrey">
64
- <!-- SandBox -->
65
- <div class="display:flex flex-direction:column position:relative overflow:hidden card border-radius:2px width:auto height:600px margin-top:20px" id="playground">
66
-
67
- <div id="demo-code" resizable class="position:relative height:50%">
68
- <textarea type="code" lang="html" collection="demos" document_id="" name="demo" save="false" id="demo" class="height:100% width:100% outline:none border:none resize:none padding:5px"></textarea>
69
- <div resize="bottom" class="background:lightgrey"></div>
70
- </div>
71
-
72
- <div id="demo-preview" class="position:relative overflow:auto background-color:white">
73
- <div get-value="#demo" class="padding:20px"></div>
74
- </div>
75
-
76
- <div class="font-size:20px position:absolute top:10px right:10px opacity:0.6">
77
- <a class="margin-right:10px" id="eye" show="#eye-slash" hide="#eye, #demo-preview" toggle="code-height" toggle-target="#demo-code"><i class="far fa-eye"></i></a>
78
- <a class="margin-right:10px" hidden id="eye-slash" show="#eye, #demo-preview" hide="#eye-slash" toggle="code-height" toggle-target="#demo-code"><i class="fas fa-eye-slash"></i></a>
79
- <a class="margin-right:10px" id="code" show="#code-slash" hide="#code, #demo-code"><i class="fa fa-code"></i></a>
80
- <a class="margin-right:10px" hidden id="code-slash" show="#code, #demo-code" hide="#code-slash"><i class="fas fa-code"></i></a>
81
- <a class="margin-right:5px" fullscreen target="#playground"><i class="fas fa-expand"></i></a>
82
- </div>
83
-
142
+
143
+ <div
144
+ class="flex-grow:1 min-width:300px width:50% padding:0px_10px margin:20px_0px border-bottom:1px_solid_lightgrey">
145
+ <!-- SandBox -->
146
+ <div
147
+ class="display:flex flex-direction:column position:relative overflow:hidden card border-radius:2px width:auto height:600px margin-top:20px"
148
+ id="playground">
149
+ <div
150
+ id="demo-code"
151
+ resizable
152
+ class="position:relative height:50%">
153
+ <textarea
154
+ type="code"
155
+ lang="html"
156
+ collection="demos"
157
+ document_id=""
158
+ name="demo"
159
+ save="false"
160
+ id="demo"
161
+ class="height:100% width:100% outline:none border:none resize:none padding:5px"></textarea>
162
+ <div
163
+ resize="bottom"
164
+ class="background:lightgrey"></div>
165
+ </div>
166
+
167
+ <div
168
+ id="demo-preview"
169
+ class="position:relative overflow:auto background-color:white">
170
+ <div get-value="#demo" class="padding:20px"></div>
171
+ </div>
172
+
173
+ <div
174
+ class="font-size:20px position:absolute top:10px right:10px opacity:0.6">
175
+ <a
176
+ class="margin-right:10px"
177
+ id="eye"
178
+ show="#eye-slash"
179
+ hide="#eye, #demo-preview"
180
+ toggle="code-height"
181
+ toggle-target="#demo-code"
182
+ ><i class="far fa-eye"></i
183
+ ></a>
184
+ <a
185
+ class="margin-right:10px"
186
+ hidden
187
+ id="eye-slash"
188
+ show="#eye, #demo-preview"
189
+ hide="#eye-slash"
190
+ toggle="code-height"
191
+ toggle-target="#demo-code"
192
+ ><i class="fas fa-eye-slash"></i
193
+ ></a>
194
+ <a
195
+ class="margin-right:10px"
196
+ id="code"
197
+ show="#code-slash"
198
+ hide="#code, #demo-code"
199
+ ><i class="fa fa-code"></i
200
+ ></a>
201
+ <a
202
+ class="margin-right:10px"
203
+ hidden
204
+ id="code-slash"
205
+ show="#code, #demo-code"
206
+ hide="#code-slash"
207
+ ><i class="fas fa-code"></i
208
+ ></a>
209
+ <a
210
+ class="margin-right:5px"
211
+ fullscreen
212
+ target="#playground"
213
+ ><i class="fas fa-expand"></i
214
+ ></a>
84
215
  </div>
85
- <!-- End SandBox -->
86
- </div>
216
+ </div>
217
+ <!-- End SandBox -->
87
218
  </div>
88
-
89
- </div>
219
+ </div>
90
220
  </div>
91
-
221
+
92
222
  <script>
93
223
  var CoCreateConfig = {
94
- key: '2061acef-0451-4545-f754-60cf8160',
95
- organization_id: '5ff747727005da1c272740ab'
96
- }
224
+ key: "2061acef-0451-4545-f754-60cf8160",
225
+ organization_id: "5ff747727005da1c272740ab",
226
+ };
97
227
  </script>
98
-
228
+
99
229
  <!--CoCreateJS-->
100
230
  <script src="https://cdn.cocreate.app/latest/CoCreate.min.js"></script>
101
-
102
231
  </body>
103
- </html>
232
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/authorize",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "A simple authorize component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "authorize",
@@ -47,6 +47,7 @@
47
47
  "devDependencies": {},
48
48
  "dependencies": {
49
49
  "@cocreate/crud-client": "^1.21.5",
50
- "@cocreate/docs": "^1.7.13"
50
+ "@cocreate/docs": "^1.7.13",
51
+ "@cocreate/utils": "^1.20.10"
51
52
  }
52
53
  }
package/src/index.js CHANGED
@@ -1,18 +1,19 @@
1
1
  (function (root, factory) {
2
2
  if (typeof define === 'function' && define.amd) {
3
- define(["@cocreate/crud-client"], function (crud) {
4
- return factory(true, crud)
3
+ define(["@cocreate/crud-client", "@cocreate/utils"], function (crud, { getValueFromObject, dotNotationToObject }) {
4
+ return factory(true, crud, { getValueFromObject, dotNotationToObject })
5
5
  });
6
6
  } else if (typeof module === 'object' && module.exports) {
7
+ const { getValueFromObject, dotNotationToObject } = require("@cocreate/utils");
7
8
  module.exports = class CoCreateAuthorize {
8
9
  constructor(crud) {
9
- return factory(false, crud);
10
+ return factory(false, crud, { getValueFromObject, dotNotationToObject });
10
11
  }
11
12
  }
12
13
  } else {
13
- root.returnExports = factory(true, root["@cocreate/crud-client"]);
14
+ root.returnExports = factory(true, root["@cocreate/crud-client"], root["@cocreate/utils"]);
14
15
  }
15
- }(typeof self !== 'undefined' ? self : this, function (isBrowser, crud) {
16
+ }(typeof self !== 'undefined' ? self : this, function (isBrowser, crud, { getValueFromObject, dotNotationToObject }) {
16
17
 
17
18
  const permissions = new Map()
18
19
 
@@ -42,6 +43,17 @@
42
43
  }
43
44
  }
44
45
 
46
+ async function deletePermission(data) {
47
+ const { collection, document, organization_id } = data
48
+
49
+ if (collection === 'keys' && document) {
50
+ let permission = document[0]
51
+ if (permission && permission.key && hasPermission(permission.key)) {
52
+ permissions.delete(permission.key)
53
+ }
54
+ }
55
+ }
56
+
45
57
  function setPermission(key, permission) {
46
58
  permissions.set(key, permission)
47
59
  }
@@ -186,12 +198,6 @@
186
198
  if (permission.admin == 'true' || permission.admin === true)
187
199
  return true;
188
200
 
189
- // if (data.request) {
190
- // let type = action.match(/[A-Z][a-z]+/g);
191
- // type = type[0].toLowerCase()
192
- // data[type] = data.request
193
- // }
194
-
195
201
  let status = await checkAction(permission.actions, action, endPoint, data, filter)
196
202
 
197
203
  if (!status)
@@ -289,7 +295,7 @@
289
295
  } else
290
296
  delete data[key]
291
297
 
292
- // if key status is true for unauthorized case
298
+ // if key status is false for unauthorized case
293
299
  if (!keyStatus || keyStatus && unauthorize) {
294
300
  if (!data.unauthorized || !data.unauthorized[action])
295
301
  data.unauthorized = { [action]: { [key]: [data[key]] } }
@@ -304,42 +310,40 @@
304
310
 
305
311
  async function checkArray(authorized, data, key, unauthorize) {
306
312
  let keyStatus = false
307
- let authorizedValue = eval('authorized.' + key)
308
- let dataValue = eval('data.' + key)
313
+ let authorizedValue = getValueFromObject(authorized, key);
314
+ let dataValue = getValueFromObject(data, key);
309
315
 
310
316
  if (!authorizedValue && !unauthorize) {
311
- if (key.endsWith(']')) {
312
- let lastIndex = key.lastIndexOf('[')
313
- let index = key.slice(lastIndex + 1).slice(0, -1)
314
- let nkey = key.slice(0, key.length - (index.length + 2))
315
- console.log(index, nkey);
316
- eval(`data.${nkey}.splice(${index}, 1)`)
317
- } else
318
- eval(`delete data.${key}`)
317
+ data = deleteKey(data, key)
319
318
  } else if (typeof dataValue == "string") {
320
319
  if (unauthorize && authorizedValue.includes(dataValue))
321
- eval(`delete data.${key}`)
320
+ data = deleteKey(data, key)
322
321
  else {
323
322
  if (!authorizedValue.includes(dataValue))
324
- eval(`delete data.${key}`)
323
+ data = deleteKey(data, key)
325
324
  else
326
325
  keyStatus = true
327
326
  }
328
-
329
327
  } else if (Array.isArray(dataValue)) {
330
328
  for (let i = 0; i < dataValue.length; i++) {
331
- let nkey = key + `[${i}]`
332
- keyStatus = await checkArray(authorized, data, nkey, unauthorize)
329
+ keyStatus = await checkArray(authorized, data, `${key}[${i}]`, unauthorize)
333
330
  }
334
331
  } else if (typeof dataValue === "object") {
335
- if (authorizedValue['*'] || authorizedValue['*'] == '') {
336
- keyStatus = true
337
- } else
338
- for (const k of Object.keys(dataValue)) {
339
- let nkey = `${key}.${k}`
340
- keyStatus = await checkArray(authorized, data, nkey, unauthorize)
341
- }
332
+ let checkKeys = true
333
+ if (dataValue['_id']) {
334
+ if (authorized.document.includes(dataValue['_id']))
335
+ checkKeys = true
336
+ }
337
+ if (checkKeys) {
338
+ if (authorizedValue['*'] || authorizedValue['*'] == '')
339
+ keyStatus = true
340
+ else
341
+ for (const k of Object.keys(dataValue)) {
342
+ keyStatus = await checkArray(authorized, data, `${key}.${k}`, unauthorize)
343
+ }
344
+ }
342
345
  }
346
+
343
347
  return keyStatus
344
348
  }
345
349
 
@@ -365,6 +369,14 @@
365
369
  }
366
370
  }
367
371
 
372
+ function deleteKey(data, path) {
373
+ if (!data || !path) return
374
+ if (path.includes('._id'))
375
+ path = path.replace('._id', '');
376
+ data = dotNotationToObject({ [path]: undefined }, data)
377
+ return data
378
+ }
379
+
368
380
  return {
369
381
  check
370
382
  }