@opengis/fastify-table 1.1.48 → 1.1.49
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 +2 -2
- package/README.md +26 -26
- package/config.js +10 -10
- package/cron/controllers/cronApi.js +22 -22
- package/cron/controllers/utils/cronList.js +1 -1
- package/cron/index.js +10 -10
- package/crud/controllers/deleteCrud.js +4 -9
- package/crud/controllers/insert.js +7 -8
- package/crud/controllers/update.js +10 -13
- package/crud/controllers/utils/xssInjection.js +72 -72
- package/crud/funcs/getAccess.js +24 -11
- package/crud/funcs/getToken.js +27 -27
- package/crud/funcs/isFileExists.js +13 -13
- package/crud/funcs/setToken.js +53 -53
- package/crud/funcs/utils/getFolder.js +9 -0
- package/package.json +6 -7
- package/redis/funcs/getRedis.js +23 -23
- package/server/migrations/log.sql +80 -80
- package/table/controllers/data.js +24 -40
- package/table/controllers/table.js +22 -26
- package/table/index.js +50 -3
- package/test/config.example +18 -18
- package/test/funcs/pg.test.js +34 -34
- package/test/funcs/redis.test.js +19 -19
- package/test/templates/cls/test.json +9 -9
- package/test/templates/form/cp_building.form.json +32 -32
- package/test/templates/select/account_id.json +3 -3
- package/test/templates/select/storage.data.json +2 -2
- package/test/templates/table/gis.dataset.table.json +20 -20
- package/util/controllers/next.id.js +4 -4
- package/util/controllers/properties.get.js +19 -19
- package/util/index.js +23 -23
- package/utils.js +2 -0
- package/table/schema.js +0 -54
package/Changelog.md
CHANGED
package/README.md
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
# fastify-table
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/@opengis/fastify-table)
|
|
4
|
-
[](http://standardjs.com/)
|
|
5
|
-
|
|
6
|
-
It standardizes the entire form building process, while taking care of everything from rendering to validation and processing:
|
|
7
|
-
|
|
8
|
-
- pg
|
|
9
|
-
- redis
|
|
10
|
-
- crud
|
|
11
|
-
|
|
12
|
-
## Install
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
npm i @opengis/fastify-table
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
## Usage
|
|
19
|
-
|
|
20
|
-
```js
|
|
21
|
-
fastify.register(import('@opengis/fastify-table'), config);
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Documenation
|
|
25
|
-
|
|
26
|
-
For a detailed understanding fastify-table, its features, and how to use them, refer to our [Documentation](https://apidocs.softpro.ua/gis.storage/).
|
|
1
|
+
# fastify-table
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@opengis/fastify-table)
|
|
4
|
+
[](http://standardjs.com/)
|
|
5
|
+
|
|
6
|
+
It standardizes the entire form building process, while taking care of everything from rendering to validation and processing:
|
|
7
|
+
|
|
8
|
+
- pg
|
|
9
|
+
- redis
|
|
10
|
+
- crud
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm i @opengis/fastify-table
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
fastify.register(import('@opengis/fastify-table'), config);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Documenation
|
|
25
|
+
|
|
26
|
+
For a detailed understanding fastify-table, its features, and how to use them, refer to our [Documentation](https://apidocs.softpro.ua/gis.storage/).
|
package/config.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
|
|
3
|
-
const fileName = ['config.json', '/data/local/config.json'].find(el => (fs.existsSync(el) ? el : null));
|
|
4
|
-
const config = fileName ? JSON.parse(fs.readFileSync(fileName)) : {};
|
|
5
|
-
|
|
6
|
-
Object.assign(config, {
|
|
7
|
-
allTemplates: config?.allTemplates || {},
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
export default config;
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
|
|
3
|
+
const fileName = ['config.json', '/data/local/config.json'].find(el => (fs.existsSync(el) ? el : null));
|
|
4
|
+
const config = fileName ? JSON.parse(fs.readFileSync(fileName)) : {};
|
|
5
|
+
|
|
6
|
+
Object.assign(config, {
|
|
7
|
+
allTemplates: config?.allTemplates || {},
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export default config;
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import cronList from './utils/cronList.js';
|
|
2
|
-
|
|
3
|
-
export default async function cronApi(req) {
|
|
4
|
-
const {
|
|
5
|
-
params = {}, user = {}, hostname,
|
|
6
|
-
} = req;
|
|
7
|
-
|
|
8
|
-
if ((!user.uid || !user.user_type?.includes('admin')) && !hostname?.includes('localhost')) {
|
|
9
|
-
return { message: 'access restricted', status: 403 };
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
if (params.name === 'list') {
|
|
13
|
-
return { data: Object.keys(cronList || {}) };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (!cronList[params.name]) {
|
|
17
|
-
return { message: `cron not found: ${params.name}`, status: 404 };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const result = await cronList[params.name](req);
|
|
21
|
-
return result;
|
|
22
|
-
}
|
|
1
|
+
import cronList from './utils/cronList.js';
|
|
2
|
+
|
|
3
|
+
export default async function cronApi(req) {
|
|
4
|
+
const {
|
|
5
|
+
params = {}, user = {}, hostname,
|
|
6
|
+
} = req;
|
|
7
|
+
|
|
8
|
+
if ((!user.uid || !user.user_type?.includes('admin')) && !hostname?.includes('localhost')) {
|
|
9
|
+
return { message: 'access restricted', status: 403 };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (params.name === 'list') {
|
|
13
|
+
return { data: Object.keys(cronList || {}) };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!cronList[params.name]) {
|
|
17
|
+
return { message: `cron not found: ${params.name}`, status: 404 };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const result = await cronList[params.name](req);
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export default {};
|
|
1
|
+
export default {};
|
package/cron/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import cronApi from './controllers/cronApi.js';
|
|
2
|
-
import addCron from './funcs/addCron.js';
|
|
3
|
-
|
|
4
|
-
async function plugin(fastify, config = {}) {
|
|
5
|
-
const prefix = config.prefix || '/api';
|
|
6
|
-
fastify.decorate('addCron', addCron);
|
|
7
|
-
fastify.get(`${prefix}/cron/:name`, {}, cronApi);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export default plugin;
|
|
1
|
+
import cronApi from './controllers/cronApi.js';
|
|
2
|
+
import addCron from './funcs/addCron.js';
|
|
3
|
+
|
|
4
|
+
async function plugin(fastify, config = {}) {
|
|
5
|
+
const prefix = config.prefix || '/api';
|
|
6
|
+
fastify.decorate('addCron', addCron);
|
|
7
|
+
fastify.get(`${prefix}/cron/:name`, {}, cronApi);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default plugin;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
dataDelete, getTemplate, getAccess, applyHook,
|
|
2
|
+
dataDelete, getTemplate, getAccess, applyHook,
|
|
3
3
|
} from '../../utils.js';
|
|
4
4
|
|
|
5
5
|
export default async function deleteCrud(req) {
|
|
@@ -11,18 +11,13 @@ export default async function deleteCrud(req) {
|
|
|
11
11
|
return { message: hookData?.message, status: hookData?.status };
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const { table: del, id } = hookData || tokenData || (config.auth?.disable ? req.params : {});
|
|
19
|
-
const { actions = [] } = await getAccess({ table: del, id, user }) || {};
|
|
14
|
+
const { table: del, id } = hookData || req.params || {};
|
|
15
|
+
const { actions = [], scope, my } = await getAccess({ table: del, id, user }) || {};
|
|
20
16
|
|
|
21
|
-
if (!actions.includes('del')) {
|
|
17
|
+
if (!actions.includes('del') || (scope === 'my' && !my)) {
|
|
22
18
|
return { message: 'access restricted', status: 403 };
|
|
23
19
|
}
|
|
24
20
|
const loadTemplate = await getTemplate('table', del);
|
|
25
|
-
|
|
26
21
|
const { table } = loadTemplate || hookData || req.params || {};
|
|
27
22
|
|
|
28
23
|
if (!table) return { status: 404, message: 'table is required' };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
applyHook, getAccess, getTemplate, checkXSS, dataInsert,
|
|
2
|
+
applyHook, getAccess, getTemplate, checkXSS, dataInsert,
|
|
3
3
|
} from '../../utils.js';
|
|
4
4
|
|
|
5
5
|
export default async function insert(req) {
|
|
@@ -10,22 +10,21 @@ export default async function insert(req) {
|
|
|
10
10
|
if (hookData?.message && hookData?.status) {
|
|
11
11
|
return { message: hookData?.message, status: hookData?.status };
|
|
12
12
|
}
|
|
13
|
-
const tokenData = await getToken({
|
|
14
|
-
uid: user.uid, token: params.table, mode: 'a', json: 1,
|
|
15
|
-
});
|
|
16
13
|
|
|
17
|
-
const { form, table: add } = hookData ||
|
|
14
|
+
const { form, table: add } = hookData || req.params || {};
|
|
18
15
|
|
|
19
16
|
const { actions = [] } = await getAccess({ table: add, user }) || {};
|
|
20
17
|
|
|
21
|
-
if (!actions.includes('
|
|
18
|
+
if (!actions.includes('edit')) {
|
|
19
|
+
return { message: 'access restricted', status: 403 };
|
|
20
|
+
}
|
|
22
21
|
|
|
23
22
|
if (!add) {
|
|
24
23
|
return { message: 'table is required', status: 400 };
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
const loadTemplate = await getTemplate('table', add);
|
|
28
|
-
const { table } = loadTemplate || hookData || req.params || {};
|
|
27
|
+
const { table, public: ispublic } = loadTemplate || hookData || req.params || {};
|
|
29
28
|
if (!table) {
|
|
30
29
|
return { message: 'table not found', status: 404 };
|
|
31
30
|
}
|
|
@@ -39,7 +38,7 @@ export default async function insert(req) {
|
|
|
39
38
|
return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
const uid = user?.uid;
|
|
41
|
+
const uid = ispublic ? (user?.uid || '1') : user?.uid;
|
|
43
42
|
if ((add || table) !== 'admin.users') {
|
|
44
43
|
Object.assign(body, { uid, editor_id: uid });
|
|
45
44
|
}
|
|
@@ -1,26 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
|
-
pgClients, applyHook, getAccess, getTemplate, checkXSS, dataInsert, dataUpdate,
|
|
2
|
+
pgClients, applyHook, getAccess, getTemplate, checkXSS, dataInsert, dataUpdate,
|
|
3
3
|
} from '../../utils.js';
|
|
4
|
-
import config from '../../config.js';
|
|
5
4
|
|
|
6
5
|
export default async function update(req) {
|
|
7
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
user, params = {}, body = {},
|
|
8
|
+
} = req || {};
|
|
8
9
|
const hookData = await applyHook('preUpdate', {
|
|
9
10
|
table: params?.table, id: params?.id, user,
|
|
10
11
|
});
|
|
11
|
-
|
|
12
12
|
if (hookData?.message && hookData?.status) {
|
|
13
13
|
return { message: hookData?.message, status: hookData?.status };
|
|
14
14
|
}
|
|
15
|
-
const tokenData = await getToken({
|
|
16
|
-
uid: user.uid, token: body.token || params.table, mode: 'w', json: 1,
|
|
17
|
-
});
|
|
18
15
|
|
|
19
|
-
const { form, table: edit, id } = hookData ||
|
|
16
|
+
const { form, table: edit, id } = hookData || req.params;
|
|
20
17
|
|
|
21
|
-
const { actions = [] } = await getAccess({ table: edit, id, user }) || {};
|
|
18
|
+
const { actions = [], scope, my } = await getAccess({ table: edit, id, user }) || {};
|
|
22
19
|
|
|
23
|
-
if (!actions.includes('edit')) {
|
|
20
|
+
if (!actions.includes('edit') || (scope === 'my' && !my)) {
|
|
24
21
|
return { message: 'access restricted', status: 403 };
|
|
25
22
|
}
|
|
26
23
|
|
|
@@ -33,16 +30,16 @@ export default async function update(req) {
|
|
|
33
30
|
}
|
|
34
31
|
|
|
35
32
|
const loadTemplate = await getTemplate('table', edit);
|
|
36
|
-
const { table } = loadTemplate || hookData || params || {};
|
|
33
|
+
const { table, public: ispublic } = loadTemplate || hookData || req.params || {};
|
|
37
34
|
|
|
38
|
-
const uid = user?.uid;
|
|
35
|
+
const uid = ispublic ? (user?.uid || '1') : user?.uid;
|
|
39
36
|
|
|
40
37
|
const formData = form || loadTemplate?.form ? await getTemplate('form', form || loadTemplate?.form) : {};
|
|
41
38
|
|
|
42
39
|
const xssCheck = checkXSS({ body, schema: formData?.schema || formData });
|
|
43
40
|
|
|
44
41
|
if (xssCheck.error && formData?.xssCheck !== false) {
|
|
45
|
-
|
|
42
|
+
req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
|
|
46
43
|
return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
|
|
47
44
|
}
|
|
48
45
|
|
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
const xssInjection = [
|
|
2
|
-
'onkeypress=',
|
|
3
|
-
'onkeyup=',
|
|
4
|
-
'ondblclick=',
|
|
5
|
-
'onerror=',
|
|
6
|
-
'onmouseover=',
|
|
7
|
-
'<meta',
|
|
8
|
-
'<script',
|
|
9
|
-
'vascript:',
|
|
10
|
-
'onkeydown=',
|
|
11
|
-
'onmousedown=',
|
|
12
|
-
'onmouseenter=',
|
|
13
|
-
'onmouseleave=',
|
|
14
|
-
'onmousemove=',
|
|
15
|
-
'onmouseout=',
|
|
16
|
-
'onmouseup=',
|
|
17
|
-
'onmousewheel=',
|
|
18
|
-
'onpaste=',
|
|
19
|
-
'onscroll=',
|
|
20
|
-
'onwheel=',
|
|
21
|
-
'javascript:',
|
|
22
|
-
'\\x',
|
|
23
|
-
'eval(',
|
|
24
|
-
'onmouseover=',
|
|
25
|
-
'action=',
|
|
26
|
-
'xlink:',
|
|
27
|
-
'allowscriptaccess',
|
|
28
|
-
'href=',
|
|
29
|
-
'behavior:',
|
|
30
|
-
'onreadystatechange=',
|
|
31
|
-
'onstart=',
|
|
32
|
-
'offline=',
|
|
33
|
-
'onabort=',
|
|
34
|
-
'onafterprint=',
|
|
35
|
-
'onbeforeonload=',
|
|
36
|
-
'onbeforeprint=',
|
|
37
|
-
'onblur=',
|
|
38
|
-
'oncanplay=',
|
|
39
|
-
'oncanplaythrough=',
|
|
40
|
-
'onchange=',
|
|
41
|
-
'onclick=',
|
|
42
|
-
'oncontextmenu=',
|
|
43
|
-
'ondblclick=',
|
|
44
|
-
'ondrag=',
|
|
45
|
-
'ondragend=',
|
|
46
|
-
'ondragenter=',
|
|
47
|
-
'ondragleave=',
|
|
48
|
-
'ondragover=',
|
|
49
|
-
'ondragstart=',
|
|
50
|
-
'ondrop=',
|
|
51
|
-
'ondurationchange=',
|
|
52
|
-
'onemptied=',
|
|
53
|
-
'onended=',
|
|
54
|
-
'onerror=',
|
|
55
|
-
'onfocus=',
|
|
56
|
-
'onformchange=',
|
|
57
|
-
'onforminput=',
|
|
58
|
-
'onhaschange=',
|
|
59
|
-
'oninput=',
|
|
60
|
-
'oninvalid=',
|
|
61
|
-
'onkeydown=',
|
|
62
|
-
'onkeypress=',
|
|
63
|
-
'onkeyup=',
|
|
64
|
-
'onload=',
|
|
65
|
-
'onloadeddata=',
|
|
66
|
-
'onloadedmetadata=',
|
|
67
|
-
'onloadstart=',
|
|
68
|
-
'alert(',
|
|
69
|
-
'script:',
|
|
70
|
-
];
|
|
71
|
-
|
|
72
|
-
export default xssInjection;
|
|
1
|
+
const xssInjection = [
|
|
2
|
+
'onkeypress=',
|
|
3
|
+
'onkeyup=',
|
|
4
|
+
'ondblclick=',
|
|
5
|
+
'onerror=',
|
|
6
|
+
'onmouseover=',
|
|
7
|
+
'<meta',
|
|
8
|
+
'<script',
|
|
9
|
+
'vascript:',
|
|
10
|
+
'onkeydown=',
|
|
11
|
+
'onmousedown=',
|
|
12
|
+
'onmouseenter=',
|
|
13
|
+
'onmouseleave=',
|
|
14
|
+
'onmousemove=',
|
|
15
|
+
'onmouseout=',
|
|
16
|
+
'onmouseup=',
|
|
17
|
+
'onmousewheel=',
|
|
18
|
+
'onpaste=',
|
|
19
|
+
'onscroll=',
|
|
20
|
+
'onwheel=',
|
|
21
|
+
'javascript:',
|
|
22
|
+
'\\x',
|
|
23
|
+
'eval(',
|
|
24
|
+
'onmouseover=',
|
|
25
|
+
'action=',
|
|
26
|
+
'xlink:',
|
|
27
|
+
'allowscriptaccess',
|
|
28
|
+
'href=',
|
|
29
|
+
'behavior:',
|
|
30
|
+
'onreadystatechange=',
|
|
31
|
+
'onstart=',
|
|
32
|
+
'offline=',
|
|
33
|
+
'onabort=',
|
|
34
|
+
'onafterprint=',
|
|
35
|
+
'onbeforeonload=',
|
|
36
|
+
'onbeforeprint=',
|
|
37
|
+
'onblur=',
|
|
38
|
+
'oncanplay=',
|
|
39
|
+
'oncanplaythrough=',
|
|
40
|
+
'onchange=',
|
|
41
|
+
'onclick=',
|
|
42
|
+
'oncontextmenu=',
|
|
43
|
+
'ondblclick=',
|
|
44
|
+
'ondrag=',
|
|
45
|
+
'ondragend=',
|
|
46
|
+
'ondragenter=',
|
|
47
|
+
'ondragleave=',
|
|
48
|
+
'ondragover=',
|
|
49
|
+
'ondragstart=',
|
|
50
|
+
'ondrop=',
|
|
51
|
+
'ondurationchange=',
|
|
52
|
+
'onemptied=',
|
|
53
|
+
'onended=',
|
|
54
|
+
'onerror=',
|
|
55
|
+
'onfocus=',
|
|
56
|
+
'onformchange=',
|
|
57
|
+
'onforminput=',
|
|
58
|
+
'onhaschange=',
|
|
59
|
+
'oninput=',
|
|
60
|
+
'oninvalid=',
|
|
61
|
+
'onkeydown=',
|
|
62
|
+
'onkeypress=',
|
|
63
|
+
'onkeyup=',
|
|
64
|
+
'onload=',
|
|
65
|
+
'onloadeddata=',
|
|
66
|
+
'onloadedmetadata=',
|
|
67
|
+
'onloadstart=',
|
|
68
|
+
'alert(',
|
|
69
|
+
'script:',
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
export default xssInjection;
|
package/crud/funcs/getAccess.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import getMeta from '../../pg/funcs/getMeta.js';
|
|
2
2
|
import getTemplate from '../../table/controllers/utils/getTemplate.js';
|
|
3
3
|
import config from '../../config.js';
|
|
4
4
|
import pgClients from '../../pg/pgClients.js';
|
|
5
|
-
import applyHook from '../../hook/funcs/applyHook.js';
|
|
6
5
|
|
|
7
6
|
const q = `select a.route_id as id, coalesce(b.actions,array['get']) as actions, b.scope
|
|
8
7
|
from admin.routes a
|
|
@@ -20,24 +19,38 @@ left join admin.user_roles d on
|
|
|
20
19
|
end )
|
|
21
20
|
where $1 in (a.route_id, a.alias) and $2 in (b.user_uid, d.user_uid)`;
|
|
22
21
|
|
|
23
|
-
export default async function getAccess({ table, user
|
|
24
|
-
|
|
22
|
+
export default async function getAccess({ table, id, user }) {
|
|
23
|
+
const { client: pg } = pgClients || {};
|
|
24
|
+
const { uid, user_type: userType } = user || {};
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
if (hookData) return hookData;
|
|
26
|
+
if (!table || !pg.pk?.['admin.access']) return null;
|
|
28
27
|
|
|
29
|
-
const { uid } = user;
|
|
30
28
|
const body = await getTemplate('table', table) || {};
|
|
31
29
|
|
|
32
|
-
if (config.auth?.disable || user?.user_type?.includes('admin') || body?.public
|
|
33
|
-
return { actions: ['get'
|
|
30
|
+
if (config.auth?.disable || user?.user_type?.includes('admin') || body?.public) {
|
|
31
|
+
return { actions: ['get', 'edit', 'del'], my: true, query: '1=1' };
|
|
34
32
|
}
|
|
35
33
|
|
|
36
34
|
if (!uid || !body?.table) return null;
|
|
37
35
|
|
|
38
|
-
const { scope, actions = [] } = await
|
|
36
|
+
const { scope = 'my', actions = [] } = await pg.query(q, [table, uid]).then((res) => res.rows?.[0] || {});
|
|
37
|
+
|
|
38
|
+
const { columns = [] } = await getMeta({ table: body?.table });
|
|
39
|
+
|
|
40
|
+
const query = userType?.includes('admin') ? '1=1' : {
|
|
41
|
+
my: `uid='${uid}'`,
|
|
42
|
+
responsible: columns.map((el) => el?.name || el).includes('responsible_id')
|
|
43
|
+
? `responsible_id='${uid}'`
|
|
44
|
+
: `uid='${uid}'`,
|
|
45
|
+
all: '1=1',
|
|
46
|
+
}[scope];
|
|
47
|
+
|
|
48
|
+
const { my } = pg.pk?.[body?.table] && id
|
|
49
|
+
? await pg.query(`select uid=$1 as my from ${body?.table} where ${pg.pk?.[body?.table]}=$2`, [uid, id])
|
|
50
|
+
.then((res) => res.rows?.[0] || {})
|
|
51
|
+
: {};
|
|
39
52
|
|
|
40
53
|
return {
|
|
41
|
-
scope, actions, query
|
|
54
|
+
scope, actions, query, my,
|
|
42
55
|
};
|
|
43
56
|
}
|
package/crud/funcs/getToken.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import getRedis from '../../redis/funcs/getRedis.js';
|
|
2
|
-
import config from '../../config.js';
|
|
3
|
-
|
|
4
|
-
function sprintf(str, ...args) {
|
|
5
|
-
return str.replace(/%s/g, () => args.shift());
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const keys = {
|
|
9
|
-
r: '%s:token:view:%s',
|
|
10
|
-
a: '%s:token:add:%s',
|
|
11
|
-
w: '%s:token:edit:%s',
|
|
12
|
-
e: '%s:token:exec:%s',
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
async function getToken({
|
|
16
|
-
uid, token, mode = 'r', json,
|
|
17
|
-
}) {
|
|
18
|
-
if (mode === 'r') return token;
|
|
19
|
-
|
|
20
|
-
const rclient = getRedis({ db: 0 });
|
|
21
|
-
|
|
22
|
-
const key = sprintf(keys[mode], config?.pg?.database, uid?.toString());
|
|
23
|
-
const id = await rclient.hget(key, token);
|
|
24
|
-
return json && id?.[0] === '{' ? JSON.parse(id) : id;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export default getToken;
|
|
1
|
+
import getRedis from '../../redis/funcs/getRedis.js';
|
|
2
|
+
import config from '../../config.js';
|
|
3
|
+
|
|
4
|
+
function sprintf(str, ...args) {
|
|
5
|
+
return str.replace(/%s/g, () => args.shift());
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const keys = {
|
|
9
|
+
r: '%s:token:view:%s',
|
|
10
|
+
a: '%s:token:add:%s',
|
|
11
|
+
w: '%s:token:edit:%s',
|
|
12
|
+
e: '%s:token:exec:%s',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
async function getToken({
|
|
16
|
+
uid, token, mode = 'r', json,
|
|
17
|
+
}) {
|
|
18
|
+
if (mode === 'r') return token;
|
|
19
|
+
|
|
20
|
+
const rclient = getRedis({ db: 0 });
|
|
21
|
+
|
|
22
|
+
const key = sprintf(keys[mode], config?.pg?.database, uid?.toString());
|
|
23
|
+
const id = await rclient.hget(key, token);
|
|
24
|
+
return json && id?.[0] === '{' ? JSON.parse(id) : id;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default getToken;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { access } from 'fs/promises';
|
|
2
|
-
|
|
3
|
-
const isFileExists = async (filepath) => {
|
|
4
|
-
try {
|
|
5
|
-
await access(filepath);
|
|
6
|
-
return true;
|
|
7
|
-
}
|
|
8
|
-
catch (err) {
|
|
9
|
-
return false;
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export default isFileExists;
|
|
1
|
+
import { access } from 'fs/promises';
|
|
2
|
+
|
|
3
|
+
const isFileExists = async (filepath) => {
|
|
4
|
+
try {
|
|
5
|
+
await access(filepath);
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
catch (err) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export default isFileExists;
|