@modular-rest/server 1.11.13 → 1.11.15

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 (181) hide show
  1. package/.nvmrc +1 -0
  2. package/.prettierrc.json +9 -0
  3. package/.releaserc.json +24 -0
  4. package/README.md +79 -94
  5. package/dist/application.d.ts +29 -0
  6. package/dist/application.js +217 -0
  7. package/dist/class/cms_trigger.d.ts +52 -0
  8. package/dist/class/cms_trigger.js +47 -0
  9. package/dist/class/collection_definition.d.ts +112 -0
  10. package/dist/class/collection_definition.js +87 -0
  11. package/dist/class/combinator.d.ts +43 -0
  12. package/dist/class/combinator.js +174 -0
  13. package/dist/class/database_trigger.d.ts +90 -0
  14. package/dist/class/database_trigger.js +64 -0
  15. package/dist/class/db_schemas.d.ts +25 -0
  16. package/dist/class/db_schemas.js +28 -0
  17. package/dist/class/directory.d.ts +20 -0
  18. package/dist/class/directory.js +87 -0
  19. package/dist/class/paginator.d.ts +31 -0
  20. package/dist/class/paginator.js +43 -0
  21. package/dist/class/reply.d.ts +29 -0
  22. package/dist/class/reply.js +44 -0
  23. package/dist/class/security.d.ts +186 -0
  24. package/dist/class/security.js +178 -0
  25. package/dist/class/trigger_operator.d.ts +92 -0
  26. package/dist/class/trigger_operator.js +99 -0
  27. package/dist/class/user.d.ts +81 -0
  28. package/dist/class/user.js +151 -0
  29. package/dist/class/validator.d.ts +19 -0
  30. package/dist/class/validator.js +101 -0
  31. package/dist/config.d.ts +113 -0
  32. package/dist/config.js +26 -0
  33. package/dist/defult-permissions.d.ts +2 -0
  34. package/dist/defult-permissions.js +31 -0
  35. package/dist/events.d.ts +23 -0
  36. package/dist/events.js +47 -0
  37. package/dist/helper/data_insertion.d.ts +38 -0
  38. package/dist/helper/data_insertion.js +110 -0
  39. package/dist/helper/presetup_services.d.ts +60 -0
  40. package/dist/helper/presetup_services.js +108 -0
  41. package/dist/index.d.ts +118 -0
  42. package/dist/index.js +79 -0
  43. package/dist/middlewares.d.ts +53 -0
  44. package/dist/middlewares.js +106 -0
  45. package/dist/play-test.d.ts +1 -0
  46. package/dist/play-test.js +9 -0
  47. package/dist/services/data_provider/router.d.ts +4 -0
  48. package/dist/services/data_provider/router.js +412 -0
  49. package/dist/services/data_provider/service.d.ts +132 -0
  50. package/dist/services/data_provider/service.js +253 -0
  51. package/dist/services/data_provider/typeCasters.d.ts +9 -0
  52. package/dist/services/data_provider/typeCasters.js +18 -0
  53. package/dist/services/file/db.d.ts +1 -0
  54. package/dist/services/file/db.js +31 -0
  55. package/dist/services/file/router.d.ts +4 -0
  56. package/dist/services/file/router.js +115 -0
  57. package/dist/services/file/service.d.ts +204 -0
  58. package/dist/services/file/service.js +341 -0
  59. package/dist/services/functions/router.d.ts +4 -0
  60. package/dist/services/functions/router.js +68 -0
  61. package/dist/services/functions/service.d.ts +132 -0
  62. package/dist/services/functions/service.js +159 -0
  63. package/dist/services/jwt/router.d.ts +4 -0
  64. package/dist/services/jwt/router.js +99 -0
  65. package/dist/services/jwt/service.d.ts +97 -0
  66. package/dist/services/jwt/service.js +135 -0
  67. package/dist/services/user_manager/db.d.ts +1 -0
  68. package/dist/services/user_manager/db.js +75 -0
  69. package/dist/services/user_manager/permissionManager.d.ts +19 -0
  70. package/dist/services/user_manager/permissionManager.js +42 -0
  71. package/dist/services/user_manager/router.d.ts +4 -0
  72. package/dist/services/user_manager/router.js +195 -0
  73. package/dist/services/user_manager/service.d.ts +317 -0
  74. package/dist/services/user_manager/service.js +632 -0
  75. package/docs/.keep +0 -0
  76. package/docs/system-access-type.md +26 -0
  77. package/package.json +59 -46
  78. package/src/application.ts +206 -0
  79. package/src/class/cms_trigger.ts +62 -0
  80. package/src/class/collection_definition.ts +134 -0
  81. package/src/class/combinator.ts +176 -0
  82. package/src/class/database_trigger.ts +105 -0
  83. package/src/class/db_schemas.ts +44 -0
  84. package/src/class/{directory.js → directory.ts} +40 -18
  85. package/src/class/paginator.ts +51 -0
  86. package/src/class/reply.ts +59 -0
  87. package/src/class/security.ts +250 -0
  88. package/src/class/trigger_operator.ts +142 -0
  89. package/src/class/user.ts +199 -0
  90. package/src/class/validator.ts +123 -0
  91. package/src/config.ts +122 -0
  92. package/src/defult-permissions.ts +31 -0
  93. package/src/events.ts +59 -0
  94. package/src/helper/data_insertion.ts +94 -0
  95. package/src/helper/presetup_services.ts +96 -0
  96. package/src/index.ts +146 -0
  97. package/src/middlewares.ts +75 -0
  98. package/src/play-test.ts +8 -0
  99. package/src/services/data_provider/router.ts +484 -0
  100. package/src/services/data_provider/service.ts +306 -0
  101. package/src/services/data_provider/typeCasters.ts +15 -0
  102. package/src/services/file/db.ts +29 -0
  103. package/src/services/file/router.ts +88 -0
  104. package/src/services/file/service.ts +387 -0
  105. package/src/services/functions/router.ts +35 -0
  106. package/src/services/functions/service.ts +203 -0
  107. package/src/services/jwt/router.ts +73 -0
  108. package/src/services/jwt/service.ts +139 -0
  109. package/src/services/user_manager/db.ts +87 -0
  110. package/src/services/user_manager/permissionManager.ts +49 -0
  111. package/src/services/user_manager/router.ts +193 -0
  112. package/src/services/user_manager/service.ts +703 -0
  113. package/tsconfig.json +16 -9
  114. package/typedoc.mjs +41 -0
  115. package/LICENSE +0 -21
  116. package/package-lock.json +0 -1373
  117. package/src/application.js +0 -239
  118. package/src/class/cms_trigger.js +0 -20
  119. package/src/class/collection_definition.js +0 -33
  120. package/src/class/combinator.js +0 -133
  121. package/src/class/database_trigger.js +0 -20
  122. package/src/class/db_schemas.js +0 -18
  123. package/src/class/paginator.js +0 -31
  124. package/src/class/reply.js +0 -37
  125. package/src/class/security.js +0 -141
  126. package/src/class/trigger_operator.js +0 -39
  127. package/src/class/user.js +0 -112
  128. package/src/class/validator.js +0 -91
  129. package/src/config.js +0 -67
  130. package/src/events.js +0 -15
  131. package/src/helper/data_insertion.js +0 -64
  132. package/src/helper/presetup_services.js +0 -31
  133. package/src/index.js +0 -66
  134. package/src/middlewares.js +0 -44
  135. package/src/services/data_provider/router.js +0 -552
  136. package/src/services/data_provider/service.js +0 -262
  137. package/src/services/data_provider/typeCasters.js +0 -10
  138. package/src/services/file/db.js +0 -29
  139. package/src/services/file/router.js +0 -92
  140. package/src/services/file/service.js +0 -231
  141. package/src/services/functions/router.js +0 -37
  142. package/src/services/functions/service.js +0 -74
  143. package/src/services/jwt/router.js +0 -82
  144. package/src/services/jwt/service.js +0 -37
  145. package/src/services/user_manager/db.js +0 -83
  146. package/src/services/user_manager/permissionManager.js +0 -43
  147. package/src/services/user_manager/router.js +0 -176
  148. package/src/services/user_manager/service.js +0 -377
  149. package/types/application.d.ts +0 -97
  150. package/types/class/cms_trigger.d.ts +0 -24
  151. package/types/class/collection_definition.d.ts +0 -36
  152. package/types/class/combinator.d.ts +0 -30
  153. package/types/class/database_trigger.d.ts +0 -28
  154. package/types/class/db_schemas.d.ts +0 -2
  155. package/types/class/directory.d.ts +0 -2
  156. package/types/class/paginator.d.ts +0 -8
  157. package/types/class/reply.d.ts +0 -8
  158. package/types/class/security.d.ts +0 -109
  159. package/types/class/trigger_operator.d.ts +0 -19
  160. package/types/class/user.d.ts +0 -24
  161. package/types/class/validator.d.ts +0 -9
  162. package/types/config.d.ts +0 -101
  163. package/types/events.d.ts +0 -7
  164. package/types/helper/data_insertion.d.ts +0 -4
  165. package/types/helper/presetup_services.d.ts +0 -5
  166. package/types/index.d.ts +0 -72
  167. package/types/middlewares.d.ts +0 -10
  168. package/types/services/data_provider/router.d.ts +0 -3
  169. package/types/services/data_provider/service.d.ts +0 -40
  170. package/types/services/data_provider/typeCasters.d.ts +0 -3
  171. package/types/services/file/db.d.ts +0 -3
  172. package/types/services/file/router.d.ts +0 -3
  173. package/types/services/file/service.d.ts +0 -81
  174. package/types/services/functions/router.d.ts +0 -3
  175. package/types/services/functions/service.d.ts +0 -23
  176. package/types/services/jwt/router.d.ts +0 -3
  177. package/types/services/jwt/service.d.ts +0 -10
  178. package/types/services/user_manager/db.d.ts +0 -3
  179. package/types/services/user_manager/permissionManager.d.ts +0 -3
  180. package/types/services/user_manager/router.d.ts +0 -3
  181. package/types/services/user_manager/service.d.ts +0 -131
@@ -1,74 +0,0 @@
1
- const functions = [];
2
-
3
- /**
4
- * @typedef {import('../../class/security.js').PermissionType} PermissionType
5
- */
6
-
7
- /**
8
- * Defines a function with a given name, permission types, and callback.
9
- *
10
- * @param {Object} params - The parameters for the function.
11
- * @param {string} params.name - The name of the function.
12
- * @param {[PermissionType]} params.permissionTypes - The permission types for the function.
13
- * @param {Function} params.callback - The callback to be executed by the function.
14
- */
15
- function defineFunction({ name, permissionTypes, callback }) {
16
- // Check if the function already exists
17
- const existingFunction = functions.find((f) => f.name === name);
18
- if (existingFunction) {
19
- throw new Error(`Function with name ${name} already exists`);
20
- }
21
-
22
- // Check if the permission types provided
23
- if (!permissionTypes || !permissionTypes.length) {
24
- throw new Error(`Permission types not provided for function ${name}`);
25
- }
26
-
27
- // Check if the callback is a function
28
- if (typeof callback !== "function") {
29
- throw new Error(`Callback is not a function for function ${name}`);
30
- }
31
-
32
- // Add the function to the list of functions
33
- return { name, permissionTypes, callback };
34
- }
35
-
36
- function runFunction(name, args, user) {
37
- return new Promise((resolve, reject) => {
38
- const func = functions.find((f) => f.name === name);
39
- if (!func) {
40
- reject(new Error(`Function with name ${name} not found`));
41
- }
42
-
43
- const hasPermission = func.permissionTypes.some((permissionType) =>
44
- user.hasPermission(permissionType)
45
- );
46
-
47
- if (hasPermission == false) {
48
- reject(
49
- new Error(`User does not have permission to run function ${name}:
50
- Function permissions: ${func.permissionTypes}
51
- User permissions: ${
52
- user.getBrief().permissionGroup.validPermissionTypes
53
- }
54
- `)
55
- );
56
- } else {
57
- try {
58
- resolve(func.callback(args));
59
- } catch (e) {
60
- reject(e);
61
- }
62
- }
63
- });
64
- }
65
-
66
- function addFunction(func) {
67
- functions.push(func);
68
- }
69
-
70
- module.exports = {
71
- defineFunction,
72
- runFunction,
73
- addFunction,
74
- };
@@ -1,82 +0,0 @@
1
- let Router = require('koa-router');
2
- let validateObject = require('../../class/validator')
3
- let reply = require('../../class/reply').create;
4
-
5
- let name = 'verify';
6
- let verify = new Router();
7
-
8
- let service = require('./service').main;
9
-
10
- verify.post('/token', async (ctx) => {
11
- let body = ctx.request.body;
12
-
13
- // validate result
14
- let bodyValidate = validateObject(body, 'token');
15
-
16
- // fields validation
17
- if (!bodyValidate.isValid) {
18
- ctx.status = 412;
19
- ctx.body = reply('e', {
20
- 'e': bodyValidate.requires
21
- });
22
- return;
23
- }
24
-
25
- await service.verify(body.token)
26
- .then((payload) => ctx.body = reply('s', {
27
- 'user': payload
28
- }))
29
- .catch(err => {
30
- ctx.status = 412;
31
- ctx.body = reply('e', {
32
- 'e': err
33
- });
34
- });
35
- });
36
-
37
- verify.post('/checkAccess', async (ctx) => {
38
- let body = ctx.request.body;
39
-
40
- // validate result
41
- let bodyValidate = validateObject(body, 'token permissionField');
42
-
43
- // fields validation
44
- if (!bodyValidate.isValid) {
45
- ctx.status = 412;
46
- ctx.body = reply('e', {
47
- 'e': bodyValidate.requires
48
- });
49
- return;
50
- }
51
-
52
- let payload = await service.verify(body.token)
53
- .catch(err => {
54
- console.log(err);
55
- ctx.throw(412, err.message);
56
- });
57
-
58
-
59
- let userid = payload.id;
60
-
61
- await global.services.userManager.main.getUserById(userid)
62
- .then((user) => {
63
- let key = user.hasPermission(body.permissionField);
64
- ctx.body = reply('s', {
65
- 'access': key
66
- });
67
- })
68
- .catch(err => {
69
- ctx.status = 412;
70
- ctx.body = reply('e', {
71
- 'e': err
72
- });
73
- });
74
- });
75
-
76
- verify.get('/ready', async (ctx) => {
77
- // it's health check, so return success
78
- ctx.body = reply('s', {});
79
- });
80
-
81
- module.exports.name = name;
82
- module.exports.main = verify;
@@ -1,37 +0,0 @@
1
- const jwt = require("jsonwebtoken");
2
-
3
- class JWT {
4
- setKies(privateKey, publicKey) {
5
- this.privateKey = privateKey;
6
- this.publicKey = publicKey;
7
- }
8
-
9
- sign(payload) {
10
- return new Promise((done, reject) => {
11
- let option = { algorithm: "RS256" };
12
-
13
- try {
14
- let token = jwt.sign(payload, this.privateKey, option);
15
- done(token);
16
- } catch (error) {
17
- reject(error.message);
18
- }
19
- });
20
- }
21
-
22
- verify(token) {
23
- return new Promise((done, reject) => {
24
- let option = { algorithm: "RS256" };
25
-
26
- try {
27
- let decoded = jwt.verify(token, this.publicKey, option);
28
- done(decoded);
29
- } catch (error) {
30
- reject(error);
31
- }
32
- });
33
- }
34
- }
35
-
36
- module.exports.name = "jwt";
37
- module.exports.main = new JWT();
@@ -1,83 +0,0 @@
1
- var mongoose = require("mongoose");
2
- var Schema = mongoose.Schema;
3
-
4
- let CollectionDefinition = require("../../class/collection_definition");
5
- let { Permission, PermissionTypes } = require("../../class/security");
6
- const { config } = require("../../config");
7
- const triggerOperator = require("./../../class/trigger_operator");
8
-
9
- let authSchema = new Schema(
10
- {
11
- permissionGroup: String,
12
- email: String,
13
- phone: String,
14
- password: String,
15
- type: { type: String, default: "user", enum: ["user", "anonymous"] },
16
- },
17
- { timestamps: true }
18
- );
19
-
20
- authSchema.index({ email: 1 }, { unique: true });
21
- authSchema.pre(["save", "updateOne"], function (next) {
22
- // Encode the password before saving
23
- if (this.isModified("password")) {
24
- this.password = Buffer.from(this.password).toString("base64");
25
- }
26
- next();
27
- });
28
-
29
- authSchema.post("save", function (doc, next) {
30
- triggerOperator.call("insert-one", "cms", "auth", {
31
- query: null,
32
- queryResult: doc._doc,
33
- });
34
- next();
35
- });
36
-
37
- authSchema.post("findOneAndUpdate", function (doc, next) {
38
- triggerOperator.call("update-one", "cms", "auth", {
39
- query: null,
40
- queryResult: doc._doc,
41
- });
42
- next();
43
- });
44
-
45
- authSchema.post("updateOne", function (result, next) {
46
- triggerOperator.call("update-one", "cms", "auth", {
47
- query: null,
48
- queryResult: doc._doc,
49
- });
50
- next();
51
- });
52
-
53
- authSchema.post("findOneAndDelete", function (doc, next) {
54
- triggerOperator.call("remove-one", "cms", "auth", {
55
- query: null,
56
- queryResult: doc._doc,
57
- });
58
- next();
59
- });
60
-
61
- authSchema.post("deleteOne", function (result, next) {
62
- triggerOperator.call("remove-one", "cms", "auth", {
63
- query: null,
64
- queryResult: doc._doc,
65
- });
66
- next();
67
- });
68
-
69
- module.exports = [
70
- new CollectionDefinition({
71
- db: "cms",
72
- collection: "auth",
73
- schema: authSchema,
74
- permissions: [
75
- new Permission({
76
- type: PermissionTypes.advanced_settings,
77
- read: true,
78
- write: true,
79
- }),
80
- ],
81
- triggers: config.authTriggers || [],
82
- }),
83
- ];
@@ -1,43 +0,0 @@
1
- const { config } = require("../../config");
2
-
3
- function getDefaultPermissionGroups() {
4
- const defaultPermissionGroups = config.permissionGroups.find(
5
- (group) => group.isDefault
6
- );
7
-
8
- if (defaultPermissionGroups == null) {
9
- throw new Error("Default permission group not found");
10
- }
11
-
12
- return defaultPermissionGroups;
13
- }
14
-
15
- function getDefaultAnonymousPermissionGroup() {
16
- const anonymousPermission = config.permissionGroups.find(
17
- (group) => group.isAnonymous
18
- );
19
-
20
- if (anonymousPermission == null) {
21
- throw new Error("Anonymous permission group not found");
22
- }
23
-
24
- return anonymousPermission;
25
- }
26
-
27
- function getDefaultAdministratorPermissionGroup() {
28
- const administratorPermission = config.permissionGroups.find(
29
- (group) => group.title.toString() == "administrator"
30
- );
31
-
32
- if (administratorPermission == null) {
33
- throw new Error("Administrator permission group not found");
34
- }
35
-
36
- return administratorPermission;
37
- }
38
-
39
- module.exports = {
40
- getDefaultPermissionGroups,
41
- getDefaultAnonymousPermissionGroup,
42
- getDefaultAdministratorPermissionGroup,
43
- };
@@ -1,176 +0,0 @@
1
- let Router = require('koa-router');
2
- let validateObject = require('../../class/validator')
3
- let reply = require('../../class/reply').create;
4
-
5
- let name = 'user';
6
- let userManager = new Router();
7
-
8
- let service = require('./service').main;
9
-
10
- userManager.post('/register_id', async (ctx) => {
11
- let body = ctx.request.body;
12
-
13
- let validateOption = {
14
- id: '',
15
- idType: 'phone email'
16
- };
17
-
18
- // validate result
19
- let bodyValidate = validateObject(body, validateOption);
20
-
21
- // fields validation
22
- if (!bodyValidate.isValid) {
23
- ctx.status = 412;
24
- ctx.body = reply('e', { 'e': bodyValidate.requires });
25
- return;
26
- }
27
-
28
- let serial = service.generateVerificationCode(body.id, body.idType);
29
-
30
- if (serial) {
31
- service.registerTemporaryID(body.id, body.idType, serial);
32
- ctx.body = reply('s');
33
- }
34
- else {
35
- ctx.status = 412;
36
- ctx.body = reply('e', { 'e': 'Could not generate verification code.' });
37
- }
38
-
39
- });
40
-
41
- userManager.post('/validateCode', async (ctx) => {
42
- let body = ctx.request.body;
43
-
44
- // validate result
45
- let bodyValidate = validateObject(body, 'id code');
46
-
47
- // fields validation
48
- if (!bodyValidate.isValid) {
49
- ctx.status = 412;
50
- ctx.body = reply('e', { 'e': bodyValidate.requires });
51
- return;
52
- }
53
-
54
- let isValid = service.isCodeValid(body.id, body.code);
55
-
56
- if (!isValid) {
57
- ctx.status = 412;
58
- ctx.body = reply('e', {
59
- 'e': 'Verification code is wrong',
60
- 'isValid': isValid
61
- });
62
- return;
63
- }
64
-
65
- ctx.body = reply('s', { 'isValid': isValid });
66
- });
67
-
68
- userManager.post('/submit_password', async (ctx) => {
69
- let body = ctx.request.body;
70
-
71
- // validate result
72
- let bodyValidate = validateObject(body, 'id password code');
73
-
74
- // fields validation
75
- if (!bodyValidate.isValid) {
76
- ctx.status = 412;
77
- ctx.body = reply('e', { 'e': bodyValidate.requires });
78
- return;
79
- }
80
-
81
- let registerResult = await service
82
- .submitPasswordForTemporaryID(body.id, body.password, body.code).then();
83
-
84
- if (registerResult == true) ctx.body = reply('s');
85
- else {
86
- ctx.status = 412;
87
- ctx.body = reply('f');
88
- }
89
- });
90
-
91
- userManager.post('/change_password', async (ctx) => {
92
- let body = ctx.request.body;
93
-
94
- // validate result
95
- let bodyValidate = validateObject(body, 'id password code');
96
-
97
- // fields validation
98
- if (!bodyValidate.isValid) {
99
- ctx.status = 412;
100
- ctx.body = reply('e', { 'e': bodyValidate.requires });
101
- return;
102
- }
103
-
104
- let registerResult = await service
105
- .changePasswordForTemporaryID(body.id, body.password, body.code).then();
106
-
107
- if (registerResult == true) ctx.body = reply('s');
108
- else {
109
- ctx.status = 412;
110
- ctx.body = reply('f');
111
- }
112
- });
113
-
114
- userManager.post('/login', async (ctx) => {
115
- let body = ctx.request.body;
116
-
117
- let validateOption = {
118
- id: '',
119
- password: '',
120
- idType: 'phone email'
121
- }
122
-
123
- // validate result
124
- let bodyValidate = validateObject(body, validateOption);
125
-
126
- // fields validation
127
- if (!bodyValidate.isValid) {
128
- ctx.status = 412;
129
- ctx.body = reply('e', { 'e': bodyValidate.requires });
130
- return;
131
- }
132
-
133
- await service.loginUser(body.id, body.idType, body.password)
134
- .then((token) => ctx.body = reply('s', { 'token': token }))
135
- .catch(err => {
136
- ctx.status = 412;
137
- ctx.body = reply('e', { 'e': err });
138
- });
139
- });
140
-
141
- userManager.get('/loginAnonymous', async (ctx) => {
142
- await service.loginAnonymous()
143
- .then((token) => ctx.body = reply('s', { 'token': token }))
144
- .catch(err => {
145
- ctx.status = 412;
146
- ctx.body = reply('e', { 'e': err });
147
- });
148
- });
149
-
150
- userManager.post('/getPermission', async (ctx) => {
151
- let body = ctx.request.body;
152
-
153
- // validate result
154
- let bodyValidate = validateObject(body, 'id');
155
-
156
- // fields validation
157
- if (!bodyValidate.isValid) {
158
- ctx.status = 412;
159
- ctx.body = reply('e', { 'e': bodyValidate.requires });
160
- return;
161
- }
162
-
163
- let query = { _id: body.id };
164
-
165
- let permission = await global.services.dataProvider.getCollection('cms', 'permission')
166
- .findOne(query)
167
- .catch(err => {
168
- ctx.status = 412;
169
- ctx.body = reply('e', { 'e': err });
170
- });
171
-
172
- ctx.body = reply('s', { 'permission': permission });
173
- });
174
-
175
- module.exports.name = name;
176
- module.exports.main = userManager;