@tomei/sso 0.32.5 → 0.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. package/.commitlintrc.json +22 -22
  2. package/.eslintrc +16 -16
  3. package/.eslintrc.js +35 -35
  4. package/.gitlab-ci.yml +16 -16
  5. package/.husky/commit-msg +15 -15
  6. package/.husky/pre-commit +7 -7
  7. package/.prettierrc +4 -4
  8. package/Jenkinsfile +57 -57
  9. package/README.md +23 -23
  10. package/__tests__/unit/components/group/group.spec.ts +79 -79
  11. package/__tests__/unit/components/group-object-privilege/group-object-privilege.spec.ts +88 -88
  12. package/__tests__/unit/components/group-privilege/group-privilege.spec.ts +68 -68
  13. package/__tests__/unit/components/group-reporting-user/group-reporting-user.spec.ts +66 -66
  14. package/__tests__/unit/components/group-system-access/group-system-access.spec.ts +83 -83
  15. package/__tests__/unit/components/login-user/l.spec.ts +746 -746
  16. package/__tests__/unit/components/login-user/login.spec.ts +1064 -1064
  17. package/__tests__/unit/components/password-hash/password-hash.service.spec.ts +31 -31
  18. package/__tests__/unit/components/system/system.spec.ts +254 -254
  19. package/__tests__/unit/components/system-privilege/system-privilege.spec.ts +83 -83
  20. package/__tests__/unit/components/user-group/user-group.spec.ts +86 -86
  21. package/__tests__/unit/components/user-object-privilege/user-object-privilege.spec.ts +78 -78
  22. package/__tests__/unit/components/user-privilege/user-privilege.spec.ts +72 -72
  23. package/__tests__/unit/components/user-system-access/user-system-access.spec.ts +89 -89
  24. package/__tests__/unit/redis-client/redis.service.spec.ts +23 -23
  25. package/__tests__/unit/session/session.service.spec.ts +47 -47
  26. package/__tests__/unit/system-privilege/system-privilage.spec.ts +91 -91
  27. package/coverage/clover.xml +1452 -1452
  28. package/coverage/coverage-final.json +47 -47
  29. package/coverage/lcov-report/base.css +224 -224
  30. package/coverage/lcov-report/block-navigation.js +87 -87
  31. package/coverage/lcov-report/components/group/group.repository.ts.html +117 -117
  32. package/coverage/lcov-report/components/group/group.ts.html +327 -327
  33. package/coverage/lcov-report/components/group/index.html +130 -130
  34. package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.repository.ts.html +117 -117
  35. package/coverage/lcov-report/components/group-object-privilege/group-object-privilege.ts.html +321 -321
  36. package/coverage/lcov-report/components/group-object-privilege/index.html +130 -130
  37. package/coverage/lcov-report/components/group-privilege/group-privilege.repository.ts.html +117 -117
  38. package/coverage/lcov-report/components/group-privilege/group-privilege.ts.html +303 -303
  39. package/coverage/lcov-report/components/group-privilege/index.html +130 -130
  40. package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.repository.ts.html +117 -117
  41. package/coverage/lcov-report/components/group-reporting-user/group-reporting-user.ts.html +327 -327
  42. package/coverage/lcov-report/components/group-reporting-user/index.html +130 -130
  43. package/coverage/lcov-report/components/group-system-access/group-system-access.repository.ts.html +117 -117
  44. package/coverage/lcov-report/components/group-system-access/group-system-access.ts.html +309 -309
  45. package/coverage/lcov-report/components/group-system-access/index.html +130 -130
  46. package/coverage/lcov-report/components/login-history/index.html +115 -115
  47. package/coverage/lcov-report/components/login-history/login-history.repository.ts.html +117 -117
  48. package/coverage/lcov-report/components/login-user/index.html +130 -130
  49. package/coverage/lcov-report/components/login-user/login-user.ts.html +5007 -5007
  50. package/coverage/lcov-report/components/login-user/user.repository.ts.html +117 -117
  51. package/coverage/lcov-report/components/password-hash/index.html +115 -115
  52. package/coverage/lcov-report/components/password-hash/password-hash.service.ts.html +126 -126
  53. package/coverage/lcov-report/components/system/index.html +130 -130
  54. package/coverage/lcov-report/components/system/system.repository.ts.html +117 -117
  55. package/coverage/lcov-report/components/system/system.ts.html +909 -909
  56. package/coverage/lcov-report/components/system-privilege/index.html +130 -130
  57. package/coverage/lcov-report/components/system-privilege/system-privilege.repository.ts.html +120 -120
  58. package/coverage/lcov-report/components/system-privilege/system-privilege.ts.html +390 -390
  59. package/coverage/lcov-report/components/user-group/index.html +130 -130
  60. package/coverage/lcov-report/components/user-group/user-group.repository.ts.html +117 -117
  61. package/coverage/lcov-report/components/user-group/user-group.ts.html +354 -354
  62. package/coverage/lcov-report/components/user-object-privilege/index.html +130 -130
  63. package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.repository.ts.html +117 -117
  64. package/coverage/lcov-report/components/user-object-privilege/user-object-privilege.ts.html +312 -312
  65. package/coverage/lcov-report/components/user-privilege/index.html +130 -130
  66. package/coverage/lcov-report/components/user-privilege/user-privilege.repository.ts.html +117 -117
  67. package/coverage/lcov-report/components/user-privilege/user-privilege.ts.html +306 -306
  68. package/coverage/lcov-report/components/user-system-access/index.html +130 -130
  69. package/coverage/lcov-report/components/user-system-access/user-system-access.repository.ts.html +117 -117
  70. package/coverage/lcov-report/components/user-system-access/user-system-access.ts.html +312 -312
  71. package/coverage/lcov-report/enum/group-type.enum.ts.html +108 -108
  72. package/coverage/lcov-report/enum/index.html +160 -160
  73. package/coverage/lcov-report/enum/index.ts.html +93 -93
  74. package/coverage/lcov-report/enum/user-status.enum.ts.html +105 -105
  75. package/coverage/lcov-report/enum/yn.enum.ts.html +96 -96
  76. package/coverage/lcov-report/index.html +370 -370
  77. package/coverage/lcov-report/models/group-object-privilege.entity.ts.html +333 -333
  78. package/coverage/lcov-report/models/group-privilege.entity.ts.html +315 -315
  79. package/coverage/lcov-report/models/group-reporting-user.entity.ts.html +339 -339
  80. package/coverage/lcov-report/models/group-system-access.entity.ts.html +324 -324
  81. package/coverage/lcov-report/models/group.entity.ts.html +435 -435
  82. package/coverage/lcov-report/models/index.html +310 -310
  83. package/coverage/lcov-report/models/login-history.entity.ts.html +252 -252
  84. package/coverage/lcov-report/models/staff.entity.ts.html +411 -411
  85. package/coverage/lcov-report/models/system-privilege.entity.ts.html +354 -354
  86. package/coverage/lcov-report/models/system.entity.ts.html +423 -423
  87. package/coverage/lcov-report/models/user-group.entity.ts.html +354 -354
  88. package/coverage/lcov-report/models/user-object-privilege.entity.ts.html +330 -330
  89. package/coverage/lcov-report/models/user-privilege.entity.ts.html +315 -315
  90. package/coverage/lcov-report/models/user-system-access.entity.ts.html +315 -315
  91. package/coverage/lcov-report/models/user.entity.ts.html +522 -522
  92. package/coverage/lcov-report/prettify.css +1 -1
  93. package/coverage/lcov-report/prettify.js +2 -2
  94. package/coverage/lcov-report/redis-client/index.html +115 -115
  95. package/coverage/lcov-report/redis-client/redis.service.ts.html +240 -240
  96. package/coverage/lcov-report/session/index.html +115 -115
  97. package/coverage/lcov-report/session/session.service.ts.html +246 -246
  98. package/coverage/lcov-report/sorter.js +196 -196
  99. package/coverage/lcov.info +2490 -2490
  100. package/coverage/test-report.xml +128 -128
  101. package/create-sso-user.sql +39 -39
  102. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.d.ts +1 -0
  103. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.js +71 -0
  104. package/dist/__tests__/unit/components/group-privilege/group-privilege.test.js.map +1 -0
  105. package/dist/__tests__/unit/components/login-user/login-user.spec.d.ts +0 -0
  106. package/dist/__tests__/unit/components/login-user/login-user.spec.js +6 -0
  107. package/dist/__tests__/unit/components/login-user/login-user.spec.js.map +1 -0
  108. package/dist/src/components/group/group.d.ts +6 -1
  109. package/dist/src/components/group/group.js +47 -5
  110. package/dist/src/components/group/group.js.map +1 -1
  111. package/dist/src/components/group-system-access/group-system-access.repository.d.ts +1 -0
  112. package/dist/src/components/group-system-access/group-system-access.repository.js +17 -0
  113. package/dist/src/components/group-system-access/group-system-access.repository.js.map +1 -1
  114. package/dist/src/components/login-user/login-user.d.ts +2 -1
  115. package/dist/src/components/login-user/login-user.js +2 -0
  116. package/dist/src/components/login-user/login-user.js.map +1 -1
  117. package/dist/src/redis-client/redis.service.d.ts +1 -0
  118. package/dist/src/redis-client/redis.service.js +20 -3
  119. package/dist/src/redis-client/redis.service.js.map +1 -1
  120. package/dist/tsconfig.tsbuildinfo +1 -1
  121. package/jest.config.js +14 -14
  122. package/migrations/20240314080602-create-user-table.js +108 -108
  123. package/migrations/20240314080603-create-user-group-table.js +85 -85
  124. package/migrations/20240314080604-create-user-user-group-table.js +55 -55
  125. package/migrations/20240314080605-create-login-history-table.js +53 -53
  126. package/migrations/20240527064925-create-system-table.js +78 -78
  127. package/migrations/20240527064926-create-system-privilege-table.js +67 -67
  128. package/migrations/20240527065342-create-group-table.js +89 -89
  129. package/migrations/20240527065633-create-group-reporting-user-table.js +76 -76
  130. package/migrations/20240528011551-create-group-system-access-table.js +72 -72
  131. package/migrations/20240528023018-user-system-access-table.js +75 -75
  132. package/migrations/20240528032229-user-privilege-table.js +75 -75
  133. package/migrations/20240528063003-create-group-privilege-table.js +75 -75
  134. package/migrations/20240528063051-create-group-object-privilege-table.js +84 -84
  135. package/migrations/20240528063107-create-user-object-privilege-table.js +83 -83
  136. package/package.json +89 -89
  137. package/sampledotenv +7 -7
  138. package/sonar-project.properties +22 -22
  139. package/src/components/group/group.ts +81 -6
  140. package/src/components/group-system-access/group-system-access.repository.ts +15 -0
  141. package/src/components/login-user/login-user.ts +3 -0
  142. package/src/redis-client/redis.service.ts +18 -3
  143. package/tsconfig.build.json +5 -5
  144. package/tsconfig.json +22 -22
@@ -10,6 +10,7 @@ import { ActionEnum, Activity } from '@tomei/activity-history';
10
10
  import { GroupSystemAccessRepository } from '../group-system-access/group-system-access.repository';
11
11
  import SystemModel from '../../models/system.entity';
12
12
  import { GroupSystemAccess } from '../group-system-access';
13
+ import { RedisService } from 'redis-client';
13
14
 
14
15
  export class Group extends ObjectBase {
15
16
  ObjectId: string;
@@ -30,8 +31,8 @@ export class Group extends ObjectBase {
30
31
  private _UpdatedById: number;
31
32
  private _UpdatedAt: Date;
32
33
  private static _Repo = new GroupRepository();
33
- private static _SystemAccessRepo = new GroupSystemAccessRepository();
34
-
34
+ private static _GroupSystemAccessRepo = new GroupSystemAccessRepository();
35
+ private static _RedisService: RedisService;
35
36
  get GroupCode(): string {
36
37
  return this.ObjectId;
37
38
  }
@@ -76,6 +77,7 @@ export class Group extends ObjectBase {
76
77
 
77
78
  public static async init(dbTransaction: any, GroupCode?: string) {
78
79
  try {
80
+ Group._RedisService = await RedisService.init();
79
81
  if (GroupCode) {
80
82
  const group = await Group._Repo.findByPk(GroupCode, {
81
83
  transaction: dbTransaction,
@@ -507,7 +509,7 @@ export class Group extends ObjectBase {
507
509
  };
508
510
  }
509
511
 
510
- const systemAccess = await Group._SystemAccessRepo.findAndCountAll();
512
+ const systemAccess = await Group._GroupSystemAccessRepo.findAndCountAll();
511
513
  return systemAccess;
512
514
  } catch (error) {
513
515
  return error;
@@ -530,7 +532,7 @@ export class Group extends ObjectBase {
530
532
  ],
531
533
  transaction: dbTransaction,
532
534
  };
533
- let systemAccess = await Group._SystemAccessRepo.findAll(options);
535
+ let systemAccess = await Group._GroupSystemAccessRepo.findAll(options);
534
536
 
535
537
  if (group.InheritParentSystemAccessYN === 'Y') {
536
538
  const parentGroup = await Group.init(
@@ -610,7 +612,7 @@ export class Group extends ObjectBase {
610
612
  try {
611
613
  if (SystemCodes.length > 0) {
612
614
  for (let i = 0; i < SystemCodes.length; i++) {
613
- const CurrentGroupSystemAccess = Group.getSystemAccesses(
615
+ const CurrentGroupSystemAccess = await Group.getSystemAccesses(
614
616
  loginUser,
615
617
  dbTransaction,
616
618
  GroupCode,
@@ -648,7 +650,7 @@ export class Group extends ObjectBase {
648
650
  UpdatedAt: groupSystemAccess.UpdatedAt,
649
651
  };
650
652
 
651
- const systemAccess = await Group._SystemAccessRepo.create(
653
+ const systemAccess = await Group._GroupSystemAccessRepo.create(
652
654
  EntityValueAfter,
653
655
  {
654
656
  transaction: dbTransaction,
@@ -673,4 +675,77 @@ export class Group extends ObjectBase {
673
675
  throw error;
674
676
  }
675
677
  }
678
+
679
+ public static async deleteSystemAccess(
680
+ loginUser: LoginUser,
681
+ dbTransaction: any,
682
+ GroupCode: string,
683
+ SystemCode: string,
684
+ ) {
685
+ // Part 1: Privilege Checking
686
+ const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
687
+ const isPrivileged = await loginUser.checkPrivileges(
688
+ systemCode,
689
+ 'SYSTEM_ACCESS_DELETE',
690
+ );
691
+
692
+ if (!isPrivileged) {
693
+ throw new ClassError(
694
+ 'Group',
695
+ 'GroupErrMsg08',
696
+ 'You do not have the privilege to delete system access',
697
+ );
698
+ }
699
+
700
+ try {
701
+ const currentGroupSystemAccess = await Group.getSystemAccesses(
702
+ loginUser,
703
+ dbTransaction,
704
+ GroupCode,
705
+ 1,
706
+ Number.MAX_SAFE_INTEGER,
707
+ { SystemCode: SystemCode },
708
+ );
709
+
710
+ if (!currentGroupSystemAccess) {
711
+ throw new ClassError(
712
+ 'Group',
713
+ 'GroupErrMsg10',
714
+ 'No associated system access found.',
715
+ );
716
+ }
717
+
718
+ await Group._GroupSystemAccessRepo.delete(
719
+ GroupCode,
720
+ SystemCode,
721
+ dbTransaction,
722
+ );
723
+
724
+ const EntityValueBefore = {
725
+ GroupCode: currentGroupSystemAccess.GroupCode,
726
+ SystemCode: currentGroupSystemAccess.SystemCode,
727
+ Status: currentGroupSystemAccess.Status,
728
+ CreatedById: currentGroupSystemAccess.CreatedById,
729
+ CreatedAt: currentGroupSystemAccess.CreatedAt,
730
+ UpdatedById: currentGroupSystemAccess.UpdatedById,
731
+ UpdatedAt: currentGroupSystemAccess.UpdatedAt,
732
+ };
733
+
734
+ const activity = new Activity();
735
+ activity.ActivityId = activity.createId();
736
+ activity.Action = ActionEnum.DELETE;
737
+ activity.Description = 'Delete Group System Access';
738
+ activity.EntityType = 'GroupSystemAccess';
739
+ activity.EntityId =
740
+ currentGroupSystemAccess.GroupSystemAccessId?.toString();
741
+ activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
742
+ activity.EntityValueAfter = JSON.stringify({});
743
+
744
+ await activity.create(loginUser.ObjectId, dbTransaction);
745
+
746
+ return { Message: 'System access removed.', SystemCode: SystemCode };
747
+ } catch (error) {
748
+ throw error;
749
+ }
750
+ }
676
751
  }
@@ -26,4 +26,19 @@ export class GroupSystemAccessRepository
26
26
  );
27
27
  }
28
28
  }
29
+
30
+ async delete(GroupCode: string, SystemCode: string, dbTransaction?: any) {
31
+ try {
32
+ const options = {
33
+ where: {
34
+ GroupCode: GroupCode,
35
+ SystemCode: SystemCode,
36
+ },
37
+ transaction: dbTransaction,
38
+ };
39
+ await GroupSystemAccessModel.destroy(options);
40
+ } catch (error) {
41
+ throw new Error(`An Error occured when delete : ${error.message}`);
42
+ }
43
+ }
29
44
  }
@@ -31,6 +31,7 @@ import GroupPrivilegeModel from '../../models/group-privilege.entity';
31
31
  import { GroupObjectPrivilegeRepository } from '../group-object-privilege/group-object-privilege.repository';
32
32
  import * as speakeasy from 'speakeasy';
33
33
  import { LoginStatusEnum } from '../../enum/login-status.enum';
34
+ import { RedisService } from '../../redis-client/redis.service';
34
35
 
35
36
  export class LoginUser extends LoginUserBase {
36
37
  ObjectId: string;
@@ -59,6 +60,7 @@ export class LoginUser extends LoginUserBase {
59
60
 
60
61
  private _OriginIP: string;
61
62
  private _SessionService: ISessionService;
63
+ private static _RedisService: RedisService;
62
64
  private static _Repository = new UserRepository();
63
65
 
64
66
  private static _LoginHistoryRepository = new LoginHistoryRepository();
@@ -285,6 +287,7 @@ export class LoginUser extends LoginUserBase {
285
287
  userId?: number,
286
288
  dbTransaction = null,
287
289
  ): Promise<LoginUser> {
290
+ LoginUser._RedisService = await RedisService.init();
288
291
  if (userId) {
289
292
  if (dbTransaction) {
290
293
  LoginUser._Repository = new UserRepository();
@@ -8,9 +8,16 @@ export class RedisService {
8
8
  if (!client) {
9
9
  if (!RedisService.client) {
10
10
  // Create a new Redis client
11
- RedisService.client = createClient({
12
- url: process.env.REDIS_URL,
13
- password: process.env.REDIS_PASSWORD,
11
+ const host = process.env.REDIS_HOST;
12
+ const password = process.env.REDIS_PASS;
13
+ const port = process.env.REDIS_PORT;
14
+ const isProduction = process.env.REDIS_HOST.includes('windows.net');
15
+ const redis = isProduction ? 'rediss' : 'redis';
16
+ const pass = password ? `:${password}@` : '';
17
+ console.log(`${redis}://${pass}${host}:${port}`);
18
+ RedisService.client = client = createClient({
19
+ url: `${redis}://${pass}${host}:${port}`,
20
+ password: password,
14
21
  });
15
22
 
16
23
  RedisService.client.on('error', (error) => {
@@ -49,4 +56,12 @@ export class RedisService {
49
56
  throw error;
50
57
  }
51
58
  }
59
+
60
+ public async close(): Promise<void> {
61
+ try {
62
+ await RedisService.client.disconnect();
63
+ } catch (error) {
64
+ throw error;
65
+ }
66
+ }
52
67
  }
@@ -1,6 +1,6 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "include": ["**/*.ts"],
4
- "exclude": ["node_modules", "__tests__", "dist", "**/*spec.ts"]
5
- }
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "include": ["**/*.ts"],
4
+ "exclude": ["node_modules", "__tests__", "dist", "**/*spec.ts"]
5
+ }
6
6
 
package/tsconfig.json CHANGED
@@ -1,23 +1,23 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "declaration": true,
5
- "removeComments": true,
6
- "emitDecoratorMetadata": true,
7
- "experimentalDecorators": true,
8
- "allowSyntheticDefaultImports": true,
9
- "moduleResolution": "node",
10
- "target": "es6",
11
- "sourceMap": true,
12
- "outDir": "./dist",
13
- "baseUrl": "./src",
14
- "rootDir": "./",
15
- "incremental": true,
16
- "skipLibCheck": true,
17
- "noImplicitAny": false,
18
- "strictBindCallApply": false,
19
- "forceConsistentCasingInFileNames": false,
20
- "noFallthroughCasesInSwitch": false,
21
- "strictNullChecks": false,
22
- },
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "declaration": true,
5
+ "removeComments": true,
6
+ "emitDecoratorMetadata": true,
7
+ "experimentalDecorators": true,
8
+ "allowSyntheticDefaultImports": true,
9
+ "moduleResolution": "node",
10
+ "target": "es6",
11
+ "sourceMap": true,
12
+ "outDir": "./dist",
13
+ "baseUrl": "./src",
14
+ "rootDir": "./",
15
+ "incremental": true,
16
+ "skipLibCheck": true,
17
+ "noImplicitAny": false,
18
+ "strictBindCallApply": false,
19
+ "forceConsistentCasingInFileNames": false,
20
+ "noFallthroughCasesInSwitch": false,
21
+ "strictNullChecks": false,
22
+ },
23
23
  }