@cocreate/authorize 1.0.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,22 @@
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
+
13
+ # [1.1.0](https://github.com/CoCreate-app/CoCreate-authorize/compare/v1.0.0...v1.1.0) (2023-06-01)
14
+
15
+
16
+ ### Features
17
+
18
+ * **authorize:** Add permission-based authorization function ([dce152f](https://github.com/CoCreate-app/CoCreate-authorize/commit/dce152f6f504a5ac790e98753ad31c5e83cee295))
19
+
1
20
  # 1.0.0 (2023-05-31)
2
21
 
3
22
 
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.0.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 ADDED
@@ -0,0 +1,384 @@
1
+ (function (root, factory) {
2
+ if (typeof define === 'function' && define.amd) {
3
+ define(["@cocreate/crud-client", "@cocreate/utils"], function (crud, { getValueFromObject, dotNotationToObject }) {
4
+ return factory(true, crud, { getValueFromObject, dotNotationToObject })
5
+ });
6
+ } else if (typeof module === 'object' && module.exports) {
7
+ const { getValueFromObject, dotNotationToObject } = require("@cocreate/utils");
8
+ module.exports = class CoCreateAuthorize {
9
+ constructor(crud) {
10
+ return factory(false, crud, { getValueFromObject, dotNotationToObject });
11
+ }
12
+ }
13
+ } else {
14
+ root.returnExports = factory(true, root["@cocreate/crud-client"], root["@cocreate/utils"]);
15
+ }
16
+ }(typeof self !== 'undefined' ? self : this, function (isBrowser, crud, { getValueFromObject, dotNotationToObject }) {
17
+
18
+ const permissions = new Map()
19
+
20
+ if (isBrowser) {
21
+ crud.listen('updateDocument', function (data) {
22
+ updatePermission(data)
23
+ });
24
+
25
+ crud.listen('deleteDocument', function (data) {
26
+ deletePermission(data)
27
+ });
28
+ } else {
29
+ process.on('changed-document', async (data) => {
30
+ updatePermission(data)
31
+ })
32
+ }
33
+
34
+ async function updatePermission(data) {
35
+ const { collection, document, organization_id } = data
36
+
37
+ if (collection === 'keys' && document) {
38
+ let permission = document[0]
39
+ if (permission && permission.key && hasPermission(permission.key)) {
40
+ let newPermission = await readPermisson(permission.key, organization_id)
41
+ setPermission(permission.key, newPermission)
42
+ }
43
+ }
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
+
57
+ function setPermission(key, permission) {
58
+ permissions.set(key, permission)
59
+ }
60
+
61
+ function hasPermission(key) {
62
+ return permissions.has(key)
63
+ }
64
+
65
+ async function getPermission(key, organization_id) {
66
+ if (permissions.get(key)) {
67
+ return permissions.get(key)
68
+ } else {
69
+ let permission = await readPermisson(key, organization_id);
70
+ permissions.set(key, permission)
71
+ return permission
72
+ }
73
+ }
74
+
75
+ async function readPermisson(key, organization_id) {
76
+ try {
77
+ if (!organization_id)
78
+ return null;
79
+
80
+ let request = {
81
+ collection: 'keys',
82
+ organization_id,
83
+ filter: {
84
+ query: []
85
+ }
86
+ }
87
+
88
+ if (key)
89
+ request.filter.query.push({ name: 'key', value: key, operator: '$eq' })
90
+ else
91
+ request.filter.query.push({ name: 'default', value: true, operator: '$eq' })
92
+
93
+
94
+ let permission = await crud.readDocument(request)
95
+ if (permission && permission.document && permission.document[0]) {
96
+ permission = permission.document[0]
97
+
98
+ if (!permission.collections) {
99
+ permission.collections = {};
100
+ }
101
+
102
+ if (permission && permission.roles) {
103
+ const role_ids = []
104
+ permission.roles.forEach((_id) => {
105
+ if (_id)
106
+ role_ids.push({ _id })
107
+ })
108
+
109
+ delete request.filter
110
+ delete request.request
111
+ request.document = role_ids
112
+
113
+ let roles = await crud.readDocument(request)
114
+ roles = roles.document
115
+
116
+ permission = createPermissionObject(permission, roles)
117
+ }
118
+
119
+ }
120
+
121
+ return permission;
122
+
123
+ } catch (error) {
124
+ console.log("Permission Error", error)
125
+ return null;
126
+ }
127
+
128
+ }
129
+
130
+ async function createPermissionObject(permission, roles) {
131
+ roles.map(role => {
132
+ for (const roleKey in role) {
133
+ if (!["_id", "type", "name", "organization_id"].includes(roleKey)) {
134
+ if (!permission[roleKey]) {
135
+ permission[roleKey] = role[roleKey]
136
+ } else {
137
+ if (Array.isArray(role[roleKey])) {
138
+ for (let item of role[roleKey]) {
139
+ if (!permission[roleKey].includes(item))
140
+ permission[roleKey].push(item)
141
+ }
142
+ }
143
+ else if (typeof role[roleKey] == 'object') {
144
+ for (const c of Object.keys(role[roleKey])) {
145
+ if (!permission[roleKey][c]) {
146
+ permission[roleKey][c] = role[roleKey][c]
147
+ } else {
148
+ if (typeof role[roleKey][c] == 'object') {
149
+ permission[roleKey][c] = { ...permission[roleKey][c], ...role[roleKey][c] }
150
+ } else {
151
+ permission[roleKey][c] = role[roleKey][c]
152
+ }
153
+ }
154
+ }
155
+ } else {
156
+ permission[roleKey] = role[roleKey]
157
+ }
158
+ }
159
+ }
160
+ }
161
+ })
162
+ return permission;
163
+ }
164
+
165
+ async function check(action, data, user_id) {
166
+ let permission = false
167
+ if (user_id) {
168
+ permission = await checkPermissionObject({
169
+ key: user_id,
170
+ action,
171
+ data
172
+ })
173
+ }
174
+ if (!permission || permission.error) {
175
+ permission = await checkPermissionObject({
176
+ key: data.key,
177
+ action,
178
+ data
179
+ })
180
+ }
181
+ return permission;
182
+ }
183
+
184
+ async function checkPermissionObject({ key, action, data }) {
185
+ let { organization_id, filter, endPoint } = data
186
+ if (!key || !organization_id) return false;
187
+
188
+ let permission = await getPermission(key, organization_id)
189
+ if (!permission || permission.error)
190
+ return permission
191
+ if (permission.organization_id !== organization_id)
192
+ return false;
193
+ if (permission.hosts && permission.hosts.length) {
194
+ if (!permission.hosts || (!permission.hosts.includes(data.host) && !permission.hosts.includes("*")))
195
+ return false;
196
+
197
+ }
198
+ if (permission.admin == 'true' || permission.admin === true)
199
+ return true;
200
+
201
+ let status = await checkAction(permission.actions, action, endPoint, data, filter)
202
+
203
+ if (!status)
204
+ return false
205
+
206
+ return { authorized: data };
207
+ }
208
+
209
+ async function checkAction(permissions, action, endPoint, data) {
210
+ if (!permissions || !action || !permissions[action] || permissions[action] == 'false') return false;
211
+ if (permissions[action] === true || permissions[action] == 'true' || permissions[action] == '*') return true;
212
+
213
+ let authorized = permissions[action].authorize
214
+ if (authorized) {
215
+ let status = await checkAthorized(authorized, action, endPoint, data)
216
+ if (!status)
217
+ return false
218
+ else {
219
+ let unauthorized = permissions[action].unauthorize
220
+ if (unauthorized) {
221
+ let status = await checkAthorized(unauthorized, action, endPoint, data, true)
222
+ if (status)
223
+ return false
224
+ }
225
+ return true
226
+ }
227
+ } else
228
+ return false
229
+ }
230
+
231
+ async function checkAthorized(authorized, action, endPoint, data, unauthorize) {
232
+ if (!Array.isArray(authorized))
233
+ authorized = [authorized]
234
+
235
+ let status = false
236
+ for (let i = 0; i < authorized.length; i++) {
237
+ // if authorized[i] is a booleaan
238
+ if (authorized[i] === true)
239
+ return true
240
+
241
+ // if authorized[i] is a string or an array
242
+ if (typeof authorized[i] === "string" || Array.isArray(authorized[i])) {
243
+ if (authorized[i].includes(true) || authorized[i].includes('true') || authorized[i].includes('*'))
244
+ return true
245
+ else if (endPoint)
246
+ return authorized.includes(endPoint)
247
+ else
248
+ return false
249
+ }
250
+
251
+ // if authorized[i] is an object
252
+ for (const key of Object.keys(authorized[i])) {
253
+ status = await checkAthorizedKey(authorized[i], action, endPoint, data, key, unauthorize)
254
+ }
255
+
256
+ }
257
+
258
+ return status
259
+
260
+ }
261
+
262
+ async function checkAthorizedKey(authorized, action, endPoint, data, key, unauthorize) {
263
+ let status = false;
264
+ let keyStatus = false;
265
+
266
+ // if authorized[key] is a booleaan
267
+ if (authorized[key] === true)
268
+ keyStatus = true
269
+
270
+ // if authorized[key] is a string or number
271
+ else if (typeof authorized[key] === "string" || typeof authorized[key] === "number") {
272
+ if (authorized[key] === true || authorized[key] === 'true' || authorized[key] === '*')
273
+ keyStatus = true
274
+ else if (data[key]) {
275
+ keyStatus = await checkArray(authorized, data, key, unauthorize)
276
+ if (await checkFilter(authorized, data, key, unauthorize))
277
+ status = true
278
+ }
279
+ }
280
+
281
+ // if authorized[key] is an array
282
+ else if (Array.isArray(authorized[key])) {
283
+ if (authorized[key].includes(true) || authorized[key].includes('true') || authorized[key].includes('*'))
284
+ keyStatus = true
285
+ else if (data[key]) {
286
+ keyStatus = await checkArray(authorized, data, key, unauthorize)
287
+ if (await checkFilter(authorized, data, key, unauthorize))
288
+ status = true
289
+ }
290
+ }
291
+
292
+ // if authorized[key] is an object
293
+ else if (typeof authorized[key] === "object") {
294
+ console.log('authorized[key] is an object', authorized[key])
295
+ } else
296
+ delete data[key]
297
+
298
+ // if key status is false for unauthorized case
299
+ if (!keyStatus || keyStatus && unauthorize) {
300
+ if (!data.unauthorized || !data.unauthorized[action])
301
+ data.unauthorized = { [action]: { [key]: [data[key]] } }
302
+ else if (!data.unauthorized[action][key])
303
+ data.unauthorized[action][key] = [data[key]]
304
+ else
305
+ data.unauthorized[action][key].push(data[key])
306
+ } else
307
+ status = true
308
+ return status
309
+ }
310
+
311
+ async function checkArray(authorized, data, key, unauthorize) {
312
+ let keyStatus = false
313
+ let authorizedValue = getValueFromObject(authorized, key);
314
+ let dataValue = getValueFromObject(data, key);
315
+
316
+ if (!authorizedValue && !unauthorize) {
317
+ data = deleteKey(data, key)
318
+ } else if (typeof dataValue == "string") {
319
+ if (unauthorize && authorizedValue.includes(dataValue))
320
+ data = deleteKey(data, key)
321
+ else {
322
+ if (!authorizedValue.includes(dataValue))
323
+ data = deleteKey(data, key)
324
+ else
325
+ keyStatus = true
326
+ }
327
+ } else if (Array.isArray(dataValue)) {
328
+ for (let i = 0; i < dataValue.length; i++) {
329
+ keyStatus = await checkArray(authorized, data, `${key}[${i}]`, unauthorize)
330
+ }
331
+ } else if (typeof dataValue === "object") {
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
+ }
345
+ }
346
+
347
+ return keyStatus
348
+ }
349
+
350
+ async function checkFilter(authorized, data, key, unauthorize) {
351
+ if (data.filter && data.filter.query) {
352
+ let name
353
+ if (data.filter.type == 'document')
354
+ name = '_id'
355
+ else if (data.filter.type == 'collection')
356
+ name = 'name'
357
+ if (name) {
358
+ for (let value of authorized[key]) {
359
+ if (value[name])
360
+ value = value[name]
361
+ if (unauthorize)
362
+ data.filter.query.push({ name, value, operator: '$ne', logicalOperator: 'or' })
363
+ else
364
+ data.filter.query.push({ name, value, operator: '$eq', logicalOperator: 'or' })
365
+ }
366
+ if (!unauthorize)
367
+ return true
368
+ }
369
+ }
370
+ }
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
+
380
+ return {
381
+ check
382
+ }
383
+
384
+ }));