@ruiapp/rapid-core 0.3.0 → 0.3.2

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 (101) hide show
  1. package/CHANGELOG.md +7 -7
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.js +359 -134
  4. package/dist/plugins/license/LicensePlugin.d.ts +23 -0
  5. package/dist/plugins/license/LicensePluginTypes.d.ts +78 -0
  6. package/dist/plugins/license/LicenseService.d.ts +22 -0
  7. package/dist/plugins/license/actionHandlers/getLicense.d.ts +6 -0
  8. package/dist/plugins/license/actionHandlers/index.d.ts +3 -0
  9. package/dist/plugins/license/helpers/certHelper.d.ts +2 -0
  10. package/dist/plugins/license/helpers/cryptoHelper.d.ts +8 -0
  11. package/dist/plugins/license/models/index.d.ts +2 -0
  12. package/dist/plugins/license/routes/getLicense.d.ts +12 -0
  13. package/dist/plugins/license/routes/index.d.ts +12 -0
  14. package/dist/utilities/typeUtility.d.ts +1 -0
  15. package/package.json +1 -1
  16. package/src/bootstrapApplicationConfig.ts +631 -631
  17. package/src/core/response.ts +76 -76
  18. package/src/core/routeContext.ts +47 -47
  19. package/src/core/server.ts +142 -142
  20. package/src/dataAccess/columnTypeMapper.ts +22 -22
  21. package/src/dataAccess/dataAccessTypes.ts +163 -163
  22. package/src/dataAccess/dataAccessor.ts +135 -135
  23. package/src/dataAccess/entityManager.ts +1718 -1718
  24. package/src/dataAccess/entityMapper.ts +100 -100
  25. package/src/dataAccess/propertyMapper.ts +28 -28
  26. package/src/deno-std/http/cookie.ts +372 -372
  27. package/src/helpers/entityHelpers.ts +76 -76
  28. package/src/helpers/filterHelper.ts +148 -148
  29. package/src/helpers/metaHelper.ts +89 -89
  30. package/src/helpers/runCollectionEntityActionHandler.ts +27 -27
  31. package/src/index.ts +57 -54
  32. package/src/plugins/auth/AuthPlugin.ts +85 -85
  33. package/src/plugins/auth/actionHandlers/changePassword.ts +54 -54
  34. package/src/plugins/auth/actionHandlers/createSession.ts +75 -63
  35. package/src/plugins/auth/actionHandlers/resetPassword.ts +38 -38
  36. package/src/plugins/cronJob/CronJobPlugin.ts +112 -112
  37. package/src/plugins/dataManage/DataManagePlugin.ts +163 -163
  38. package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +20 -20
  39. package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +16 -16
  40. package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +42 -42
  41. package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +24 -24
  42. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +38 -38
  43. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +22 -22
  44. package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +26 -26
  45. package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +21 -21
  46. package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +20 -20
  47. package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +41 -41
  48. package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
  49. package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
  50. package/src/plugins/fileManage/actionHandlers/downloadDocument.ts +65 -65
  51. package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
  52. package/src/plugins/license/LicensePlugin.ts +79 -0
  53. package/src/plugins/license/LicensePluginTypes.ts +95 -0
  54. package/src/plugins/license/LicenseService.ts +112 -0
  55. package/src/plugins/license/actionHandlers/getLicense.ts +18 -0
  56. package/src/plugins/license/actionHandlers/index.ts +4 -0
  57. package/src/plugins/license/helpers/certHelper.ts +21 -0
  58. package/src/plugins/license/helpers/cryptoHelper.ts +47 -0
  59. package/src/plugins/license/models/index.ts +1 -0
  60. package/src/plugins/license/routes/getLicense.ts +15 -0
  61. package/src/plugins/license/routes/index.ts +3 -0
  62. package/src/plugins/mail/MailPlugin.ts +74 -74
  63. package/src/plugins/mail/MailPluginTypes.ts +27 -27
  64. package/src/plugins/mail/MailService.ts +38 -38
  65. package/src/plugins/mail/actionHandlers/index.ts +3 -3
  66. package/src/plugins/mail/models/index.ts +1 -1
  67. package/src/plugins/mail/routes/index.ts +1 -1
  68. package/src/plugins/metaManage/MetaManagePlugin.ts +504 -504
  69. package/src/plugins/notification/NotificationPlugin.ts +68 -68
  70. package/src/plugins/notification/NotificationPluginTypes.ts +13 -13
  71. package/src/plugins/notification/NotificationService.ts +25 -25
  72. package/src/plugins/notification/actionHandlers/index.ts +3 -3
  73. package/src/plugins/notification/models/Notification.ts +60 -57
  74. package/src/plugins/notification/models/index.ts +3 -3
  75. package/src/plugins/notification/routes/index.ts +1 -1
  76. package/src/plugins/routeManage/RouteManagePlugin.ts +62 -62
  77. package/src/plugins/sequence/SequencePlugin.ts +136 -136
  78. package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
  79. package/src/plugins/sequence/SequenceService.ts +81 -81
  80. package/src/plugins/sequence/actionHandlers/generateSn.ts +32 -32
  81. package/src/plugins/sequence/segments/autoIncrement.ts +78 -78
  82. package/src/plugins/sequence/segments/dayOfMonth.ts +17 -17
  83. package/src/plugins/sequence/segments/literal.ts +14 -14
  84. package/src/plugins/sequence/segments/month.ts +17 -17
  85. package/src/plugins/sequence/segments/parameter.ts +18 -18
  86. package/src/plugins/sequence/segments/year.ts +17 -17
  87. package/src/plugins/setting/SettingPlugin.ts +68 -68
  88. package/src/plugins/setting/SettingPluginTypes.ts +37 -37
  89. package/src/plugins/setting/models/SystemSettingItem.ts +48 -42
  90. package/src/plugins/setting/models/UserSettingItem.ts +55 -49
  91. package/src/plugins/stateMachine/StateMachinePlugin.ts +186 -186
  92. package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
  93. package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +51 -51
  94. package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
  95. package/src/plugins/webhooks/pluginConfig.ts +75 -75
  96. package/src/queryBuilder/queryBuilder.ts +665 -665
  97. package/src/server.ts +463 -463
  98. package/src/types.ts +701 -701
  99. package/src/utilities/errorUtility.ts +15 -15
  100. package/src/utilities/pathUtility.ts +14 -14
  101. package/src/utilities/typeUtility.ts +15 -11
@@ -1,27 +1,27 @@
1
- import { RunEntityActionHandlerOptions } from "~/types";
2
- import { mergeInput } from "./inputHelper";
3
- import { ActionHandlerContext } from "~/core/actionHandler";
4
- import EntityManager from "~/dataAccess/entityManager";
5
-
6
- type EntityActionHandler = (entityManager: EntityManager, input: any) => Promise<any>;
7
-
8
- export default async function runCollectionEntityActionHandler(
9
- ctx: ActionHandlerContext,
10
- options: RunEntityActionHandlerOptions,
11
- code: string,
12
- handleEntityAction: EntityActionHandler,
13
- ) {
14
- const { logger, server, input } = ctx;
15
-
16
- const { defaultInput, fixedInput } = options;
17
- const mergedInput = mergeInput(defaultInput, input, fixedInput);
18
- logger.debug(`Running ${code} handler...`, { defaultInput, fixedInput, mergedInput });
19
-
20
- const entityManager = server.getEntityManager(options.singularCode);
21
- const result = handleEntityAction(entityManager, mergedInput);
22
- if (result instanceof Promise) {
23
- ctx.output = await result;
24
- } else {
25
- ctx.output = result;
26
- }
27
- }
1
+ import { RunEntityActionHandlerOptions } from "~/types";
2
+ import { mergeInput } from "./inputHelper";
3
+ import { ActionHandlerContext } from "~/core/actionHandler";
4
+ import EntityManager from "~/dataAccess/entityManager";
5
+
6
+ type EntityActionHandler = (entityManager: EntityManager, input: any) => Promise<any>;
7
+
8
+ export default async function runCollectionEntityActionHandler(
9
+ ctx: ActionHandlerContext,
10
+ options: RunEntityActionHandlerOptions,
11
+ code: string,
12
+ handleEntityAction: EntityActionHandler,
13
+ ) {
14
+ const { logger, server, input } = ctx;
15
+
16
+ const { defaultInput, fixedInput } = options;
17
+ const mergedInput = mergeInput(defaultInput, input, fixedInput);
18
+ logger.debug(`Running ${code} handler...`, { defaultInput, fixedInput, mergedInput });
19
+
20
+ const entityManager = server.getEntityManager(options.singularCode);
21
+ const result = handleEntityAction(entityManager, mergedInput);
22
+ if (result instanceof Promise) {
23
+ ctx.output = await result;
24
+ } else {
25
+ ctx.output = result;
26
+ }
27
+ }
package/src/index.ts CHANGED
@@ -1,54 +1,57 @@
1
- import { fixBigIntJSONSerialize } from "./polyfill";
2
- fixBigIntJSONSerialize();
3
-
4
- export * from "./types";
5
- export * from "./server";
6
-
7
- export * from "./core/request";
8
- export * from "./core/routeContext";
9
- export * from "./core/server";
10
- export * from "./core/http-types";
11
- export * from "./core/actionHandler";
12
-
13
- export * from "./utilities/jwtUtility";
14
-
15
- export { mapDbRowToEntity } from "./dataAccess/entityMapper";
16
-
17
- export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
18
-
19
- export { default as MetaManagePlugin } from "./plugins/metaManage/MetaManagePlugin";
20
-
21
- export { default as DataManagePlugin } from "./plugins/dataManage/DataManagePlugin";
22
-
23
- export { default as RouteManagePlugin } from "./plugins/routeManage/RouteManagePlugin";
24
-
25
- export { default as SequencePlugin } from "./plugins/sequence/SequencePlugin";
26
- export * from "./plugins/sequence/SequencePluginTypes";
27
-
28
- export { default as WebhooksPlugin } from "./plugins/webhooks/WebhooksPlugin";
29
-
30
- export { default as AuthPlugin } from "./plugins/auth/AuthPlugin";
31
-
32
- export { default as FileManagePlugin } from "./plugins/fileManage/FileManagePlugin";
33
-
34
- export { default as MailPlugin } from "./plugins/mail/MailPlugin";
35
- export * from "./plugins/mail/MailPluginTypes";
36
-
37
- export { default as NotificationPlugin } from "./plugins/notification/NotificationPlugin";
38
- export * from "./plugins/notification/NotificationPluginTypes";
39
-
40
- export { default as ServerOperationPlugin } from "./plugins/serverOperation/ServerOperationPlugin";
41
- export * from "./plugins/serverOperation/ServerOperationPluginTypes";
42
-
43
- export { default as SettingPlugin } from "./plugins/setting/SettingPlugin";
44
- export * from "./plugins/setting/SettingPluginTypes";
45
-
46
- export { default as CronJobPlugin } from "./plugins/cronJob/CronJobPlugin";
47
- export * from "./plugins/cronJob/CronJobPluginTypes";
48
-
49
- export { default as StateMachinePlugin } from "./plugins/stateMachine/StateMachinePlugin";
50
- export * from "./plugins/stateMachine/StateMachinePluginTypes";
51
-
52
- export { default as EntityAccessControlPlugin } from "./plugins/entityAccessControl/EntityAccessControlPlugin";
53
-
54
- export * from "./facilities/log/LogFacility";
1
+ import { fixBigIntJSONSerialize } from "./polyfill";
2
+ fixBigIntJSONSerialize();
3
+
4
+ export * from "./types";
5
+ export * from "./server";
6
+
7
+ export * from "./core/request";
8
+ export * from "./core/routeContext";
9
+ export * from "./core/server";
10
+ export * from "./core/http-types";
11
+ export * from "./core/actionHandler";
12
+
13
+ export * from "./utilities/jwtUtility";
14
+
15
+ export { mapDbRowToEntity } from "./dataAccess/entityMapper";
16
+
17
+ export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
18
+
19
+ export { default as MetaManagePlugin } from "./plugins/metaManage/MetaManagePlugin";
20
+
21
+ export { default as DataManagePlugin } from "./plugins/dataManage/DataManagePlugin";
22
+
23
+ export { default as RouteManagePlugin } from "./plugins/routeManage/RouteManagePlugin";
24
+
25
+ export { default as SequencePlugin } from "./plugins/sequence/SequencePlugin";
26
+ export * from "./plugins/sequence/SequencePluginTypes";
27
+
28
+ export { default as WebhooksPlugin } from "./plugins/webhooks/WebhooksPlugin";
29
+
30
+ export { default as AuthPlugin } from "./plugins/auth/AuthPlugin";
31
+
32
+ export { default as FileManagePlugin } from "./plugins/fileManage/FileManagePlugin";
33
+
34
+ export { default as LicensePlugin } from "./plugins/license/LicensePlugin";
35
+ export * from "./plugins/license/LicensePluginTypes";
36
+
37
+ export { default as MailPlugin } from "./plugins/mail/MailPlugin";
38
+ export * from "./plugins/mail/MailPluginTypes";
39
+
40
+ export { default as NotificationPlugin } from "./plugins/notification/NotificationPlugin";
41
+ export * from "./plugins/notification/NotificationPluginTypes";
42
+
43
+ export { default as ServerOperationPlugin } from "./plugins/serverOperation/ServerOperationPlugin";
44
+ export * from "./plugins/serverOperation/ServerOperationPluginTypes";
45
+
46
+ export { default as SettingPlugin } from "./plugins/setting/SettingPlugin";
47
+ export * from "./plugins/setting/SettingPluginTypes";
48
+
49
+ export { default as CronJobPlugin } from "./plugins/cronJob/CronJobPlugin";
50
+ export * from "./plugins/cronJob/CronJobPluginTypes";
51
+
52
+ export { default as StateMachinePlugin } from "./plugins/stateMachine/StateMachinePlugin";
53
+ export * from "./plugins/stateMachine/StateMachinePluginTypes";
54
+
55
+ export { default as EntityAccessControlPlugin } from "./plugins/entityAccessControl/EntityAccessControlPlugin";
56
+
57
+ export * from "./facilities/log/LogFacility";
@@ -1,85 +1,85 @@
1
- /**
2
- * Auth manager plugin
3
- */
4
-
5
- import { RpdApplicationConfig } from "~/types";
6
- import {
7
- IRpdServer,
8
- RapidPlugin,
9
- RpdConfigurationItemOptions,
10
- RpdServerPluginConfigurableTargetOptions,
11
- RpdServerPluginExtendingAbilities,
12
- } from "~/core/server";
13
-
14
- import pluginActionHandlers from "./actionHandlers";
15
- import pluginModels from "./models";
16
- import pluginRoutes from "./routes";
17
- import { RouteContext } from "~/core/routeContext";
18
- import { verifyJwt } from "~/utilities/jwtUtility";
19
-
20
- class AuthPlugin implements RapidPlugin {
21
- get code(): string {
22
- return "authManager";
23
- }
24
-
25
- get description(): string {
26
- return null;
27
- }
28
-
29
- get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
30
- return [];
31
- }
32
-
33
- get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
34
- return [];
35
- }
36
-
37
- get configurations(): RpdConfigurationItemOptions[] {
38
- return [];
39
- }
40
-
41
- async registerActionHandlers(server: IRpdServer): Promise<any> {
42
- for (const actionHandler of pluginActionHandlers) {
43
- server.registerActionHandler(this, actionHandler);
44
- }
45
- }
46
-
47
- async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
48
- server.appendApplicationConfig({ models: pluginModels });
49
- }
50
-
51
- async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
52
- server.appendApplicationConfig({ routes: pluginRoutes });
53
- }
54
-
55
- async onPrepareRouteContext(server: IRpdServer, routeContext: RouteContext) {
56
- const request = routeContext.request;
57
- let token: string;
58
-
59
- const headers = request.headers;
60
- // No Authorization header
61
- if (headers.has("Authorization")) {
62
- // Authorization header has no Bearer or no token
63
- const authHeader = headers.get("Authorization")!;
64
- if (!authHeader.startsWith("Bearer ") || authHeader.length <= 7) {
65
- throw new Error("AUTHORIZATION_HEADER_INVALID");
66
- }
67
-
68
- token = authHeader.slice(7);
69
- } else {
70
- token = request.cookies[server.config.sessionCookieName];
71
- }
72
-
73
- try {
74
- const secretKey = Buffer.from(server.config.jwtKey, "base64");
75
- const tokenPayload = verifyJwt(token, secretKey);
76
- routeContext.state.userId = tokenPayload.aud as string;
77
- routeContext.state.userLogin = tokenPayload.act as string;
78
- } catch (error) {
79
- const logger = server.getLogger();
80
- logger.debug("Verify JWT failed.", { error });
81
- }
82
- }
83
- }
84
-
85
- export default AuthPlugin;
1
+ /**
2
+ * Auth manager plugin
3
+ */
4
+
5
+ import { RpdApplicationConfig } from "~/types";
6
+ import {
7
+ IRpdServer,
8
+ RapidPlugin,
9
+ RpdConfigurationItemOptions,
10
+ RpdServerPluginConfigurableTargetOptions,
11
+ RpdServerPluginExtendingAbilities,
12
+ } from "~/core/server";
13
+
14
+ import pluginActionHandlers from "./actionHandlers";
15
+ import pluginModels from "./models";
16
+ import pluginRoutes from "./routes";
17
+ import { RouteContext } from "~/core/routeContext";
18
+ import { verifyJwt } from "~/utilities/jwtUtility";
19
+
20
+ class AuthPlugin implements RapidPlugin {
21
+ get code(): string {
22
+ return "authManager";
23
+ }
24
+
25
+ get description(): string {
26
+ return null;
27
+ }
28
+
29
+ get extendingAbilities(): RpdServerPluginExtendingAbilities[] {
30
+ return [];
31
+ }
32
+
33
+ get configurableTargets(): RpdServerPluginConfigurableTargetOptions[] {
34
+ return [];
35
+ }
36
+
37
+ get configurations(): RpdConfigurationItemOptions[] {
38
+ return [];
39
+ }
40
+
41
+ async registerActionHandlers(server: IRpdServer): Promise<any> {
42
+ for (const actionHandler of pluginActionHandlers) {
43
+ server.registerActionHandler(this, actionHandler);
44
+ }
45
+ }
46
+
47
+ async configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
48
+ server.appendApplicationConfig({ models: pluginModels });
49
+ }
50
+
51
+ async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
52
+ server.appendApplicationConfig({ routes: pluginRoutes });
53
+ }
54
+
55
+ async onPrepareRouteContext(server: IRpdServer, routeContext: RouteContext) {
56
+ const request = routeContext.request;
57
+ let token: string;
58
+
59
+ const headers = request.headers;
60
+ // No Authorization header
61
+ if (headers.has("Authorization")) {
62
+ // Authorization header has no Bearer or no token
63
+ const authHeader = headers.get("Authorization")!;
64
+ if (!authHeader.startsWith("Bearer ") || authHeader.length <= 7) {
65
+ throw new Error("AUTHORIZATION_HEADER_INVALID");
66
+ }
67
+
68
+ token = authHeader.slice(7);
69
+ } else {
70
+ token = request.cookies[server.config.sessionCookieName];
71
+ }
72
+
73
+ try {
74
+ const secretKey = Buffer.from(server.config.jwtKey, "base64");
75
+ const tokenPayload = verifyJwt(token, secretKey);
76
+ routeContext.state.userId = tokenPayload.aud as string;
77
+ routeContext.state.userLogin = tokenPayload.act as string;
78
+ } catch (error) {
79
+ const logger = server.getLogger();
80
+ logger.debug("Verify JWT failed.", { error });
81
+ }
82
+ }
83
+ }
84
+
85
+ export default AuthPlugin;
@@ -1,54 +1,54 @@
1
- import bcrypt from "bcrypt";
2
- import { ActionHandlerContext } from "~/core/actionHandler";
3
- import { RapidPlugin } from "~/core/server";
4
-
5
- export const code = "changePassword";
6
-
7
- export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
8
- const { server, input, routerContext } = ctx;
9
- const { response } = routerContext;
10
- const { id, oldPassword, newPassword } = input;
11
-
12
- const userId = routerContext.state.userId;
13
- if (!userId) {
14
- ctx.status = 401;
15
- ctx.output = {
16
- error: {
17
- message: "You are not signed in.",
18
- },
19
- };
20
- return;
21
- }
22
-
23
- const userDataAccessor = server.getDataAccessor({
24
- singularCode: "oc_user",
25
- });
26
-
27
- const user = await userDataAccessor.findOne({
28
- filters: [
29
- {
30
- operator: "eq",
31
- field: "id",
32
- value: userId,
33
- },
34
- ],
35
- });
36
-
37
- if (!user) {
38
- throw new Error("User not found.");
39
- }
40
-
41
- const isMatch = await bcrypt.compare(oldPassword, user.password);
42
- if (!isMatch) {
43
- throw new Error("旧密码错误。");
44
- }
45
-
46
- const saltRounds = 10;
47
- const passwordHash = await bcrypt.hash(newPassword, saltRounds);
48
-
49
- await userDataAccessor.updateById(user.id, {
50
- password: passwordHash,
51
- });
52
-
53
- ctx.output = {};
54
- }
1
+ import bcrypt from "bcrypt";
2
+ import { ActionHandlerContext } from "~/core/actionHandler";
3
+ import { RapidPlugin } from "~/core/server";
4
+
5
+ export const code = "changePassword";
6
+
7
+ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
8
+ const { server, input, routerContext } = ctx;
9
+ const { response } = routerContext;
10
+ const { id, oldPassword, newPassword } = input;
11
+
12
+ const userId = routerContext.state.userId;
13
+ if (!userId) {
14
+ ctx.status = 401;
15
+ ctx.output = {
16
+ error: {
17
+ message: "You are not signed in.",
18
+ },
19
+ };
20
+ return;
21
+ }
22
+
23
+ const userDataAccessor = server.getDataAccessor({
24
+ singularCode: "oc_user",
25
+ });
26
+
27
+ const user = await userDataAccessor.findOne({
28
+ filters: [
29
+ {
30
+ operator: "eq",
31
+ field: "id",
32
+ value: userId,
33
+ },
34
+ ],
35
+ });
36
+
37
+ if (!user) {
38
+ throw new Error("User not found.");
39
+ }
40
+
41
+ const isMatch = await bcrypt.compare(oldPassword, user.password);
42
+ if (!isMatch) {
43
+ throw new Error("旧密码错误。");
44
+ }
45
+
46
+ const saltRounds = 10;
47
+ const passwordHash = await bcrypt.hash(newPassword, saltRounds);
48
+
49
+ await userDataAccessor.updateById(user.id, {
50
+ password: passwordHash,
51
+ });
52
+
53
+ ctx.output = {};
54
+ }
@@ -1,63 +1,75 @@
1
- import bcrypt from "bcrypt";
2
- import { setCookie } from "~/deno-std/http/cookie";
3
- import { createJwt } from "~/utilities/jwtUtility";
4
- import { ActionHandlerContext } from "~/core/actionHandler";
5
- import { RapidPlugin } from "~/core/server";
6
-
7
- export interface UserAccessToken {
8
- sub: "userAccessToken";
9
- aud: string;
10
- }
11
-
12
- export const code = "createSession";
13
-
14
- export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
15
- const { server, input, routerContext } = ctx;
16
- const { response } = routerContext;
17
- const { account, password } = input;
18
-
19
- const userDataAccessor = server.getDataAccessor({
20
- singularCode: "oc_user",
21
- });
22
-
23
- const user = await userDataAccessor.findOne({
24
- filters: [
25
- {
26
- operator: "eq",
27
- field: "login",
28
- value: account,
29
- },
30
- ],
31
- });
32
-
33
- if (!user) {
34
- throw new Error("用户名或密码错误。");
35
- }
36
-
37
- const isMatch = await bcrypt.compare(password, user.password);
38
- if (!isMatch) {
39
- throw new Error("用户名或密码错误。");
40
- }
41
-
42
- const secretKey = Buffer.from(server.config.jwtKey, "base64");
43
- const token = createJwt(
44
- {
45
- iss: "authManager",
46
- sub: "userAccessToken",
47
- aud: "" + user.id,
48
- iat: Math.floor(Date.now() / 1000),
49
- act: user.login,
50
- } as UserAccessToken,
51
- secretKey,
52
- );
53
-
54
- setCookie(response.headers, {
55
- name: ctx.server.config.sessionCookieName,
56
- value: token,
57
- path: "/",
58
- });
59
-
60
- ctx.output = {
61
- token,
62
- };
63
- }
1
+ import bcrypt from "bcrypt";
2
+ import { setCookie } from "~/deno-std/http/cookie";
3
+ import { createJwt } from "~/utilities/jwtUtility";
4
+ import { ActionHandlerContext } from "~/core/actionHandler";
5
+ import { RapidPlugin } from "~/core/server";
6
+ import LicenseService from "~/plugins/license/LicenseService";
7
+ import { get } from "lodash";
8
+
9
+ export interface UserAccessToken {
10
+ sub: "userAccessToken";
11
+ aud: string;
12
+ }
13
+
14
+ export const code = "createSession";
15
+
16
+ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
17
+ const { server, input, routerContext } = ctx;
18
+ const { response } = routerContext;
19
+ const { account, password } = input;
20
+
21
+ const licenseService = server.getService<LicenseService>("licenseService");
22
+ const license = licenseService.getLicense();
23
+ if (!license) {
24
+ throw new Error(`登录失败,无法获取系统授权信息。`);
25
+ }
26
+ if (licenseService.isExpired()) {
27
+ const expireDate = get(license.authority, "expireDate");
28
+ throw new Error(`登录失败,系统授权已于${expireDate}过期。`);
29
+ }
30
+
31
+ const userDataAccessor = server.getDataAccessor({
32
+ singularCode: "oc_user",
33
+ });
34
+
35
+ const user = await userDataAccessor.findOne({
36
+ filters: [
37
+ {
38
+ operator: "eq",
39
+ field: "login",
40
+ value: account,
41
+ },
42
+ ],
43
+ });
44
+
45
+ if (!user) {
46
+ throw new Error("用户名或密码错误。");
47
+ }
48
+
49
+ const isMatch = await bcrypt.compare(password, user.password);
50
+ if (!isMatch) {
51
+ throw new Error("用户名或密码错误。");
52
+ }
53
+
54
+ const secretKey = Buffer.from(server.config.jwtKey, "base64");
55
+ const token = createJwt(
56
+ {
57
+ iss: "authManager",
58
+ sub: "userAccessToken",
59
+ aud: "" + user.id,
60
+ iat: Math.floor(Date.now() / 1000),
61
+ act: user.login,
62
+ } as UserAccessToken,
63
+ secretKey,
64
+ );
65
+
66
+ setCookie(response.headers, {
67
+ name: ctx.server.config.sessionCookieName,
68
+ value: token,
69
+ path: "/",
70
+ });
71
+
72
+ ctx.output = {
73
+ token,
74
+ };
75
+ }
@@ -1,38 +1,38 @@
1
- import bcrypt from "bcrypt";
2
- import { ActionHandlerContext } from "~/core/actionHandler";
3
- import { RapidPlugin } from "~/core/server";
4
-
5
- export const code = "resetPassword";
6
-
7
- export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
8
- const { server, input, routerContext } = ctx;
9
- const { response } = routerContext;
10
- const { userId, password } = input;
11
-
12
- const userDataAccessor = server.getDataAccessor({
13
- singularCode: "oc_user",
14
- });
15
-
16
- const user = await userDataAccessor.findOne({
17
- filters: [
18
- {
19
- operator: "eq",
20
- field: "id",
21
- value: userId,
22
- },
23
- ],
24
- });
25
-
26
- if (!user) {
27
- throw new Error("User not found.");
28
- }
29
-
30
- const saltRounds = 10;
31
- const passwordHash = await bcrypt.hash(password, saltRounds);
32
-
33
- await userDataAccessor.updateById(user.id, {
34
- password: passwordHash,
35
- });
36
-
37
- ctx.output = {};
38
- }
1
+ import bcrypt from "bcrypt";
2
+ import { ActionHandlerContext } from "~/core/actionHandler";
3
+ import { RapidPlugin } from "~/core/server";
4
+
5
+ export const code = "resetPassword";
6
+
7
+ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, options: any) {
8
+ const { server, input, routerContext } = ctx;
9
+ const { response } = routerContext;
10
+ const { userId, password } = input;
11
+
12
+ const userDataAccessor = server.getDataAccessor({
13
+ singularCode: "oc_user",
14
+ });
15
+
16
+ const user = await userDataAccessor.findOne({
17
+ filters: [
18
+ {
19
+ operator: "eq",
20
+ field: "id",
21
+ value: userId,
22
+ },
23
+ ],
24
+ });
25
+
26
+ if (!user) {
27
+ throw new Error("User not found.");
28
+ }
29
+
30
+ const saltRounds = 10;
31
+ const passwordHash = await bcrypt.hash(password, saltRounds);
32
+
33
+ await userDataAccessor.updateById(user.id, {
34
+ password: passwordHash,
35
+ });
36
+
37
+ ctx.output = {};
38
+ }