@tomei/sso 0.33.7 → 0.34.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) 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 +27 -0
  109. package/dist/src/components/group/group.js +438 -4
  110. package/dist/src/components/group/group.js.map +1 -1
  111. package/dist/src/components/group-object-privilege/group-object-privilege.d.ts +3 -0
  112. package/dist/src/components/group-object-privilege/group-object-privilege.js +108 -0
  113. package/dist/src/components/group-object-privilege/group-object-privilege.js.map +1 -1
  114. package/dist/src/components/group-object-privilege/group-object-privilege.repository.d.ts +1 -0
  115. package/dist/src/components/group-object-privilege/group-object-privilege.repository.js +22 -0
  116. package/dist/src/components/group-object-privilege/group-object-privilege.repository.js.map +1 -1
  117. package/dist/src/components/group-privilege/group-privilege.d.ts +2 -0
  118. package/dist/src/components/group-privilege/group-privilege.js +10 -0
  119. package/dist/src/components/group-privilege/group-privilege.js.map +1 -1
  120. package/dist/src/components/group-privilege/group-privilege.repository.d.ts +1 -0
  121. package/dist/src/components/group-privilege/group-privilege.repository.js +22 -0
  122. package/dist/src/components/group-privilege/group-privilege.repository.js.map +1 -1
  123. package/dist/src/components/login-user/login-user.d.ts +1 -1
  124. package/dist/src/components/system-privilege/system-privilege.d.ts +7 -0
  125. package/dist/src/components/system-privilege/system-privilege.js +54 -1
  126. package/dist/src/components/system-privilege/system-privilege.js.map +1 -1
  127. package/dist/src/interfaces/group-object-privilege.interface.d.ts +1 -1
  128. package/dist/src/interfaces/group-privilege.interface.d.ts +1 -1
  129. package/dist/src/interfaces/system-privilege-search.interface.d.ts +5 -0
  130. package/dist/src/interfaces/system-privilege-search.interface.js +3 -0
  131. package/dist/src/interfaces/system-privilege-search.interface.js.map +1 -0
  132. package/dist/src/models/group-object-privilege.entity.js +1 -0
  133. package/dist/src/models/group-object-privilege.entity.js.map +1 -1
  134. package/dist/src/models/group-privilege.entity.js +1 -0
  135. package/dist/src/models/group-privilege.entity.js.map +1 -1
  136. package/dist/src/models/group-reporting-user.entity.js +1 -0
  137. package/dist/src/models/group-reporting-user.entity.js.map +1 -1
  138. package/dist/src/models/user-object-privilege.entity.js +1 -0
  139. package/dist/src/models/user-object-privilege.entity.js.map +1 -1
  140. package/dist/src/models/user-privilege.entity.js +1 -0
  141. package/dist/src/models/user-privilege.entity.js.map +1 -1
  142. package/dist/src/models/user-system-access.entity.js +1 -0
  143. package/dist/src/models/user-system-access.entity.js.map +1 -1
  144. package/dist/tsconfig.tsbuildinfo +1 -1
  145. package/jest.config.js +14 -14
  146. package/migrations/20240314080602-create-user-table.js +108 -108
  147. package/migrations/20240314080603-create-user-group-table.js +85 -85
  148. package/migrations/20240314080604-create-user-user-group-table.js +55 -55
  149. package/migrations/20240314080605-create-login-history-table.js +53 -53
  150. package/migrations/20240527064925-create-system-table.js +78 -78
  151. package/migrations/20240527064926-create-system-privilege-table.js +67 -67
  152. package/migrations/20240527065342-create-group-table.js +89 -89
  153. package/migrations/20240527065633-create-group-reporting-user-table.js +76 -76
  154. package/migrations/20240528011551-create-group-system-access-table.js +72 -72
  155. package/migrations/20240528023018-user-system-access-table.js +75 -75
  156. package/migrations/20240528032229-user-privilege-table.js +75 -75
  157. package/migrations/20240528063003-create-group-privilege-table.js +75 -75
  158. package/migrations/20240528063051-create-group-object-privilege-table.js +84 -84
  159. package/migrations/20240528063107-create-user-object-privilege-table.js +83 -83
  160. package/package.json +89 -89
  161. package/sampledotenv +7 -7
  162. package/sonar-project.properties +22 -22
  163. package/src/components/group/group.ts +742 -4
  164. package/src/components/group-object-privilege/group-object-privilege.repository.ts +14 -0
  165. package/src/components/group-object-privilege/group-object-privilege.ts +196 -0
  166. package/src/components/group-privilege/group-privilege.repository.ts +11 -0
  167. package/src/components/group-privilege/group-privilege.ts +11 -0
  168. package/src/components/system-privilege/system-privilege.ts +89 -2
  169. package/src/interfaces/group-object-privilege.interface.ts +1 -1
  170. package/src/interfaces/group-privilege.interface.ts +1 -1
  171. package/src/interfaces/system-privilege-search.interface.ts +5 -0
  172. package/src/models/group-object-privilege.entity.ts +1 -0
  173. package/src/models/group-privilege.entity.ts +1 -0
  174. package/src/models/group-reporting-user.entity.ts +1 -0
  175. package/src/models/user-object-privilege.entity.ts +1 -0
  176. package/src/models/user-privilege.entity.ts +1 -0
  177. package/src/models/user-system-access.entity.ts +1 -0
  178. package/tsconfig.build.json +5 -5
  179. package/tsconfig.json +22 -22
@@ -11,6 +11,13 @@ import { GroupSystemAccessRepository } from '../group-system-access/group-system
11
11
  import SystemModel from '../../models/system.entity';
12
12
  import { GroupSystemAccess } from '../group-system-access';
13
13
  import { RedisService } from '../../redis-client/redis.service';
14
+ import SystemPrivilegeModel from '../../models/system-privilege.entity';
15
+ import { GroupPrivilegeRepository } from '../group-privilege/group-privilege.repository';
16
+ import { SystemPrivilege } from '../system-privilege/system-privilege';
17
+ import GroupPrivilegeModel from '../../models/group-privilege.entity';
18
+ import { GroupObjectPrivilegeRepository } from '../group-object-privilege/group-object-privilege.repository';
19
+ import { GroupObjectPrivilege } from '../group-object-privilege/group-object-privilege';
20
+ import { GroupPrivilege } from '../group-privilege/group-privilege';
14
21
 
15
22
  export class Group extends ObjectBase {
16
23
  ObjectId: string;
@@ -32,6 +39,9 @@ export class Group extends ObjectBase {
32
39
  private _UpdatedAt: Date;
33
40
  private static _Repo = new GroupRepository();
34
41
  private static _GroupSystemAccessRepo = new GroupSystemAccessRepository();
42
+ private static _GroupPrivilegeRepo = new GroupPrivilegeRepository();
43
+ private static _GroupObjectPrivilegeRepo =
44
+ new GroupObjectPrivilegeRepository();
35
45
  private static _RedisService: RedisService;
36
46
  get GroupCode(): string {
37
47
  return this.ObjectId;
@@ -567,7 +577,7 @@ export class Group extends ObjectBase {
567
577
 
568
578
  try {
569
579
  const group = await Group.init(dbTransaction, GroupCode);
570
- if (group.InheritParentSystemAccessYN !== 'Y' && !group.ParentGroupCode) {
580
+ if (group.InheritParentSystemAccessYN !== 'Y' || !group.ParentGroupCode) {
571
581
  return [];
572
582
  } else {
573
583
  const parentGroup = await Group.init(
@@ -608,14 +618,14 @@ export class Group extends ObjectBase {
608
618
 
609
619
  try {
610
620
  if (SystemCodes.length > 0) {
611
- for (let i = 0; i < SystemCodes.length; i++) {
621
+ for (const element of SystemCodes) {
612
622
  const CurrentGroupSystemAccess = await Group.getSystemAccesses(
613
623
  loginUser,
614
624
  dbTransaction,
615
625
  GroupCode,
616
626
  1,
617
627
  Number.MAX_SAFE_INTEGER,
618
- { SystemCode: SystemCodes[i] },
628
+ { SystemCode: element },
619
629
  );
620
630
 
621
631
  if (CurrentGroupSystemAccess?.count > 0) {
@@ -629,7 +639,7 @@ export class Group extends ObjectBase {
629
639
  const groupSystemAccess = await GroupSystemAccess.init(dbTransaction);
630
640
  groupSystemAccess.createId();
631
641
  groupSystemAccess.GroupCode = GroupCode;
632
- groupSystemAccess.SystemCode = SystemCodes[i];
642
+ groupSystemAccess.SystemCode = element;
633
643
  groupSystemAccess.Status = 'Active';
634
644
  groupSystemAccess.CreatedById = +loginUser.ObjectId;
635
645
  groupSystemAccess.CreatedAt = new Date();
@@ -744,4 +754,732 @@ export class Group extends ObjectBase {
744
754
  throw error;
745
755
  }
746
756
  }
757
+
758
+ public static async getSystemPrivileges(
759
+ loginUser: LoginUser,
760
+ dbTransaction: any,
761
+ GroupCode: string,
762
+ search?: {
763
+ SystemCode?: string;
764
+ Status?: string;
765
+ },
766
+ ) {
767
+ try {
768
+ //Part 1: Privilege Checking
769
+ const systemCode =
770
+ ApplicationConfig.getComponentConfigValue('system-code');
771
+ const isPrivileged = await loginUser.checkPrivileges(
772
+ systemCode,
773
+ 'GROUP_PRIVILEGE_VIEW',
774
+ );
775
+
776
+ if (!isPrivileged) {
777
+ throw new ClassError(
778
+ 'Group',
779
+ 'GroupErrMsg11',
780
+ 'You do not have the privilege to view group privileges',
781
+ );
782
+ }
783
+
784
+ //Set group to instantiation of existing Group
785
+ await Group.init(dbTransaction, GroupCode);
786
+
787
+ //Part 3: Retrieve Group Own Privilege
788
+ //Retrieve group data and it's privileged by calling Group._Repo.findAll
789
+ let where: any = {
790
+ GroupCode,
791
+ };
792
+
793
+ let systemWhere: any = {};
794
+
795
+ if (search) {
796
+ if (search.Status) {
797
+ where = {
798
+ ...where,
799
+ Status: search.Status,
800
+ };
801
+ }
802
+
803
+ if (search.SystemCode) {
804
+ systemWhere = {
805
+ SystemCode: {
806
+ [Op.substring]: search.SystemCode,
807
+ },
808
+ };
809
+ }
810
+ }
811
+
812
+ const groupOwnPrivileges = await Group._GroupPrivilegeRepo.findAll({
813
+ where,
814
+ include: [
815
+ {
816
+ model: SystemPrivilegeModel,
817
+ where: systemWhere,
818
+ },
819
+ ],
820
+ transaction: dbTransaction,
821
+ });
822
+
823
+ //Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
824
+ const privileges: SystemPrivilege[] = [];
825
+
826
+ for (const groupPrivilege of groupOwnPrivileges) {
827
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
828
+ systemPrivilege.setAttributes(
829
+ groupPrivilege.Privilege.get({ plain: true }),
830
+ );
831
+ privileges.push(systemPrivilege);
832
+ }
833
+
834
+ return privileges;
835
+ } catch (error) {
836
+ throw error;
837
+ }
838
+ }
839
+
840
+ public static async getInheritedSystemPrivileges(
841
+ dbTransaction: any,
842
+ GroupCode: string,
843
+ search?: {
844
+ SystemCode?: string;
845
+ Status?: string;
846
+ PrivilegeCode?: string;
847
+ },
848
+ ): Promise<SystemPrivilege[]> {
849
+ try {
850
+ //Retrieve group data and it's privileges by calling Group._Repo.findAll
851
+ const where: any = {
852
+ GroupCode,
853
+ };
854
+
855
+ let groupPrivilegeWhere: any = {};
856
+ let systemPrivilegeWhere: any = {};
857
+
858
+ if (search) {
859
+ if (search.Status) {
860
+ groupPrivilegeWhere = {
861
+ Status: search.Status,
862
+ };
863
+ }
864
+
865
+ if (search.SystemCode) {
866
+ systemPrivilegeWhere = {
867
+ SystemCode: {
868
+ [Op.substring]: search.SystemCode,
869
+ },
870
+ };
871
+ }
872
+ }
873
+ const group = await Group._Repo.findOne({
874
+ where: where,
875
+ include: [
876
+ {
877
+ model: GroupPrivilegeModel,
878
+ where: groupPrivilegeWhere,
879
+ separate: true,
880
+ include: [
881
+ {
882
+ model: SystemPrivilegeModel,
883
+ where: systemPrivilegeWhere,
884
+ },
885
+ ],
886
+ },
887
+ ],
888
+ transaction: dbTransaction,
889
+ });
890
+
891
+ //Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
892
+ const objectWhere: any = {
893
+ GroupCode,
894
+ };
895
+ const systemWhere: any = {};
896
+ if (search) {
897
+ Object.entries(search).forEach(([key, value]) => {
898
+ if (key === 'SystemCode') {
899
+ systemWhere[key] = {
900
+ [Op.substring]: value,
901
+ };
902
+ } else {
903
+ objectWhere[key] = {
904
+ [Op.substring]: value,
905
+ };
906
+ }
907
+ });
908
+ }
909
+ const groupObjectPrivileges =
910
+ await Group._GroupObjectPrivilegeRepo.findAll({
911
+ where: objectWhere,
912
+ include: [
913
+ {
914
+ model: SystemPrivilegeModel,
915
+ where: systemWhere,
916
+ },
917
+ ],
918
+ transaction: dbTransaction,
919
+ });
920
+
921
+ //Map to SystemPrivilege object
922
+ let privileges: SystemPrivilege[] = [];
923
+ for (const groupPrivilege of group.GroupPrivileges) {
924
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
925
+ systemPrivilege.setAttributes(
926
+ groupPrivilege.Privilege.get({ plain: true }),
927
+ );
928
+ privileges.push(systemPrivilege);
929
+ }
930
+
931
+ for (const groupObjectPrivilege of groupObjectPrivileges) {
932
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
933
+ systemPrivilege.setAttributes(
934
+ groupObjectPrivilege.Privilege.get({ plain: true }),
935
+ );
936
+ privileges.push(systemPrivilege);
937
+ }
938
+
939
+ //Part 2: Retrieve Privileges Inherited from Parent Group
940
+ //if group data retrieved from 1.1 have InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. Call this method again
941
+ if (group.InheritParentPrivilegeYN === 'Y' && group.ParentGroupCode) {
942
+ const inheritedPrivileges = await Group.getInheritedSystemPrivileges(
943
+ dbTransaction,
944
+ group.ParentGroupCode,
945
+ search,
946
+ );
947
+ privileges = privileges.concat(inheritedPrivileges);
948
+ }
949
+
950
+ //format to make sure no duplicate
951
+ const uniquePrivileges = Array.from(
952
+ new Set(privileges.map((a) => a.PrivilegeCode)),
953
+ ).map((PrivilegeCode) => {
954
+ return privileges.find((a) => a.PrivilegeCode === PrivilegeCode);
955
+ });
956
+
957
+ return uniquePrivileges;
958
+ } catch (error) {
959
+ throw error;
960
+ }
961
+ }
962
+
963
+ public static async getParentSystemPrivileges(
964
+ loginUser: LoginUser,
965
+ dbTransaction: any,
966
+ GroupCode: string,
967
+ search?: {
968
+ SystemCode?: string;
969
+ Status?: string;
970
+ PrivilegeCode?: string;
971
+ },
972
+ ): Promise<SystemPrivilege[]> {
973
+ try {
974
+ //Part 1: Privilege Checking
975
+ const systemCode =
976
+ ApplicationConfig.getComponentConfigValue('system-code');
977
+ const isPrivileged = await loginUser.checkPrivileges(
978
+ systemCode,
979
+ 'GROUP_PRIVILEGE_VIEW',
980
+ );
981
+
982
+ if (!isPrivileged) {
983
+ throw new ClassError(
984
+ 'Group',
985
+ 'GroupErrMsg11',
986
+ 'You do not have the privilege to view group privileges',
987
+ );
988
+ }
989
+
990
+ //Part 2: Validation
991
+ //Set group to instantiation of existing Group
992
+ const group = await Group.init(dbTransaction, GroupCode);
993
+ //Check if group.InheritParentPrivilegeYN == "Y" and ParentGroupCode value is not empty. if no then return an empty array
994
+ if (group.InheritParentPrivilegeYN !== 'Y' || !group.ParentGroupCode) {
995
+ return [];
996
+ }
997
+
998
+ //Part 3: Retrieve Group Own Privilege
999
+ //Retrieve group data and it's privileged by calling Group.getIheritedSystemPrivileges
1000
+ const privileges = await Group.getInheritedSystemPrivileges(
1001
+ dbTransaction,
1002
+ group.ParentGroupCode,
1003
+ search,
1004
+ );
1005
+
1006
+ return privileges;
1007
+ } catch (error) {
1008
+ throw error;
1009
+ }
1010
+ }
1011
+
1012
+ public static async assignGroupObjectPrivilege(
1013
+ loginUser: LoginUser,
1014
+ dbTransaction: any,
1015
+ GroupCode: string,
1016
+ GroupObjectPrivileges: GroupObjectPrivilege[],
1017
+ ): Promise<string> {
1018
+ try {
1019
+ //Part 1: Privilege Checking
1020
+ const systemCode =
1021
+ ApplicationConfig.getComponentConfigValue('system-code');
1022
+ const isPrivileged = await loginUser.checkPrivileges(
1023
+ systemCode,
1024
+ 'GROUP_OBJECT_PRIVILEGE_ASSIGN',
1025
+ );
1026
+
1027
+ if (!isPrivileged) {
1028
+ throw new ClassError(
1029
+ 'Group',
1030
+ 'GroupErrMsg12',
1031
+ 'You do not have the privilege to assign group object privilege',
1032
+ );
1033
+ }
1034
+
1035
+ //Part 2: Validation
1036
+ //Initialise group with group init
1037
+ const group = await Group.init(dbTransaction, GroupCode);
1038
+ //Retrieve all group system access by calling Group.getSystemAccesses
1039
+ const groupSystemAccesses = await Group.getSystemAccesses(
1040
+ loginUser,
1041
+ dbTransaction,
1042
+ GroupCode,
1043
+ 1,
1044
+ Number.MAX_SAFE_INTEGER,
1045
+ {},
1046
+ );
1047
+
1048
+ //If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist, initialise parent group
1049
+ let parentGroupSystemAccesses: any = {};
1050
+ if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1051
+ //Retrieve all parent group system access by calling Group.getSystemAccesses
1052
+ parentGroupSystemAccesses = await Group.getSystemAccesses(
1053
+ loginUser,
1054
+ dbTransaction,
1055
+ group.ParentGroupCode,
1056
+ 1,
1057
+ Number.MAX_SAFE_INTEGER,
1058
+ {},
1059
+ );
1060
+ }
1061
+
1062
+ // For each Params.GroupObjectPrivileges.
1063
+ for (const groupObjectPrivilege of GroupObjectPrivileges) {
1064
+ //Initialise existing System privilege
1065
+ const systemPrivilege = await SystemPrivilege.init(
1066
+ dbTransaction,
1067
+ groupObjectPrivilege.PrivilegeCode,
1068
+ );
1069
+ //Check whether the system codes used by that privilege is exist inside the group system access
1070
+ const combinedSystemAccesses = {
1071
+ ...groupSystemAccesses.rows,
1072
+ ...parentGroupSystemAccesses.rows,
1073
+ };
1074
+ const systemAccess = combinedSystemAccesses.find(
1075
+ (systemAccess) =>
1076
+ systemAccess.SystemCode === systemPrivilege.SystemCode,
1077
+ );
1078
+ if (!systemAccess) {
1079
+ throw new ClassError(
1080
+ 'Group',
1081
+ 'GroupErrMsg13',
1082
+ 'Failed to assign privilege ' +
1083
+ groupObjectPrivilege.PrivilegeCode +
1084
+ ' due to non-existent system access.',
1085
+ );
1086
+ }
1087
+
1088
+ //Check whether the group object privilege already exist by using Group._GroupObjectPrivilegesRepo.findOne
1089
+ const groupObjectPrivilegeData =
1090
+ await Group._GroupObjectPrivilegeRepo.findOne({
1091
+ where: {
1092
+ GroupCode,
1093
+ PrivilegeCode: groupObjectPrivilege.PrivilegeCode,
1094
+ ObjectId: groupObjectPrivilege.ObjectId,
1095
+ ObjectType: groupObjectPrivilege.ObjectType,
1096
+ },
1097
+ transaction: dbTransaction,
1098
+ });
1099
+ //If GroupObjectPrivilege record exist. Skip this loop and proceed to the next privilege code
1100
+ if (groupObjectPrivilegeData) {
1101
+ continue;
1102
+ } else {
1103
+ //Call GroupObjectPrivilege.create
1104
+ await GroupObjectPrivilege.create(
1105
+ loginUser,
1106
+ dbTransaction,
1107
+ groupObjectPrivilege,
1108
+ );
1109
+ }
1110
+ }
1111
+
1112
+ return 'Successfully added.';
1113
+ } catch (error) {
1114
+ throw error;
1115
+ }
1116
+ }
1117
+
1118
+ public static async getGroubObjectPrivileges(
1119
+ loginUser: LoginUser,
1120
+ dbTransaction: any,
1121
+ GroupCode: string,
1122
+ search?: {
1123
+ PrivilegeCode?: string;
1124
+ ObjectType?: string;
1125
+ ObjectId?: string;
1126
+ SystemCode?: string;
1127
+ },
1128
+ ): Promise<SystemPrivilege[]> {
1129
+ try {
1130
+ // Part 1: Privilege Checking
1131
+ const systemCode =
1132
+ ApplicationConfig.getComponentConfigValue('system-code');
1133
+ const isPrivileged = await loginUser.checkPrivileges(
1134
+ systemCode,
1135
+ 'GROUP_PRIVILEGE_VIEW',
1136
+ );
1137
+
1138
+ if (!isPrivileged) {
1139
+ throw new ClassError(
1140
+ 'Group',
1141
+ 'GroupErrMsg11',
1142
+ 'You do not have the privilege to view group privileges',
1143
+ );
1144
+ }
1145
+
1146
+ // Part 2: Validation
1147
+ // Set group to instantiation of existing Group
1148
+ await Group.init(dbTransaction, GroupCode);
1149
+
1150
+ // Part 3: Retrieve Group Own Privilege
1151
+ // Retrieve group object privileges by calling LoginUser._GroupObjectPrivilegeRepo.findAll
1152
+ const where: any = {
1153
+ GroupCode,
1154
+ };
1155
+
1156
+ const systemWhere: any = {};
1157
+
1158
+ if (search) {
1159
+ Object.entries(search).forEach(([key, value]) => {
1160
+ if (key === 'SystemCode') {
1161
+ systemWhere[key] = {
1162
+ [Op.substring]: value,
1163
+ };
1164
+ } else {
1165
+ where[key] = {
1166
+ [Op.substring]: value,
1167
+ };
1168
+ }
1169
+ });
1170
+ }
1171
+
1172
+ const groupObjectPrivileges =
1173
+ await Group._GroupObjectPrivilegeRepo.findAll({
1174
+ where,
1175
+ include: [
1176
+ {
1177
+ model: SystemPrivilegeModel,
1178
+ where: systemWhere,
1179
+ },
1180
+ ],
1181
+ transaction: dbTransaction,
1182
+ });
1183
+ // Create variable called privileges and Map the SystemPrivilege data retrieved from 3.1 into SystemPrivilege object and push it to the privileges variables
1184
+ const privileges: SystemPrivilege[] = [];
1185
+ for (const groupObjectPrivilege of groupObjectPrivileges) {
1186
+ const systemPrivilege = await SystemPrivilege.init(dbTransaction);
1187
+ systemPrivilege.setAttributes(
1188
+ groupObjectPrivilege.Privilege.get({ plain: true }),
1189
+ );
1190
+ privileges.push(systemPrivilege);
1191
+ }
1192
+
1193
+ //Remove duplicate
1194
+ const uniquePrivileges = Array.from(
1195
+ new Set(privileges.map((a) => a.PrivilegeCode)),
1196
+ ).map((PrivilegeCode) => {
1197
+ return privileges.find((a) => a.PrivilegeCode === PrivilegeCode);
1198
+ });
1199
+
1200
+ // Create the result based on the spec on return then returns it.
1201
+ return uniquePrivileges;
1202
+ } catch (error) {
1203
+ throw error;
1204
+ }
1205
+ }
1206
+
1207
+ public static async assignGroupPrivileges(
1208
+ loginUser: LoginUser,
1209
+ dbTransaction: any,
1210
+ GroupCode: string,
1211
+ PrivilegeCodes: string[],
1212
+ ) {
1213
+ try {
1214
+ // Part 1: Privilege Checking
1215
+ const systemCode =
1216
+ ApplicationConfig.getComponentConfigValue('system-code');
1217
+ const isPrivileged = await loginUser.checkPrivileges(
1218
+ systemCode,
1219
+ 'GROUP_PRIVILEGE_ASSIGN',
1220
+ );
1221
+
1222
+ if (!isPrivileged) {
1223
+ throw new ClassError(
1224
+ 'Group',
1225
+ 'GroupErrMsg06',
1226
+ 'You do not have the privilege to assign group privileges',
1227
+ );
1228
+ }
1229
+
1230
+ // Part 2: Validation, Create and Record Activity
1231
+ // Initialise group with group init
1232
+
1233
+ const group = await Group.init(dbTransaction, GroupCode);
1234
+
1235
+ // Retrieve all group system access by calling Group.getSystemAccess
1236
+ const groupSystemAccesses = await Group.getSystemAccesses(
1237
+ loginUser,
1238
+ dbTransaction,
1239
+ GroupCode,
1240
+ 1,
1241
+ Number.MAX_SAFE_INTEGER,
1242
+ {},
1243
+ );
1244
+
1245
+ // If Group.InheritParentSystemAccess == "Y" and Group.ParentGroupCode exist
1246
+ let parentGroupSystemAccesses: any = {};
1247
+ if (group.InheritParentSystemAccessYN === 'Y' && group.ParentGroupCode) {
1248
+ // Retrieve all parent group system access by calling Group.getSystemAccess
1249
+ parentGroupSystemAccesses = await Group.getSystemAccesses(
1250
+ loginUser,
1251
+ dbTransaction,
1252
+ group.ParentGroupCode,
1253
+ 1,
1254
+ Number.MAX_SAFE_INTEGER,
1255
+ {},
1256
+ );
1257
+ }
1258
+
1259
+ // For each Params.PrivilegesCodes.
1260
+ for (const PrivilegeCode of PrivilegeCodes) {
1261
+ // Initialise existing System privilege by calling SystemPrivilege.init
1262
+ const systemPrivilege = await SystemPrivilege.init(
1263
+ dbTransaction,
1264
+ PrivilegeCode,
1265
+ );
1266
+ //Check whether the system codes used by that privilege is exist inside the group system access retrieved from step 2.2 & 2.4. If system code does not exist in group system access, throw a new ClassError by passing:
1267
+ // Classname: "Group"
1268
+ // MessageCode: "GroupErrMsg0X"
1269
+ // Message: "Failed to assign privilege <PrivilegeCode> due to non-existent system access."
1270
+ const combinedSystemAccesses = [
1271
+ ...groupSystemAccesses.rows,
1272
+ ...parentGroupSystemAccesses.rows,
1273
+ ];
1274
+ const systemAccess = combinedSystemAccesses.find(
1275
+ (systemAccess) =>
1276
+ systemAccess.SystemCode === systemPrivilege.SystemCode,
1277
+ );
1278
+ if (!systemAccess) {
1279
+ throw new ClassError(
1280
+ 'Group',
1281
+ 'GroupErrMsg13',
1282
+ 'Failed to assign privilege ' +
1283
+ PrivilegeCode +
1284
+ ' due to non-existent system access.',
1285
+ );
1286
+ }
1287
+
1288
+ //Check whether the group privilege exist by using Group._GroupPrivilegesRepo.findOne
1289
+ const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1290
+ where: {
1291
+ GroupCode,
1292
+ PrivilegeCode,
1293
+ },
1294
+ transaction: dbTransaction,
1295
+ });
1296
+
1297
+ //If GroupPrivilege record exist and status is "Active". Skip this loop and proceed to the next privilege code
1298
+ if (groupPrivilege && groupPrivilege.Status === 'Active') {
1299
+ continue;
1300
+ }
1301
+
1302
+ let entityValueBefore = {};
1303
+ let entityValueAfter = {};
1304
+ let action = ActionEnum.ADD;
1305
+ let description = 'Create Group Privilege';
1306
+ let entityId = null;
1307
+ //If GroupPrivilege record exist and status is not "Active" do the following:
1308
+ if (groupPrivilege && groupPrivilege.Status !== 'Active') {
1309
+ //Set this GroupPrivilege entity as EntityValueBefore
1310
+ entityValueBefore = {
1311
+ GroupCode: groupPrivilege.GroupCode,
1312
+ PrivilegeCode: groupPrivilege.PrivilegeCode,
1313
+ Status: groupPrivilege.Status,
1314
+ CreatedById: groupPrivilege.CreatedById,
1315
+ CreatedAt: groupPrivilege.CreatedAt,
1316
+ UpdatedById: groupPrivilege.UpdatedById,
1317
+ UpdatedAt: groupPrivilege.UpdatedAt,
1318
+ };
1319
+
1320
+ //Update the status to active using Group._GroupPrivilegesRepo.Update.
1321
+ const updatedPayload = {
1322
+ Status: 'Active',
1323
+ UpdatedById: loginUser.UserId,
1324
+ UpdatedAt: new Date(),
1325
+ };
1326
+ await Group._GroupPrivilegeRepo.update(updatedPayload, {
1327
+ where: {
1328
+ GroupCode,
1329
+ PrivilegeCode,
1330
+ },
1331
+ transaction: dbTransaction,
1332
+ });
1333
+
1334
+ //Set updated GroupPrivilege as EntityValueAfter
1335
+ entityValueAfter = {
1336
+ GroupCode: groupPrivilege.GroupCode,
1337
+ PrivilegeCode: groupPrivilege.PrivilegeCode,
1338
+ Status: updatedPayload.Status,
1339
+ CreatedById: groupPrivilege.CreatedById,
1340
+ CreatedAt: groupPrivilege.CreatedAt,
1341
+ UpdatedById: updatedPayload.UpdatedById,
1342
+ UpdatedAt: updatedPayload.UpdatedAt,
1343
+ };
1344
+
1345
+ //Instantiate new activity from Activity class
1346
+ action = ActionEnum.UPDATE;
1347
+ description = 'Update Group Privilege';
1348
+ entityId = groupPrivilege.GroupPrivilegeId;
1349
+ } else {
1350
+ //If GroupPrivilege record does not exist, do the following:
1351
+ //Initialise empty GroupPrivilege.
1352
+ const newGroupPrivilege = await GroupPrivilege.init(dbTransaction);
1353
+ //Set the attributes
1354
+ newGroupPrivilege.setAttributes({
1355
+ GroupCode,
1356
+ PrivilegeCode,
1357
+ Status: 'Active',
1358
+ CreatedById: loginUser.UserId,
1359
+ CreatedAt: new Date(),
1360
+ UpdatedById: loginUser.UserId,
1361
+ UpdatedAt: new Date(),
1362
+ });
1363
+
1364
+ // Set EntityValueAfter to above instance.
1365
+ entityValueAfter = {
1366
+ GroupCode: newGroupPrivilege.GroupCode,
1367
+ PrivilegeCode: newGroupPrivilege.PrivilegeCode,
1368
+ Status: newGroupPrivilege.Status,
1369
+ CreatedById: newGroupPrivilege.CreatedById,
1370
+ CreatedAt: newGroupPrivilege.CreatedAt,
1371
+ UpdatedById: newGroupPrivilege.UpdatedById,
1372
+ UpdatedAt: newGroupPrivilege.UpdatedAt,
1373
+ };
1374
+
1375
+ //Call Group._GroupPrivilegesRepo.create
1376
+ const groupPrivilege = await Group._GroupPrivilegeRepo.create(
1377
+ entityValueAfter,
1378
+ {
1379
+ transaction: dbTransaction,
1380
+ },
1381
+ );
1382
+ action = ActionEnum.ADD;
1383
+ description = 'Create Group Privilege';
1384
+ entityId = groupPrivilege.GroupPrivilegeId;
1385
+ }
1386
+
1387
+ //Instantiate new activity from Activity class, call createId() method, then set:
1388
+ const activity = new Activity();
1389
+ activity.ActivityId = activity.createId();
1390
+ activity.Action = action;
1391
+ activity.Description = description;
1392
+ activity.EntityType = 'GroupPrivilege';
1393
+ activity.EntityId = entityId;
1394
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1395
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
1396
+
1397
+ //Call new activity create method
1398
+ await activity.create(loginUser.ObjectId, dbTransaction);
1399
+ }
1400
+
1401
+ return 'Successfully added.';
1402
+ } catch (error) {
1403
+ throw error;
1404
+ }
1405
+ }
1406
+
1407
+ public static async deleteGroupPrivilege(
1408
+ loginUser: LoginUser,
1409
+ dbTransaction: any,
1410
+ GroupCode: string,
1411
+ PrivilegeCodes: string[],
1412
+ ) {
1413
+ try {
1414
+ // Part 1: Privilege Checking
1415
+ const systemCode =
1416
+ ApplicationConfig.getComponentConfigValue('system-code');
1417
+ const isPrivileged = await loginUser.checkPrivileges(
1418
+ systemCode,
1419
+ 'GROUP_PRIVILEGE_DELETE',
1420
+ );
1421
+
1422
+ if (!isPrivileged) {
1423
+ throw new ClassError(
1424
+ 'Group',
1425
+ 'GroupErrMsg06',
1426
+ 'You do not have the privilege to delete group privileges',
1427
+ );
1428
+ }
1429
+
1430
+ // Part 2: Validation, Create and Record Activity
1431
+ // For each Params.PrivilegesCodes.
1432
+ for (const PrivilegeCode of PrivilegeCodes) {
1433
+ //Check whether the record exist in database by calling Group._GroupPrivilegeRepo.findOne
1434
+ const groupPrivilege = await Group._GroupPrivilegeRepo.findOne({
1435
+ where: {
1436
+ GroupCode,
1437
+ PrivilegeCode,
1438
+ },
1439
+ transaction: dbTransaction,
1440
+ });
1441
+
1442
+ //If the record does not exist, throw a new ClassError
1443
+ if (!groupPrivilege) {
1444
+ throw new ClassError(
1445
+ 'Group',
1446
+ 'GroupErrMsg14',
1447
+ 'GroupPrivilege not found.',
1448
+ );
1449
+ }
1450
+
1451
+ //Set the EntityValueBefore to the GroupPrivilegesValue from step 1.c.
1452
+ const entityValueBefore = {
1453
+ GroupCode: groupPrivilege.GroupCode,
1454
+ PrivilegeCode: groupPrivilege.PrivilegeCode,
1455
+ Status: groupPrivilege.Status,
1456
+ CreatedById: groupPrivilege.CreatedById,
1457
+ CreatedAt: groupPrivilege.CreatedAt,
1458
+ UpdatedById: groupPrivilege.UpdatedById,
1459
+ UpdatedAt: groupPrivilege.UpdatedAt,
1460
+ };
1461
+
1462
+ //Call Group._GroupPrivilegeRepo.delete
1463
+ await Group._GroupPrivilegeRepo.delete(
1464
+ GroupCode,
1465
+ PrivilegeCode,
1466
+ dbTransaction,
1467
+ );
1468
+ // Instantiate new activity from Activity class, call createId() method, then set:
1469
+ const activity = new Activity();
1470
+ activity.ActivityId = activity.createId();
1471
+ activity.Action = ActionEnum.DELETE;
1472
+ activity.Description = 'DELETE Group Privilege';
1473
+ activity.EntityType = 'GroupPrivilege';
1474
+ activity.EntityId = groupPrivilege.GroupPrivilegeId.toString();
1475
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
1476
+ activity.EntityValueAfter = JSON.stringify({});
1477
+ //Call new activity create method
1478
+ await activity.create(loginUser.ObjectId, dbTransaction);
1479
+ }
1480
+ return 'Successfully deleted.';
1481
+ } catch (error) {
1482
+ throw error;
1483
+ }
1484
+ }
747
1485
  }