@opengis/fastify-table 1.1.45 → 1.1.47

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 (129) hide show
  1. package/Changelog.md +1 -1
  2. package/README.md +26 -26
  3. package/config.js +10 -10
  4. package/cron/controllers/cronApi.js +22 -22
  5. package/cron/controllers/utils/cronList.js +1 -1
  6. package/cron/index.js +10 -10
  7. package/crud/controllers/deleteCrud.js +9 -4
  8. package/crud/controllers/insert.js +9 -6
  9. package/crud/controllers/update.js +13 -10
  10. package/crud/controllers/utils/checkXSS.js +3 -2
  11. package/crud/controllers/utils/xssInjection.js +72 -72
  12. package/crud/funcs/getAccess.js +14 -13
  13. package/crud/funcs/getToken.js +27 -27
  14. package/crud/funcs/isFileExists.js +13 -13
  15. package/crud/funcs/setToken.js +53 -53
  16. package/docs/.vitepress/abbr.mjs +26 -0
  17. package/docs/.vitepress/config.mjs +127 -0
  18. package/docs/.vitepress/navigation.mjs +82 -0
  19. package/docs/.vitepress/theme/Layout.vue +17 -0
  20. package/docs/.vitepress/theme/components/NavigationLinks.vue +102 -0
  21. package/docs/.vitepress/theme/components/Panzoom.vue +169 -0
  22. package/docs/.vitepress/theme/index.mjs +15 -0
  23. package/docs/.vitepress/theme/style.scss +163 -0
  24. package/docs/abbr.json +4 -0
  25. package/docs/api/cron/cronApi.md +56 -0
  26. package/docs/api/crud/deleteCrud.md +58 -0
  27. package/docs/api/crud/insert.md +82 -0
  28. package/docs/api/crud/update.md +85 -0
  29. package/docs/api/index.md +47 -0
  30. package/docs/api/notification/testEmail.md +91 -0
  31. package/docs/api/table/card.md +73 -0
  32. package/docs/api/table/data.md +134 -0
  33. package/docs/api/table/export.md +60 -0
  34. package/docs/api/table/filter.md +104 -0
  35. package/docs/api/table/form.md +126 -0
  36. package/docs/api/table/search.md +123 -0
  37. package/docs/api/table/suggest.md +156 -0
  38. package/docs/api/table/table.md +107 -0
  39. package/docs/api/user/user.cls.id.md +77 -0
  40. package/docs/api/user/user.cls.md +49 -0
  41. package/docs/api/user/user.cls.post.md +62 -0
  42. package/docs/api/user/user.info.md +37 -0
  43. package/docs/api/utils/logger.file.md +61 -0
  44. package/docs/api/utils/next.id.md +34 -0
  45. package/docs/api/utils/properties.add.md +127 -0
  46. package/docs/api/utils/properties.get.md +73 -0
  47. package/docs/api/utils/status.monitor.md +36 -0
  48. package/docs/api/widget/widget.del.md +76 -0
  49. package/docs/api/widget/widget.get.md +233 -0
  50. package/docs/api/widget/widget.set.md +88 -0
  51. package/docs/db/admin.md +947 -0
  52. package/docs/db/crm.md +564 -0
  53. package/docs/db/index.md +9 -0
  54. package/docs/db/log.md +204 -0
  55. package/docs/hook/card/afterCard.md +20 -0
  56. package/docs/hook/card/preCard.md +25 -0
  57. package/docs/hook/data/afterData.md +26 -0
  58. package/docs/hook/data/preData.md +26 -0
  59. package/docs/hook/deleteCrud/afterDelete.md +21 -0
  60. package/docs/hook/deleteCrud/preDelete.md +26 -0
  61. package/docs/hook/form/afterForm.md +19 -0
  62. package/docs/hook/form/preForm.md +26 -0
  63. package/docs/hook/getTemplate/afterTemplate.md +24 -0
  64. package/docs/hook/getTemplate/preTemplate.md +29 -0
  65. package/docs/hook/index.md +45 -0
  66. package/docs/hook/insert/afterInsert.md +41 -0
  67. package/docs/hook/insert/preInsert.md +25 -0
  68. package/docs/hook/table/afterTable.md +20 -0
  69. package/docs/hook/table/preTable.md +25 -0
  70. package/docs/hook/update/afterUpdate.md +41 -0
  71. package/docs/hook/update/preUpdate.md +25 -0
  72. package/docs/index.md +42 -0
  73. package/docs/public/fastify-dark.svg +4 -0
  74. package/docs/public/fastify.svg +1 -0
  75. package/docs/public/logo-short-dark.svg +12 -0
  76. package/docs/public/logo-short.svg +11 -0
  77. package/docs/public/logo.svg +19 -0
  78. package/docs/readme/index.md +121 -0
  79. package/docs/templates/card.md +83 -0
  80. package/docs/templates/cls.md +29 -0
  81. package/docs/templates/filters.md +91 -0
  82. package/docs/templates/forms.md +139 -0
  83. package/docs/templates/image.png +0 -0
  84. package/docs/templates/index.md +28 -0
  85. package/docs/templates/select.md +90 -0
  86. package/docs/templates/table.md +162 -0
  87. package/docs/utils/cron/addCron.md +29 -0
  88. package/docs/utils/crud/dataInsert.md +52 -0
  89. package/docs/utils/crud/dataUpdate.md +53 -0
  90. package/docs/utils/crud/getOpt.md +34 -0
  91. package/docs/utils/crud/isFileExists.md +38 -0
  92. package/docs/utils/crud/setOpt.md +38 -0
  93. package/docs/utils/hook/addHook.md +74 -0
  94. package/docs/utils/hook/applyHook.md +64 -0
  95. package/docs/utils/index.md +48 -0
  96. package/docs/utils/notification/addNotification.md +28 -0
  97. package/docs/utils/notification/notification.md +41 -0
  98. package/docs/utils/pg/autoIndex.md +22 -0
  99. package/docs/utils/pg/getMeta.md +59 -0
  100. package/docs/utils/pg/getPG.md +34 -0
  101. package/docs/utils/pg/init.md +30 -0
  102. package/docs/utils/pg/pg.md +70 -0
  103. package/docs/utils/redis/getRedis.md +36 -0
  104. package/docs/utils/redis/rclient.md +74 -0
  105. package/docs/utils/table/getForm.md +69 -0
  106. package/docs/utils/table/getMeta.md +56 -0
  107. package/docs/utils/table/getSelect.md +39 -0
  108. package/docs/utils/table/getSelectMeta.md +47 -0
  109. package/docs/utils/table/getTable.md +78 -0
  110. package/logger/createFileStream.js +1 -1
  111. package/package.json +15 -3
  112. package/redis/funcs/getRedis.js +23 -23
  113. package/server/migrations/log.sql +80 -80
  114. package/table/controllers/data.js +31 -23
  115. package/table/controllers/table.js +26 -22
  116. package/table/index.js +3 -50
  117. package/table/schema.js +54 -0
  118. package/test/api/crud.xss.test.js +38 -22
  119. package/test/config.example +18 -18
  120. package/test/funcs/pg.test.js +34 -34
  121. package/test/funcs/redis.test.js +19 -19
  122. package/test/templates/cls/test.json +9 -9
  123. package/test/templates/form/cp_building.form.json +32 -32
  124. package/test/templates/select/account_id.json +3 -3
  125. package/test/templates/select/storage.data.json +2 -2
  126. package/test/templates/table/gis.dataset.table.json +20 -20
  127. package/util/controllers/next.id.js +4 -4
  128. package/util/controllers/properties.get.js +19 -19
  129. package/util/index.js +23 -23
package/Changelog.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # fastify-table
2
2
 
3
- ## 1.1.45 - 22.10.2024
3
+ ## 1.1.46 - 22.10.2024
4
4
 
5
5
  - addHook params refactor
6
6
  - add handlebars to utils
package/README.md CHANGED
@@ -1,26 +1,26 @@
1
- # fastify-table
2
-
3
- [![NPM version](https://img.shields.io/npm/v/@opengis/fastify-table)](https://www.npmjs.com/package/@opengis/fastify-table)
4
- [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](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
+ [![NPM version](https://img.shields.io/npm/v/@opengis/fastify-table)](https://www.npmjs.com/package/@opengis/fastify-table)
4
+ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](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, getToken, config,
3
3
  } from '../../utils.js';
4
4
 
5
5
  export default async function deleteCrud(req) {
@@ -11,13 +11,18 @@ export default async function deleteCrud(req) {
11
11
  return { message: hookData?.message, status: hookData?.status };
12
12
  }
13
13
 
14
- const { table: del, id } = hookData || req.params || {};
15
- const { actions = [], scope, my } = await getAccess({ table: del, id, user }) || {};
14
+ const tokenData = await getToken({
15
+ uid: user.uid, token: params.table, mode: 'w', json: 1,
16
+ });
17
+
18
+ const { table: del, id } = hookData || tokenData || (config.auth?.disable ? req.params : {});
19
+ const { actions = [] } = await getAccess({ table: del, id, user }) || {};
16
20
 
17
- if (!actions.includes('del') || (scope === 'my' && !my)) {
21
+ if (!actions.includes('del')) {
18
22
  return { message: 'access restricted', status: 403 };
19
23
  }
20
24
  const loadTemplate = await getTemplate('table', del);
25
+
21
26
  const { table } = loadTemplate || hookData || req.params || {};
22
27
 
23
28
  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, getToken, config,
3
3
  } from '../../utils.js';
4
4
 
5
5
  export default async function insert(req) {
@@ -10,12 +10,15 @@ 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
-
14
- const { form, table: add } = hookData || req.params || {};
13
+ const tokenData = await getToken({
14
+ uid: user.uid, token: params.table, mode: 'w', json: 1,
15
+ });
16
+ // console.log(tokenData);
17
+ const { form, table: add } = hookData || tokenData || (config.auth?.disable ? req.params : {});
15
18
 
16
19
  const { actions = [] } = await getAccess({ table: add, user }) || {};
17
20
 
18
- if (!actions.includes('edit')) {
21
+ if (!actions.includes('add')) {
19
22
  return { message: 'access restricted', status: 403 };
20
23
  }
21
24
 
@@ -24,7 +27,7 @@ export default async function insert(req) {
24
27
  }
25
28
 
26
29
  const loadTemplate = await getTemplate('table', add);
27
- const { table, public: ispublic } = loadTemplate || hookData || req.params || {};
30
+ const { table } = loadTemplate || hookData || req.params || {};
28
31
  if (!table) {
29
32
  return { message: 'table not found', status: 404 };
30
33
  }
@@ -38,7 +41,7 @@ export default async function insert(req) {
38
41
  return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
39
42
  }
40
43
 
41
- const uid = ispublic ? (user?.uid || '1') : user?.uid;
44
+ const uid = user?.uid;
42
45
  if ((add || table) !== 'admin.users') {
43
46
  Object.assign(body, { uid, editor_id: uid });
44
47
  }
@@ -1,23 +1,26 @@
1
1
  import {
2
- pgClients, applyHook, getAccess, getTemplate, checkXSS, dataInsert, dataUpdate,
2
+ pgClients, applyHook, getAccess, getTemplate, checkXSS, dataInsert, dataUpdate, logger, getToken,
3
3
  } from '../../utils.js';
4
+ import config from '../../config.js';
4
5
 
5
6
  export default async function update(req) {
6
- const {
7
- user, params = {}, body = {},
8
- } = req || {};
7
+ const { user, params = {}, body = {} } = req;
9
8
  const hookData = await applyHook('preUpdate', {
10
9
  table: params?.table, id: params?.id, user,
11
10
  });
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
+ });
15
18
 
16
- const { form, table: edit, id } = hookData || req.params;
19
+ const { form, table: edit, id } = hookData || tokenData || (config.auth?.disable ? params : {});
17
20
 
18
- const { actions = [], scope, my } = await getAccess({ table: edit, id, user }) || {};
21
+ const { actions = [] } = await getAccess({ table: edit, id, user }) || {};
19
22
 
20
- if (!actions.includes('edit') || (scope === 'my' && !my)) {
23
+ if (!actions.includes('edit')) {
21
24
  return { message: 'access restricted', status: 403 };
22
25
  }
23
26
 
@@ -30,16 +33,16 @@ export default async function update(req) {
30
33
  }
31
34
 
32
35
  const loadTemplate = await getTemplate('table', edit);
33
- const { table, public: ispublic } = loadTemplate || hookData || req.params || {};
36
+ const { table } = loadTemplate || hookData || params || {};
34
37
 
35
- const uid = ispublic ? (user?.uid || '1') : user?.uid;
38
+ const uid = user?.uid;
36
39
 
37
40
  const formData = form || loadTemplate?.form ? await getTemplate('form', form || loadTemplate?.form) : {};
38
41
 
39
42
  const xssCheck = checkXSS({ body, schema: formData?.schema || formData });
40
43
 
41
44
  if (xssCheck.error && formData?.xssCheck !== false) {
42
- req.log.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
45
+ logger.warn({ name: 'injection/xss', msg: xssCheck.error, table }, req);
43
46
  return { message: 'Дані містять заборонені символи. Приберіть їх та спробуйте ще раз', status: 409 };
44
47
  }
45
48
 
@@ -16,15 +16,16 @@ function checkXSS({ body, schema = {} }) {
16
16
  // escape arrows on non-RTE
17
17
  Object.keys(body)
18
18
  .filter((key) => ['<', '>'].find((el) => body[key]?.includes?.(el))
19
- && !['Summernote', 'Tiny', 'Ace'].includes(schema[key]?.type))
19
+ && !['Summernote', 'Tiny', 'Ace'].includes(schema?.[key]?.type))
20
20
  ?.forEach((key) => {
21
21
  Object.assign(body, { [key]: body[key].replace(/</g, '&lt;').replace(/>/g, '&gt;') });
22
22
  });
23
+
23
24
  // try { } catch (err) { return { error: err.toString() }; }
24
25
 
25
26
  if (!stopWords.length) return { body };
26
27
 
27
- const disabledCheckFields = Object.keys(schema)?.filter((el) => schema[el]?.xssCheck === false); // exclude specific columns
28
+ const disabledCheckFields = Object.keys(schema || {})?.filter((el) => schema?.[el]?.xssCheck === false); // exclude specific columns
28
29
 
29
30
  // check RTE
30
31
  /* const richTextFields = Object.keys(schema).filter((el) => ['Summernote', 'Tiny', 'Ace'].includes(schema[el]?.type));
@@ -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;
@@ -1,7 +1,8 @@
1
- import getMeta from '../../pg/funcs/getMeta.js';
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';
5
6
 
6
7
  const q = `select a.route_id as id, coalesce(b.actions,array['get']) as actions, b.scope
7
8
  from admin.routes a
@@ -19,24 +20,24 @@ left join admin.user_roles d on
19
20
  end )
20
21
  where $1 in (a.route_id, a.alias) and $2 in (b.user_uid, d.user_uid)`;
21
22
 
22
- export default async function getAccess({ table, id, user }) {
23
- const { client: pg } = pgClients || {};
24
- const { uid, user_type: userType } = user || {};
23
+ export default async function getAccess({ table, user = {} }) {
24
+ if (!table) return null;
25
25
 
26
- if (!uid || !table) return null;
27
-
28
- if (!pg.pk?.['admin.access']) return null;
26
+ const hookData = await applyHook('getAccess', { table, user });
27
+ if (hookData) return hookData;
29
28
 
29
+ const { uid } = user;
30
30
  const body = await getTemplate('table', table) || {};
31
- if (!body?.table) return null;
32
31
 
33
32
  if (config.auth?.disable || user?.user_type?.includes('admin') || body?.public) {
34
- return { actions: ['get', 'edit', 'del'], my: true, query: '1=1' };
33
+ return { actions: ['get'].concat(body.actions || body.action_default || []), query: '1=1' };
35
34
  }
36
35
 
37
- const { scope = 'my', actions = [] } = await pg.query(q, [table, uid]).then((res) => res.rows?.[0] || {});
36
+ if (!uid || !body?.table) return null;
37
+
38
+ const { scope = 'my', actions = [] } = await pgClients.client.query(q, [table, uid]).then((res) => res.rows?.[0] || {});
38
39
 
39
- const { columns = [] } = await getMeta({ table: body?.table });
40
+ /* const { columns = [] } = await getMeta({ table: body?.table });
40
41
 
41
42
  const query = userType?.includes('admin') ? '1=1' : {
42
43
  my: `uid='${uid}'`,
@@ -49,9 +50,9 @@ export default async function getAccess({ table, id, user }) {
49
50
  const { my } = pg.pk?.[body?.table] && id
50
51
  ? await pg.query(`select uid=$1 as my from ${body?.table} where ${pg.pk?.[body?.table]}=$2`, [uid, id])
51
52
  .then((res) => res.rows?.[0] || {})
52
- : {};
53
+ : {}; */
53
54
 
54
55
  return {
55
- scope, actions, query, my,
56
+ scope, actions,
56
57
  };
57
58
  }
@@ -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;