@tomei/sso 0.58.12 → 0.59.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.58.12",
3
+ "version": "0.59.0",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -22,6 +22,7 @@ import { User } from '../login-user/user';
22
22
  import GroupReportingUserModel from '../../models/group-reporting-user.entity';
23
23
  import GroupModel from '../../models/group.entity';
24
24
  import UserModel from '../../models/user.entity';
25
+ import { UserGroup } from 'components/user-group';
25
26
 
26
27
  export class Group extends TreeNodeBase<Group> {
27
28
  ObjectId: string;
@@ -2192,4 +2193,46 @@ export class Group extends TreeNodeBase<Group> {
2192
2193
  throw error;
2193
2194
  }
2194
2195
  }
2196
+
2197
+ public async unassignUser(
2198
+ UserId: number,
2199
+ loginUser: LoginUser,
2200
+ dbTransaction: Transaction,
2201
+ ) {
2202
+ try {
2203
+ const systemCode =
2204
+ ApplicationConfig.getComponentConfigValue('system-code');
2205
+ const isPrivileged = await loginUser.checkPrivileges(
2206
+ systemCode,
2207
+ 'GROUP_UPDATE',
2208
+ );
2209
+
2210
+ if (!isPrivileged) {
2211
+ throw new ClassError(
2212
+ 'Group',
2213
+ 'GroupErrMsg05',
2214
+ 'You do not have the privilege to update group',
2215
+ );
2216
+ }
2217
+
2218
+ const userGroup = await UserGroup.findOne(
2219
+ dbTransaction,
2220
+ loginUser,
2221
+ this.GroupCode,
2222
+ UserId,
2223
+ );
2224
+
2225
+ if (!userGroup) {
2226
+ throw new ClassError(
2227
+ 'Group',
2228
+ 'GroupErrMsg07',
2229
+ 'User is not assigned to this group',
2230
+ );
2231
+ }
2232
+
2233
+ await userGroup.delete(loginUser, dbTransaction);
2234
+ } catch (error) {
2235
+ throw error;
2236
+ }
2237
+ }
2195
2238
  }
@@ -8,4 +8,12 @@ export class UserGroupRepository
8
8
  constructor() {
9
9
  super(UserGroupModel);
10
10
  }
11
+
12
+ async delete(options: any) {
13
+ try {
14
+ return UserGroupModel.destroy(options);
15
+ } catch (error) {
16
+ throw error;
17
+ }
18
+ }
11
19
  }
@@ -647,51 +647,117 @@ export class UserGroup extends ObjectBase {
647
647
  }
648
648
  }
649
649
 
650
- public static isUserMemberOfGroup(
650
+ public static async isUserMemberOfGroup(
651
651
  dbTransaction: any,
652
652
  loginUser: LoginUser,
653
653
  UserId: number,
654
654
  GroupCode: string,
655
655
  ): Promise<boolean> {
656
- return new Promise(async (resolve, reject) => {
657
- try {
658
- // Part 1: Privilege Checking
659
- // Call loginUser.checkPrivileges() to ensure the user has permission to retrieve system access information.
660
- // SystemCode: Retrieve from app config.
661
- // PrivilegeCode: 'USER_GROUP_VIEW'.
662
- const systemCode =
663
- ApplicationConfig.getComponentConfigValue('system-code');
664
- const isPrivileged = await loginUser.checkPrivileges(
665
- systemCode,
666
- 'USER_GROUP_VIEW',
656
+ try {
657
+ // Part 1: Privilege Checking
658
+ // Call loginUser.checkPrivileges() to ensure the user has permission to retrieve system access information.
659
+ // SystemCode: Retrieve from app config.
660
+ // PrivilegeCode: 'USER_GROUP_VIEW'.
661
+ const systemCode =
662
+ ApplicationConfig.getComponentConfigValue('system-code');
663
+ const isPrivileged = await loginUser.checkPrivileges(
664
+ systemCode,
665
+ 'USER_GROUP_VIEW',
666
+ );
667
+ // If the privilege check fails, throw an error with a 403 Forbidden status.
668
+ if (!isPrivileged) {
669
+ throw new ClassError(
670
+ 'UserGroup',
671
+ 'UserGroupErrMsg0X',
672
+ 'User does not have privilege to view user group.',
673
+ 'isUserMemberOfGroup',
674
+ 403,
667
675
  );
668
- // If the privilege check fails, throw an error with a 403 Forbidden status.
669
- if (!isPrivileged) {
670
- throw new ClassError(
671
- 'UserGroup',
672
- 'UserGroupErrMsg0X',
673
- 'User does not have privilege to view user group.',
674
- 'isUserMemberOfGroup',
675
- 403,
676
- );
677
- }
678
- // Part 2: Retrieve User Group
679
- // Query the sso_UserGroup table to find the user group record with the given UserId and GroupCode.
680
- // If the record exists, return true; otherwise, return false.
681
- const userGroup = await UserGroup.findOne(
682
- dbTransaction,
683
- loginUser,
684
- GroupCode,
685
- UserId,
676
+ }
677
+ // Part 2: Retrieve User Group
678
+ // Query the sso_UserGroup table to find the user group record with the given UserId and GroupCode.
679
+ // If the record exists, return true; otherwise, return false.
680
+ const userGroup = await UserGroup.findOne(
681
+ dbTransaction,
682
+ loginUser,
683
+ GroupCode,
684
+ UserId,
685
+ );
686
+ return !!userGroup;
687
+ } catch (error) {
688
+ throw error;
689
+ }
690
+ }
691
+
692
+ public async delete(
693
+ loginUser: LoginUser,
694
+ dbTransaction: Transaction,
695
+ ): Promise<void> {
696
+ try {
697
+ // Part 1: Privilege Checking
698
+ // Call loginUser.checkPrivileges() to ensure the user has permission to delete user group records.
699
+ // SystemCode: Retrieve from app config.
700
+ // PrivilegeCode: 'USER_GROUP_DELETE'.
701
+ const systemCode =
702
+ ApplicationConfig.getComponentConfigValue('system-code');
703
+ const isPrivileged = await loginUser.checkPrivileges(
704
+ systemCode,
705
+ 'USER_GROUP_DELETE',
706
+ );
707
+ // If the privilege check fails, throw an error with a 403 Forbidden status.
708
+ if (!isPrivileged) {
709
+ throw new ClassError(
710
+ 'UserGroup',
711
+ 'UserGroupErrMsg0X',
712
+ 'User does not have privilege to delete user group.',
713
+ 'delete',
714
+ 403,
686
715
  );
687
- if (userGroup) {
688
- resolve(true);
689
- } else {
690
- resolve(false);
691
- }
692
- } catch (error) {
693
- reject(error);
694
716
  }
695
- });
717
+ // Part 2: Delete User Group
718
+ // Call the UserGroup._Repo.destroy() method to delete the user group record with the given UserGroupId.
719
+ // Pass the dbTransaction parameter to ensure the operation is part of the current transaction.
720
+ await UserGroup._Repository.delete({
721
+ where: {
722
+ UserGroupId: this.UserGroupId,
723
+ },
724
+ transaction: dbTransaction,
725
+ });
726
+ // Part 3: Record Activity History
727
+ // Initialize a variable entityValueBefore to store the current state of the record before the update.
728
+ const entityValueBefore = {
729
+ UserGroupId: this.UserGroupId,
730
+ UserId: this.UserId,
731
+ GroupCode: this.GroupCode,
732
+ Status: this.Status,
733
+ CreatedById: this._CreatedById,
734
+ CreatedAt: this._CreatedAt,
735
+ UpdatedById: this._UpdatedById,
736
+ UpdatedAt: this._UpdatedAt,
737
+ InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN,
738
+ InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN,
739
+ };
740
+ // Create an instance of the Activity class and set the following properties:
741
+ // ActivityId: Call activity.createId().
742
+ // Action: Set to ActionEnum.Delete.
743
+ // Description: Set to Delete User Group.
744
+ // EntityType: Set to UserGroup.
745
+ // EntityId: Use the ID of the deleted record.
746
+ // EntityValueBefore: Stringify entityValueBefore to capture the state before the delete.
747
+ // EntityValueAfter: Set to an empty string to indicate the record has been deleted.
748
+ const activity = new Activity();
749
+ activity.ActivityId = activity.createId();
750
+ activity.Action = ActionEnum.DELETE;
751
+ activity.Description = `Delete User Group ${this.UserGroupId}`;
752
+ activity.EntityType = 'UserGroup';
753
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
754
+ activity.EntityValueAfter = JSON.stringify({});
755
+ // Call the activity create method with the following parameters:
756
+ // dbTransaction
757
+ // userId: loginUser.UserId
758
+ await activity.create(loginUser.ObjectId, dbTransaction);
759
+ } catch (error) {
760
+ throw error;
761
+ }
696
762
  }
697
763
  }