@ruiapp/rapid-core 0.5.11 → 0.5.13

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 (185) hide show
  1. package/CHANGELOG.md +7 -7
  2. package/dist/facilities/cache/CacheFacilityTypes.d.ts +4 -2
  3. package/dist/facilities/cache/MemoryCache.d.ts +3 -1
  4. package/dist/index.d.ts +4 -0
  5. package/dist/index.js +131 -72
  6. package/dist/utilities/entityUtility.d.ts +1 -0
  7. package/dist/utilities/passwordUtility.d.ts +14 -0
  8. package/package.json +2 -2
  9. package/rollup.config.js +16 -16
  10. package/src/bootstrapApplicationConfig.ts +638 -638
  11. package/src/core/actionHandler.ts +22 -22
  12. package/src/core/eventManager.ts +20 -20
  13. package/src/core/facility.ts +7 -7
  14. package/src/core/http/formDataParser.ts +89 -89
  15. package/src/core/http-types.ts +4 -4
  16. package/src/core/pluginManager.ts +175 -175
  17. package/src/core/providers/runtimeProvider.ts +5 -5
  18. package/src/core/request.ts +95 -95
  19. package/src/core/response.ts +79 -79
  20. package/src/core/routeContext.ts +100 -100
  21. package/src/core/routesBuilder.ts +88 -88
  22. package/src/core/server.ts +145 -145
  23. package/src/dataAccess/columnTypeMapper.ts +22 -22
  24. package/src/dataAccess/dataAccessTypes.ts +163 -163
  25. package/src/dataAccess/dataAccessor.ts +135 -135
  26. package/src/dataAccess/entityManager.ts +1910 -1910
  27. package/src/dataAccess/entityMapper.ts +100 -100
  28. package/src/dataAccess/propertyMapper.ts +28 -28
  29. package/src/deno-std/assert/assert.ts +9 -9
  30. package/src/deno-std/assert/assertion_error.ts +7 -7
  31. package/src/deno-std/datetime/to_imf.ts +32 -32
  32. package/src/deno-std/encoding/base64.ts +141 -141
  33. package/src/deno-std/http/cookie.ts +372 -372
  34. package/src/facilities/cache/CacheFacilityTypes.ts +29 -27
  35. package/src/facilities/cache/CacheFactory.ts +31 -31
  36. package/src/facilities/cache/MemoryCache.ts +58 -42
  37. package/src/facilities/cache/MemoryCacheProvider.ts +15 -15
  38. package/src/facilities/log/LogFacility.ts +35 -35
  39. package/src/helpers/entityHelpers.ts +76 -76
  40. package/src/helpers/filterHelper.ts +148 -148
  41. package/src/helpers/inputHelper.ts +11 -11
  42. package/src/helpers/metaHelper.ts +104 -104
  43. package/src/helpers/runCollectionEntityActionHandler.ts +57 -57
  44. package/src/index.ts +67 -63
  45. package/src/plugins/auth/AuthPlugin.ts +93 -93
  46. package/src/plugins/auth/actionHandlers/changePassword.ts +60 -61
  47. package/src/plugins/auth/actionHandlers/createSession.ts +68 -68
  48. package/src/plugins/auth/actionHandlers/deleteSession.ts +18 -18
  49. package/src/plugins/auth/actionHandlers/getMyProfile.ts +35 -35
  50. package/src/plugins/auth/actionHandlers/index.ts +8 -8
  51. package/src/plugins/auth/actionHandlers/resetPassword.ts +44 -45
  52. package/src/plugins/auth/models/AccessToken.ts +56 -56
  53. package/src/plugins/auth/models/index.ts +3 -3
  54. package/src/plugins/auth/routes/changePassword.ts +15 -15
  55. package/src/plugins/auth/routes/getMyProfile.ts +15 -15
  56. package/src/plugins/auth/routes/index.ts +7 -7
  57. package/src/plugins/auth/routes/resetPassword.ts +15 -15
  58. package/src/plugins/auth/routes/signin.ts +15 -15
  59. package/src/plugins/auth/routes/signout.ts +15 -15
  60. package/src/plugins/auth/services/AuthService.ts +39 -39
  61. package/src/plugins/cronJob/CronJobPlugin.ts +112 -112
  62. package/src/plugins/cronJob/CronJobPluginTypes.ts +49 -49
  63. package/src/plugins/cronJob/actionHandlers/index.ts +4 -4
  64. package/src/plugins/cronJob/actionHandlers/runCronJob.ts +29 -29
  65. package/src/plugins/cronJob/routes/index.ts +3 -3
  66. package/src/plugins/cronJob/routes/runCronJob.ts +15 -15
  67. package/src/plugins/dataManage/DataManagePlugin.ts +163 -163
  68. package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +15 -15
  69. package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +17 -17
  70. package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +81 -81
  71. package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +20 -20
  72. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +45 -45
  73. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +20 -20
  74. package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +27 -27
  75. package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +30 -30
  76. package/src/plugins/dataManage/actionHandlers/queryDatabase.ts +22 -22
  77. package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +15 -15
  78. package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +38 -38
  79. package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
  80. package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
  81. package/src/plugins/fileManage/actionHandlers/downloadDocument.ts +65 -65
  82. package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
  83. package/src/plugins/fileManage/actionHandlers/uploadFile.ts +33 -33
  84. package/src/plugins/fileManage/routes/downloadDocument.ts +15 -15
  85. package/src/plugins/fileManage/routes/downloadFile.ts +15 -15
  86. package/src/plugins/fileManage/routes/index.ts +5 -5
  87. package/src/plugins/fileManage/routes/uploadFile.ts +15 -15
  88. package/src/plugins/license/LicensePlugin.ts +79 -79
  89. package/src/plugins/license/LicensePluginTypes.ts +95 -95
  90. package/src/plugins/license/LicenseService.ts +118 -118
  91. package/src/plugins/license/actionHandlers/getLicense.ts +18 -18
  92. package/src/plugins/license/actionHandlers/index.ts +4 -4
  93. package/src/plugins/license/helpers/certHelper.ts +21 -21
  94. package/src/plugins/license/helpers/cryptoHelper.ts +47 -47
  95. package/src/plugins/license/models/index.ts +1 -1
  96. package/src/plugins/license/routes/getLicense.ts +15 -15
  97. package/src/plugins/license/routes/index.ts +3 -3
  98. package/src/plugins/mail/MailPlugin.ts +74 -74
  99. package/src/plugins/mail/MailPluginTypes.ts +27 -27
  100. package/src/plugins/mail/MailService.ts +38 -38
  101. package/src/plugins/mail/actionHandlers/index.ts +3 -3
  102. package/src/plugins/mail/models/index.ts +1 -1
  103. package/src/plugins/mail/routes/index.ts +1 -1
  104. package/src/plugins/metaManage/MetaManagePlugin.ts +530 -530
  105. package/src/plugins/metaManage/actionHandlers/getMetaModelDetail.ts +10 -10
  106. package/src/plugins/metaManage/actionHandlers/listMetaModels.ts +9 -9
  107. package/src/plugins/metaManage/actionHandlers/listMetaRoutes.ts +9 -9
  108. package/src/plugins/notification/NotificationPlugin.ts +68 -68
  109. package/src/plugins/notification/NotificationPluginTypes.ts +13 -13
  110. package/src/plugins/notification/NotificationService.ts +25 -25
  111. package/src/plugins/notification/actionHandlers/index.ts +3 -3
  112. package/src/plugins/notification/models/Notification.ts +60 -60
  113. package/src/plugins/notification/models/index.ts +3 -3
  114. package/src/plugins/notification/routes/index.ts +1 -1
  115. package/src/plugins/routeManage/RouteManagePlugin.ts +62 -62
  116. package/src/plugins/routeManage/actionHandlers/httpProxy.ts +13 -13
  117. package/src/plugins/sequence/SequencePlugin.ts +146 -146
  118. package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
  119. package/src/plugins/sequence/SequenceService.ts +92 -92
  120. package/src/plugins/sequence/actionHandlers/generateSn.ts +32 -32
  121. package/src/plugins/sequence/actionHandlers/index.ts +4 -4
  122. package/src/plugins/sequence/models/SequenceAutoIncrementRecord.ts +49 -49
  123. package/src/plugins/sequence/models/SequenceRule.ts +42 -42
  124. package/src/plugins/sequence/models/index.ts +4 -4
  125. package/src/plugins/sequence/routes/generateSn.ts +15 -15
  126. package/src/plugins/sequence/routes/index.ts +3 -3
  127. package/src/plugins/sequence/segment-utility.ts +11 -11
  128. package/src/plugins/sequence/segments/autoIncrement.ts +90 -90
  129. package/src/plugins/sequence/segments/dayOfMonth.ts +19 -19
  130. package/src/plugins/sequence/segments/index.ts +9 -9
  131. package/src/plugins/sequence/segments/literal.ts +16 -16
  132. package/src/plugins/sequence/segments/month.ts +19 -19
  133. package/src/plugins/sequence/segments/parameter.ts +20 -20
  134. package/src/plugins/sequence/segments/year.ts +19 -19
  135. package/src/plugins/serverOperation/ServerOperationPlugin.ts +91 -91
  136. package/src/plugins/serverOperation/ServerOperationPluginTypes.ts +15 -15
  137. package/src/plugins/serverOperation/actionHandlers/index.ts +4 -4
  138. package/src/plugins/serverOperation/actionHandlers/runServerOperation.ts +15 -15
  139. package/src/plugins/setting/SettingPlugin.ts +68 -68
  140. package/src/plugins/setting/SettingPluginTypes.ts +37 -37
  141. package/src/plugins/setting/SettingService.ts +213 -213
  142. package/src/plugins/setting/actionHandlers/getSystemSettingValues.ts +30 -30
  143. package/src/plugins/setting/actionHandlers/getUserSettingValues.ts +38 -38
  144. package/src/plugins/setting/actionHandlers/index.ts +6 -6
  145. package/src/plugins/setting/actionHandlers/setSystemSettingValues.ts +30 -30
  146. package/src/plugins/setting/models/SystemSettingGroupSetting.ts +57 -57
  147. package/src/plugins/setting/models/SystemSettingItem.ts +48 -48
  148. package/src/plugins/setting/models/SystemSettingItemSetting.ts +73 -73
  149. package/src/plugins/setting/models/UserSettingGroupSetting.ts +57 -57
  150. package/src/plugins/setting/models/UserSettingItem.ts +55 -55
  151. package/src/plugins/setting/models/UserSettingItemSetting.ts +73 -73
  152. package/src/plugins/setting/models/index.ts +8 -8
  153. package/src/plugins/setting/routes/getSystemSettingValues.ts +15 -15
  154. package/src/plugins/setting/routes/getUserSettingValues.ts +15 -15
  155. package/src/plugins/setting/routes/index.ts +5 -5
  156. package/src/plugins/setting/routes/setSystemSettingValues.ts +15 -15
  157. package/src/plugins/stateMachine/StateMachinePlugin.ts +196 -196
  158. package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
  159. package/src/plugins/stateMachine/actionHandlers/index.ts +4 -4
  160. package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +54 -54
  161. package/src/plugins/stateMachine/models/StateMachine.ts +42 -42
  162. package/src/plugins/stateMachine/models/index.ts +3 -3
  163. package/src/plugins/stateMachine/routes/index.ts +3 -3
  164. package/src/plugins/stateMachine/routes/sendStateMachineEvent.ts +15 -15
  165. package/src/plugins/stateMachine/stateMachineHelper.ts +36 -36
  166. package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
  167. package/src/plugins/webhooks/pluginConfig.ts +75 -75
  168. package/src/polyfill.ts +5 -5
  169. package/src/proxy/mod.ts +38 -38
  170. package/src/proxy/types.ts +21 -21
  171. package/src/queryBuilder/index.ts +1 -1
  172. package/src/queryBuilder/queryBuilder.ts +668 -668
  173. package/src/server.ts +480 -480
  174. package/src/types.ts +722 -722
  175. package/src/utilities/accessControlUtility.ts +33 -33
  176. package/src/utilities/entityUtility.ts +18 -0
  177. package/src/utilities/errorUtility.ts +15 -15
  178. package/src/utilities/fsUtility.ts +61 -61
  179. package/src/utilities/httpUtility.ts +19 -19
  180. package/src/utilities/jwtUtility.ts +26 -26
  181. package/src/utilities/passwordUtility.ts +26 -0
  182. package/src/utilities/pathUtility.ts +14 -14
  183. package/src/utilities/timeUtility.ts +9 -9
  184. package/src/utilities/typeUtility.ts +15 -15
  185. package/tsconfig.json +19 -19
package/CHANGELOG.md CHANGED
@@ -1,7 +1,7 @@
1
- # @ruiapp/rapid-core
2
-
3
- ## 0.2.4
4
-
5
- ### Patch Changes
6
-
7
- - 146a99d: add between filterMode, range is deprecated
1
+ # @ruiapp/rapid-core
2
+
3
+ ## 0.2.4
4
+
5
+ ### Patch Changes
6
+
7
+ - 146a99d: add between filterMode, range is deprecated
@@ -7,8 +7,10 @@ export interface CacheProvider {
7
7
  createCache: (server: IRpdServer) => Promise<Cache>;
8
8
  }
9
9
  export interface Cache {
10
- set: (key: string, value: any, options?: SetValueOptions) => Promise<void>;
11
- get: (key: string) => Promise<any>;
10
+ set: (key: string, value: string, options?: SetValueOptions) => Promise<void>;
11
+ setObject: (key: string, value: Record<string, any>, options?: SetValueOptions) => Promise<void>;
12
+ get: (key: string) => Promise<string | null>;
13
+ getObject: (key: string) => Promise<Record<string, any> | null>;
12
14
  del: (key: string) => Promise<void>;
13
15
  }
14
16
  export interface SetValueOptions {
@@ -4,7 +4,9 @@ export type MemoryCacheItem<TValue = any> = {
4
4
  expireAt: number;
5
5
  };
6
6
  export default class MemoryCache implements Cache {
7
- set(key: string, value: any, options?: SetValueOptions): Promise<void>;
7
+ set(key: string, value: string, options?: SetValueOptions): Promise<void>;
8
+ setObject(key: string, value: Record<string, any>, options?: SetValueOptions): Promise<void>;
8
9
  get(key: string): Promise<any>;
10
+ getObject(key: string): Promise<any>;
9
11
  del(key: string): Promise<void>;
10
12
  }
package/dist/index.d.ts CHANGED
@@ -6,7 +6,11 @@ export * from "./core/routeContext";
6
6
  export * from "./core/server";
7
7
  export * from "./core/http-types";
8
8
  export * from "./core/actionHandler";
9
+ export * from "./utilities/accessControlUtility";
10
+ export * from "./utilities/entityUtility";
9
11
  export * from "./utilities/jwtUtility";
12
+ export * from "./utilities/timeUtility";
13
+ export * from "./utilities/passwordUtility";
10
14
  export * from "./deno-std/http/cookie";
11
15
  export { mapDbRowToEntity } from "./dataAccess/entityMapper";
12
16
  export * as bootstrapApplicationConfig from "./bootstrapApplicationConfig";
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ var qs = require('qs');
9
9
  var dayjs = require('dayjs');
10
10
  var jsonwebtoken = require('jsonwebtoken');
11
11
  var crypto = require('crypto');
12
- var bcrypt = require('bcrypt');
12
+ var bcrypt = require('bcryptjs');
13
13
  var path = require('path');
14
14
  var fs = require('fs');
15
15
  var uuid = require('uuid');
@@ -2509,6 +2509,9 @@ function getEntityPartChanges(server, model, before, after) {
2509
2509
  return null;
2510
2510
  }
2511
2511
 
2512
+ function getNowString() {
2513
+ return dayjs__default["default"]().format("YYYY-MM-DD HH:mm:ss.SSS");
2514
+ }
2512
2515
  function getNowStringWithTimezone() {
2513
2516
  return dayjs__default["default"]().format("YYYY-MM-DD HH:mm:ss.SSSZ");
2514
2517
  }
@@ -3013,8 +3016,8 @@ async function findManyRelationLinksViaLinkTable(options) {
3013
3016
  const command = `SELECT * FROM ${server.queryBuilder.quoteTable({
3014
3017
  schema: relationProperty.linkSchema,
3015
3018
  tableName: relationProperty.linkTableName,
3016
- })} WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = ANY($1::int[])
3017
- ORDER BY id
3019
+ })} WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = ANY($1::int[])
3020
+ ORDER BY id
3018
3021
  `;
3019
3022
  const params = [mainEntityIds];
3020
3023
  const links = await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
@@ -3636,7 +3639,7 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
3636
3639
  await server.queryDatabaseObject(`DELETE FROM ${server.queryBuilder.quoteTable({
3637
3640
  schema: property.linkSchema,
3638
3641
  tableName: property.linkTableName,
3639
- })} WHERE ${server.queryBuilder.quoteObject(property.selfIdColumnName)} = $1
3642
+ })} WHERE ${server.queryBuilder.quoteObject(property.selfIdColumnName)} = $1
3640
3643
  AND ${server.queryBuilder.quoteObject(property.targetIdColumnName)} <> ALL($2::int[])`, [id, targetIdsToKeep], routeContext?.getDbTransactionClient());
3641
3644
  }
3642
3645
  else {
@@ -3665,8 +3668,8 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
3665
3668
  await server.queryDatabaseObject(`UPDATE ${server.queryBuilder.quoteTable({
3666
3669
  schema: relationModel.schema,
3667
3670
  tableName: relationModel.tableName,
3668
- })}
3669
- SET ${server.queryBuilder.quoteObject(property.selfIdColumnName)} = null
3671
+ })}
3672
+ SET ${server.queryBuilder.quoteObject(property.selfIdColumnName)} = null
3670
3673
  WHERE id = ANY($1::int[])`, [targetIdsToRemove], routeContext?.getDbTransactionClient());
3671
3674
  }
3672
3675
  else {
@@ -3951,7 +3954,7 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
3951
3954
  await server.queryDatabaseObject(`DELETE FROM ${server.queryBuilder.quoteTable({
3952
3955
  schema: relationProperty.linkSchema,
3953
3956
  tableName: relationProperty.linkTableName,
3954
- })}
3957
+ })}
3955
3958
  WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = $1`, [id], routeContext?.getDbTransactionClient());
3956
3959
  }
3957
3960
  else {
@@ -3961,8 +3964,8 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
3961
3964
  await server.queryDatabaseObject(`UPDATE ${server.queryBuilder.quoteTable({
3962
3965
  schema: relationModel.schema,
3963
3966
  tableName: relationModel.tableName,
3964
- })}
3965
- SET ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = null
3967
+ })}
3968
+ SET ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = null
3966
3969
  WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = $1`, [id], routeContext?.getDbTransactionClient());
3967
3970
  }
3968
3971
  }
@@ -4059,11 +4062,11 @@ class EntityManager {
4059
4062
  const command = `INSERT INTO ${queryBuilder.quoteTable({
4060
4063
  schema: relationProperty.linkSchema,
4061
4064
  tableName: relationProperty.linkTableName,
4062
- })} (${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)})
4063
- SELECT $1, $2 WHERE NOT EXISTS (
4064
- SELECT ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}
4065
- FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4066
- WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2
4065
+ })} (${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)})
4066
+ SELECT $1, $2 WHERE NOT EXISTS (
4067
+ SELECT ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}
4068
+ FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4069
+ WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2
4067
4070
  )`;
4068
4071
  const params = [id, relation.id];
4069
4072
  await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
@@ -4103,7 +4106,7 @@ class EntityManager {
4103
4106
  const { queryBuilder } = server;
4104
4107
  if (relationProperty.linkTableName) {
4105
4108
  for (const relation of relations) {
4106
- const command = `DELETE FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4109
+ const command = `DELETE FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4107
4110
  WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2;`;
4108
4111
  const params = [id, relation.id];
4109
4112
  await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
@@ -4511,6 +4514,46 @@ class RapidServer {
4511
4514
  }
4512
4515
  }
4513
4516
 
4517
+ function isAccessAllowed(policy, allowedActions) {
4518
+ let isAnyCheckPassed = true;
4519
+ let isAllCheckPassed = true;
4520
+ if (policy.any) {
4521
+ isAnyCheckPassed = false;
4522
+ for (const action of policy.any) {
4523
+ if (lodash.find(allowedActions, (item) => item === action) != null) {
4524
+ isAnyCheckPassed = true;
4525
+ break;
4526
+ }
4527
+ }
4528
+ }
4529
+ if (policy.all) {
4530
+ isAllCheckPassed = true;
4531
+ for (const action of policy.all) {
4532
+ if (lodash.find(allowedActions, (item) => item === action) == null) {
4533
+ isAnyCheckPassed = false;
4534
+ break;
4535
+ }
4536
+ }
4537
+ }
4538
+ return isAnyCheckPassed && isAllCheckPassed;
4539
+ }
4540
+
4541
+ function getEntityRelationTargetId(entity, propName, targetColumnName) {
4542
+ if (!entity) {
4543
+ throw new Error(`"entity" parameter is required.`);
4544
+ }
4545
+ let targetId = entity[propName];
4546
+ if (!targetId && targetColumnName) {
4547
+ targetId = entity[targetColumnName];
4548
+ }
4549
+ if (lodash.isObject(targetId)) {
4550
+ return targetId.id;
4551
+ }
4552
+ else {
4553
+ return targetId;
4554
+ }
4555
+ }
4556
+
4514
4557
  // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
4515
4558
  // This module is browser compatible.
4516
4559
  /**
@@ -4654,30 +4697,66 @@ async function generateJwtSecretKey() {
4654
4697
  return encode(exportedKey);
4655
4698
  }
4656
4699
 
4700
+ /**
4701
+ * Generates password hash.
4702
+ * @param password
4703
+ * @param salt
4704
+ * @returns
4705
+ */
4706
+ async function generatePasswordHash(password, salt) {
4707
+ if (!salt) {
4708
+ salt = 10;
4709
+ }
4710
+ const passwordHash = await bcrypt__default["default"].hash(password, salt);
4711
+ return passwordHash;
4712
+ }
4713
+ /**
4714
+ * Validates the password against the hash.
4715
+ * @param password
4716
+ * @param passwordHash
4717
+ * @returns
4718
+ */
4719
+ async function validatePassword(password, passwordHash) {
4720
+ const isMatch = await bcrypt__default["default"].compare(password, passwordHash);
4721
+ return isMatch;
4722
+ }
4723
+
4657
4724
  const values = new Map();
4725
+ async function set(key, value, options) {
4726
+ let expireAt = -1;
4727
+ if (options && options.ttl > 0) {
4728
+ expireAt = new Date().valueOf() + options.ttl;
4729
+ }
4730
+ const cacheItem = {
4731
+ value,
4732
+ expireAt,
4733
+ };
4734
+ values.set(key, cacheItem);
4735
+ }
4736
+ async function get(key) {
4737
+ const cacheItem = values.get(key);
4738
+ if (cacheItem) {
4739
+ if (cacheItem.expireAt === -1) {
4740
+ return cacheItem.value;
4741
+ }
4742
+ if (cacheItem.expireAt >= new Date().valueOf()) {
4743
+ return cacheItem.value;
4744
+ }
4745
+ }
4746
+ return undefined;
4747
+ }
4658
4748
  class MemoryCache {
4659
4749
  async set(key, value, options) {
4660
- let expireAt = -1;
4661
- if (options && options.ttl > 0) {
4662
- expireAt = new Date().valueOf() + options.ttl;
4663
- }
4664
- const cacheItem = {
4665
- value,
4666
- expireAt,
4667
- };
4668
- values.set(key, cacheItem);
4750
+ await set(key, value, options);
4751
+ }
4752
+ async setObject(key, value, options) {
4753
+ await set(key, value, options);
4669
4754
  }
4670
4755
  async get(key) {
4671
- const cacheItem = values.get(key);
4672
- if (cacheItem) {
4673
- if (cacheItem.expireAt === -1) {
4674
- return cacheItem.value;
4675
- }
4676
- if (cacheItem.expireAt >= new Date().valueOf()) {
4677
- return cacheItem.value;
4678
- }
4679
- }
4680
- return undefined;
4756
+ return get(key);
4757
+ }
4758
+ async getObject(key) {
4759
+ return get(key);
4681
4760
  }
4682
4761
  async del(key) {
4683
4762
  values.delete(key);
@@ -4699,9 +4778,9 @@ class CacheFactory {
4699
4778
  #providers;
4700
4779
  constructor(config) {
4701
4780
  this.name = "cache";
4702
- const memoryCacheCreator = new MemoryCacheProvider();
4781
+ const memoryCacheProvider = new MemoryCacheProvider();
4703
4782
  this.#providers = new Map();
4704
- this.#providers.set(memoryCacheCreator.providerName, memoryCacheCreator);
4783
+ this.#providers.set(memoryCacheProvider.providerName, memoryCacheProvider);
4705
4784
  for (const provider of config.providers) {
4706
4785
  this.#providers.set(provider.providerName, provider);
4707
4786
  }
@@ -5003,9 +5082,9 @@ async function syncDatabaseSchema(server, applicationConfig) {
5003
5082
  await server.queryDatabaseObject(`COMMENT ON TABLE ${queryBuilder.quoteTable(model)} IS ${queryBuilder.formatValueToSqlLiteral(model.name)};`, []);
5004
5083
  }
5005
5084
  }
5006
- const sqlQueryColumnInformations = `SELECT c.table_schema, c.table_name, c.column_name, c.ordinal_position, d.description, c.data_type, c.udt_name, c.is_nullable, c.column_default, c.character_maximum_length, c.numeric_precision, c.numeric_scale
5007
- FROM information_schema.columns c
5008
- INNER JOIN pg_catalog.pg_statio_all_tables st ON (st.schemaname = c.table_schema and st.relname = c.table_name)
5085
+ const sqlQueryColumnInformations = `SELECT c.table_schema, c.table_name, c.column_name, c.ordinal_position, d.description, c.data_type, c.udt_name, c.is_nullable, c.column_default, c.character_maximum_length, c.numeric_precision, c.numeric_scale
5086
+ FROM information_schema.columns c
5087
+ INNER JOIN pg_catalog.pg_statio_all_tables st ON (st.schemaname = c.table_schema and st.relname = c.table_name)
5009
5088
  LEFT JOIN pg_catalog.pg_description d ON (d.objoid = st.relid and d.objsubid = c.ordinal_position);`;
5010
5089
  const columnsInDb = await server.queryDatabaseObject(sqlQueryColumnInformations, []);
5011
5090
  for (const model of applicationConfig.models) {
@@ -6433,12 +6512,11 @@ async function handler$e(plugin, ctx, options) {
6433
6512
  if (!user) {
6434
6513
  throw new Error("User not found.");
6435
6514
  }
6436
- const isMatch = await bcrypt__default["default"].compare(oldPassword, user.password);
6515
+ const isMatch = await validatePassword(oldPassword, user.password);
6437
6516
  if (!isMatch) {
6438
6517
  throw new Error("旧密码错误。");
6439
6518
  }
6440
- const saltRounds = 10;
6441
- const passwordHash = await bcrypt__default["default"].hash(newPassword, saltRounds);
6519
+ const passwordHash = await generatePasswordHash(newPassword);
6442
6520
  await userDataAccessor.updateById(user.id, {
6443
6521
  password: passwordHash,
6444
6522
  }, routeContext?.getDbTransactionClient());
@@ -6480,7 +6558,7 @@ async function handler$d(plugin, ctx, options) {
6480
6558
  if (!user) {
6481
6559
  throw new Error("用户名或密码错误。");
6482
6560
  }
6483
- const isMatch = await bcrypt__default["default"].compare(password, user.password);
6561
+ const isMatch = await validatePassword(password, user.password);
6484
6562
  if (!isMatch) {
6485
6563
  throw new Error("用户名或密码错误。");
6486
6564
  }
@@ -6578,8 +6656,7 @@ async function handler$a(plugin, ctx, options) {
6578
6656
  if (!user) {
6579
6657
  throw new Error("User not found.");
6580
6658
  }
6581
- const saltRounds = 10;
6582
- const passwordHash = await bcrypt__default["default"].hash(password, saltRounds);
6659
+ const passwordHash = await generatePasswordHash(password);
6583
6660
  await userDataAccessor.updateById(user.id, {
6584
6661
  password: passwordHash,
6585
6662
  }, routeContext?.getDbTransactionClient());
@@ -8591,30 +8668,6 @@ function getStateMachineCode(model, property) {
8591
8668
  }
8592
8669
  }
8593
8670
 
8594
- function isAccessAllowed(policy, allowedActions) {
8595
- let isAnyCheckPassed = true;
8596
- let isAllCheckPassed = true;
8597
- if (policy.any) {
8598
- isAnyCheckPassed = false;
8599
- for (const action of policy.any) {
8600
- if (lodash.find(allowedActions, (item) => item === action) != null) {
8601
- isAnyCheckPassed = true;
8602
- break;
8603
- }
8604
- }
8605
- }
8606
- if (policy.all) {
8607
- isAllCheckPassed = true;
8608
- for (const action of policy.all) {
8609
- if (lodash.find(allowedActions, (item) => item === action) == null) {
8610
- isAnyCheckPassed = false;
8611
- break;
8612
- }
8613
- }
8614
- }
8615
- return isAnyCheckPassed && isAllCheckPassed;
8616
- }
8617
-
8618
8671
  class EntityAccessControlPlugin {
8619
8672
  constructor() { }
8620
8673
  get code() {
@@ -8713,9 +8766,9 @@ class EntityAccessControlPlugin {
8713
8766
  if (!userId) {
8714
8767
  return;
8715
8768
  }
8716
- const actions = await server.queryDatabaseObject(`select distinct a.* from sys_actions a
8717
- inner join oc_role_sys_action_links ra on a.id = ra.action_id
8718
- inner join oc_role_user_links ru on ru.role_id = ra.role_id
8769
+ const actions = await server.queryDatabaseObject(`select distinct a.* from sys_actions a
8770
+ inner join oc_role_sys_action_links ra on a.id = ra.action_id
8771
+ inner join oc_role_user_links ru on ru.role_id = ra.role_id
8719
8772
  where ru.user_id = $1;`, [userId]);
8720
8773
  routeContext.state.allowedActions = actions.map((item) => item.code);
8721
8774
  }
@@ -8761,8 +8814,14 @@ exports.createJwt = createJwt;
8761
8814
  exports.decodeJwt = decodeJwt;
8762
8815
  exports.deleteCookie = deleteCookie;
8763
8816
  exports.generateJwtSecretKey = generateJwtSecretKey;
8817
+ exports.generatePasswordHash = generatePasswordHash;
8764
8818
  exports.getCookies = getCookies;
8819
+ exports.getEntityRelationTargetId = getEntityRelationTargetId;
8820
+ exports.getNowString = getNowString;
8821
+ exports.getNowStringWithTimezone = getNowStringWithTimezone;
8765
8822
  exports.getSetCookies = getSetCookies;
8823
+ exports.isAccessAllowed = isAccessAllowed;
8766
8824
  exports.mapDbRowToEntity = mapDbRowToEntity;
8767
8825
  exports.setCookie = setCookie;
8826
+ exports.validatePassword = validatePassword;
8768
8827
  exports.verifyJwt = verifyJwt;
@@ -0,0 +1 @@
1
+ export declare function getEntityRelationTargetId(entity: Record<string, any>, propName: string, targetColumnName?: string): number;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Generates password hash.
3
+ * @param password
4
+ * @param salt
5
+ * @returns
6
+ */
7
+ export declare function generatePasswordHash(password: string, salt?: number | string): Promise<string>;
8
+ /**
9
+ * Validates the password against the hash.
10
+ * @param password
11
+ * @param passwordHash
12
+ * @returns
13
+ */
14
+ export declare function validatePassword(password: string, passwordHash: string): Promise<boolean>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.5.11",
3
+ "version": "0.5.13",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,7 +19,7 @@
19
19
  "typescript": "^4.8.4"
20
20
  },
21
21
  "dependencies": {
22
- "bcrypt": "^5.1.1",
22
+ "bcryptjs": "^3.0.2",
23
23
  "cron": "^3.1.7",
24
24
  "dayjs": "^1.11.7",
25
25
  "jsonwebtoken": "^9.0.2",
package/rollup.config.js CHANGED
@@ -1,16 +1,16 @@
1
- import typescript from "rollup-plugin-typescript2";
2
- import tscAlias from "rollup-plugin-tsc-alias";
3
-
4
- export default {
5
- input: ["src/index.ts"],
6
- output: [
7
- {
8
- dir: "dist",
9
- entryFileNames: "[name].js",
10
- format: "cjs",
11
- exports: "named",
12
- },
13
- ],
14
- plugins: [typescript(), tscAlias()],
15
- external: [],
16
- };
1
+ import typescript from "rollup-plugin-typescript2";
2
+ import tscAlias from "rollup-plugin-tsc-alias";
3
+
4
+ export default {
5
+ input: ["src/index.ts"],
6
+ output: [
7
+ {
8
+ dir: "dist",
9
+ entryFileNames: "[name].js",
10
+ format: "cjs",
11
+ exports: "named",
12
+ },
13
+ ],
14
+ plugins: [typescript(), tscAlias()],
15
+ external: [],
16
+ };