@cocreate/authorize 1.1.0 → 1.2.1
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/.github/FUNDING.yml +3 -3
- package/.github/workflows/automated.yml +55 -55
- package/CHANGELOG.md +27 -8
- package/CoCreate.config.js +25 -25
- package/LICENSE +574 -574
- package/README.md +70 -70
- package/docs/index.html +232 -103
- package/package.json +53 -52
- package/release.config.js +21 -21
- package/src/index.js +384 -372
package/src/index.js
CHANGED
|
@@ -1,372 +1,384 @@
|
|
|
1
|
-
(function (root, factory) {
|
|
2
|
-
if (typeof define === 'function' && define.amd) {
|
|
3
|
-
define(["@cocreate/crud-client"], function (crud) {
|
|
4
|
-
return factory(true, crud)
|
|
5
|
-
});
|
|
6
|
-
} else if (typeof module === 'object' && module.exports) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
permission
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (!
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
if (
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
// if key
|
|
293
|
-
if (
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
if (
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
|
|
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
|
+
}));
|