@tomei/sso 0.58.1 → 0.58.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. package/dist/src/components/api-key/api-key.js +1 -0
  2. package/dist/src/components/api-key/api-key.js.map +1 -1
  3. package/dist/src/components/group/group.js +1 -1
  4. package/dist/src/components/group/group.js.map +1 -1
  5. package/dist/src/components/login-user/login-user.js +60 -0
  6. package/dist/src/components/login-user/login-user.js.map +1 -1
  7. package/dist/src/components/login-user/user.d.ts +2 -0
  8. package/dist/src/components/login-user/user.js +91 -12
  9. package/dist/src/components/login-user/user.js.map +1 -1
  10. package/dist/src/components/system/system.js +1 -1
  11. package/dist/src/components/system/system.js.map +1 -1
  12. package/dist/src/components/system-privilege/system-privilege.js +1 -0
  13. package/dist/src/components/system-privilege/system-privilege.js.map +1 -1
  14. package/dist/src/components/user-privilege/user-privilege.js +1 -0
  15. package/dist/src/components/user-privilege/user-privilege.js.map +1 -1
  16. package/dist/src/components/user-system-access/user-system-access.js +1 -0
  17. package/dist/src/components/user-system-access/user-system-access.js.map +1 -1
  18. package/dist/tsconfig.tsbuildinfo +1 -1
  19. package/package.json +1 -1
  20. package/src/components/api-key/api-key.ts +1 -0
  21. package/src/components/group/group.ts +1 -0
  22. package/src/components/login-user/login-user.ts +63 -0
  23. package/src/components/login-user/user.ts +141 -30
  24. package/src/components/system/system.ts +1 -0
  25. package/src/components/system-privilege/system-privilege.ts +1 -0
  26. package/src/components/user-privilege/user-privilege.ts +1 -0
  27. package/src/components/user-system-access/user-system-access.ts +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.58.1",
3
+ "version": "0.58.4",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -301,6 +301,7 @@ export class APIKey extends ObjectBase {
301
301
  offset,
302
302
  limit: pagination.limit,
303
303
  transaction: dbTransaction,
304
+ distinct: true,
304
305
  });
305
306
 
306
307
  // Return the list of API keys including:
@@ -239,6 +239,7 @@ export class Group extends TreeNodeBase<Group> {
239
239
  limit: row,
240
240
  offset: row * (page - 1),
241
241
  order: [['CreatedAt', 'DESC']],
242
+ distinct: true,
242
243
  };
243
244
  }
244
245
 
@@ -9,6 +9,9 @@ import { IUserAttr, IUserInfo } from './interfaces/user-info.interface';
9
9
  import Staff from '../../models/staff.entity';
10
10
  import UserModel from '../../models/user.entity';
11
11
  import { createHash, randomBytes } from 'crypto';
12
+ import { UserGroupRepository } from '../user-group/user-group.repository';
13
+ import GroupSystemAccessModel from '../../models/group-system-access.entity';
14
+ import SystemModel from '../../models/system.entity';
12
15
 
13
16
  export class LoginUser extends User implements ILoginUser {
14
17
  session = {
@@ -219,11 +222,71 @@ export class LoginUser extends User implements ILoginUser {
219
222
  UpdatedById: systemAccess[i].UpdatedById,
220
223
  CreatedAt: systemAccess[i].CreatedAt,
221
224
  UpdatedAt: systemAccess[i].UpdatedAt,
225
+ inheritedBy: ['OWN'],
222
226
  System: system,
223
227
  });
224
228
  }
225
229
  }
226
230
 
231
+ let userGroupRepository = new UserGroupRepository();
232
+ const userGroups = await userGroupRepository.findAll({
233
+ where: {
234
+ UserId: loginUser.UserId,
235
+ Status: 'Active',
236
+ },
237
+ include: [
238
+ {
239
+ model: GroupModel,
240
+ required: true,
241
+ where: {
242
+ Status: 'Active',
243
+ },
244
+ include: [
245
+ {
246
+ model: GroupSystemAccessModel,
247
+ where: {
248
+ Status: 'Active',
249
+ },
250
+ include: [
251
+ {
252
+ model: SystemModel,
253
+ },
254
+ ],
255
+ },
256
+ ],
257
+ },
258
+ ],
259
+ transaction: dbTransaction,
260
+ });
261
+
262
+ if (userGroups) {
263
+ for (let i = 0; i < userGroups.length; i++) {
264
+ let systemAccessList = userGroups[i].Group.GroupSystemAccesses;
265
+ for (let j = 0; j < systemAccessList.length; j++) {
266
+ let systemDetails = systemAccessList[j];
267
+ let isFound = output.findIndex(
268
+ (e) => e.SystemCode === systemDetails.SystemCode,
269
+ );
270
+ if (isFound > -1) {
271
+ output[isFound].inheritedBy.push(userGroups[i].GroupCode);
272
+ } else {
273
+ output.push({
274
+ UserSystemAccessId: systemDetails.GroupSystemAccessId,
275
+ UserId: systemDetails.GroupSystemAccessId,
276
+ SystemCode: systemDetails.SystemCode,
277
+ Status: systemDetails.Status,
278
+ CreatedById: systemDetails.CreatedById,
279
+ UpdatedById: systemDetails.UpdatedById,
280
+ CreatedAt: systemDetails.CreatedAt,
281
+ UpdatedAt: systemDetails.UpdatedAt,
282
+ inheritedBy: [userGroups[i].GroupCode],
283
+ System: systemDetails.System,
284
+ });
285
+ }
286
+ }
287
+ }
288
+ }
289
+
227
290
  // Part 3: Map Result to System Object
228
291
  return output;
229
292
  }
@@ -2132,25 +2132,26 @@ export class User extends UserBase {
2132
2132
  DepartmentCode?: string;
2133
2133
  ContactNo?: string;
2134
2134
  IdNo?: string;
2135
+ FullName?: string;
2135
2136
  },
2136
2137
  loginUser: User,
2137
2138
  dbTransaction: any,
2138
2139
  ) {
2139
2140
  //Part 1: Privilege Checking
2140
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
2141
- const isPrivileged = await loginUser.checkPrivileges(
2142
- systemCode,
2143
- 'User - Update',
2144
- );
2145
-
2146
- //If user does not have privilege to update user, throw a ClassError
2147
- if (!isPrivileged) {
2148
- throw new ClassError(
2149
- 'LoginUser',
2150
- 'LoginUserErrMsg0X',
2151
- 'You do not have the privilege to update user',
2152
- );
2153
- }
2141
+ // const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
2142
+ // const isPrivileged = await loginUser.checkPrivileges(
2143
+ // systemCode,
2144
+ // 'User - Update',
2145
+ // );
2146
+
2147
+ // //If user does not have privilege to update user, throw a ClassError
2148
+ // if (!isPrivileged) {
2149
+ // throw new ClassError(
2150
+ // 'LoginUser',
2151
+ // 'LoginUserErrMsg0X',
2152
+ // 'You do not have the privilege to update user',
2153
+ // );
2154
+ // }
2154
2155
 
2155
2156
  //Part 2: Validation
2156
2157
  //Make sure UserId got values. If not, throw new ClassError
@@ -2422,6 +2423,7 @@ export class User extends UserBase {
2422
2423
 
2423
2424
  //Update user record
2424
2425
  this.UserName = data.UserName;
2426
+ this.FullName = data.FullName;
2425
2427
  this.Email = data.Email;
2426
2428
  this.Status = data.Status;
2427
2429
  this.RecoveryEmail = data.RecoveryEmail;
@@ -2434,6 +2436,7 @@ export class User extends UserBase {
2434
2436
  await User._Repository.update(
2435
2437
  {
2436
2438
  UserName: this.UserName,
2439
+ FullName: this.FullName,
2437
2440
  Email: this.Email,
2438
2441
  Status: this.Status,
2439
2442
  RecoveryEmail: this.RecoveryEmail,
@@ -2499,22 +2502,22 @@ export class User extends UserBase {
2499
2502
  dbTransaction: any,
2500
2503
  UserId: string,
2501
2504
  ) {
2502
- const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
2503
- if ('loginUser' in AuthContext) {
2504
- const isPrivileged = await AuthContext.loginUser.checkPrivileges(
2505
- systemCode,
2506
- 'USER_VIEW',
2507
- );
2508
-
2509
- //If user does not have privilege to update user, throw a ClassError
2510
- if (!isPrivileged) {
2511
- throw new ClassError(
2512
- 'LoginUser',
2513
- 'LoginUserErrMsg0X',
2514
- 'You do not have the privilege to find user',
2515
- );
2516
- }
2517
- }
2505
+ // const systemCode = ApplicationConfig.getComponentConfigValue('system-code');
2506
+ // if ('loginUser' in AuthContext) {
2507
+ // const isPrivileged = await AuthContext.loginUser.checkPrivileges(
2508
+ // systemCode,
2509
+ // 'USER_VIEW',
2510
+ // );
2511
+
2512
+ // //If user does not have privilege to update user, throw a ClassError
2513
+ // if (!isPrivileged) {
2514
+ // throw new ClassError(
2515
+ // 'LoginUser',
2516
+ // 'LoginUserErrMsg0X',
2517
+ // 'You do not have the privilege to find user',
2518
+ // );
2519
+ // }
2520
+ // }
2518
2521
 
2519
2522
  const user = await User._Repository.findOne({
2520
2523
  where: {
@@ -2800,6 +2803,114 @@ export class User extends UserBase {
2800
2803
  }
2801
2804
  }
2802
2805
 
2806
+ async updatePassword(
2807
+ loginUser: User,
2808
+ oldPassword: string,
2809
+ newPassword: string,
2810
+ dbTransaction: any,
2811
+ ) {
2812
+ try {
2813
+ const passwordHashService = new PasswordHashService();
2814
+ const isPasswordValid = await passwordHashService.verify(
2815
+ oldPassword,
2816
+ this.Password,
2817
+ );
2818
+ if (!isPasswordValid) {
2819
+ throw new Error(
2820
+ 'The old password you entered is incorrect. Please try again.',
2821
+ );
2822
+ }
2823
+
2824
+ const passwordHash = await passwordHashService.hashPassword(newPassword);
2825
+
2826
+ const entityValueBefore: IUserAttr = {
2827
+ UserId: this.UserId,
2828
+ UserName: this.UserName,
2829
+ FullName: this.FullName,
2830
+ IDNo: this.IDNo,
2831
+ IDType: this.IDType,
2832
+ ContactNo: this.ContactNo,
2833
+ Email: this.Email,
2834
+ Password: this.Password,
2835
+ Status: this.Status,
2836
+ DefaultPasswordChangedYN: this.DefaultPasswordChangedYN,
2837
+ FirstLoginAt: this.FirstLoginAt,
2838
+ LastLoginAt: this.LastLoginAt,
2839
+ MFAEnabled: this.MFAEnabled,
2840
+ MFAConfig: this.MFAConfig,
2841
+ RecoveryEmail: this.RecoveryEmail,
2842
+ FailedLoginAttemptCount: this.FailedLoginAttemptCount,
2843
+ LastFailedLoginAt: this.LastFailedLoginAt,
2844
+ LastPasswordChangedAt: this.LastPasswordChangedAt,
2845
+ NeedToChangePasswordYN: this.NeedToChangePasswordYN,
2846
+ CreatedById: this.CreatedById,
2847
+ CreatedAt: this.CreatedAt,
2848
+ UpdatedById: this.UpdatedById,
2849
+ UpdatedAt: this.UpdatedAt,
2850
+ PasscodeHash: this.PasscodeHash,
2851
+ PasscodeUpdatedAt: this.PasscodeUpdatedAt,
2852
+ };
2853
+
2854
+ this.Password = passwordHash;
2855
+ this.LastPasswordChangedAt = new Date();
2856
+
2857
+ const entityValueAfter: IUserAttr = {
2858
+ UserId: this.UserId,
2859
+ UserName: this.UserName,
2860
+ FullName: this.FullName,
2861
+ IDNo: this.IDNo,
2862
+ IDType: this.IDType,
2863
+ ContactNo: this.ContactNo,
2864
+ Email: this.Email,
2865
+ Password: this.Password,
2866
+ Status: this.Status,
2867
+ DefaultPasswordChangedYN: this.DefaultPasswordChangedYN,
2868
+ FirstLoginAt: this.FirstLoginAt,
2869
+ LastLoginAt: this.LastLoginAt,
2870
+ MFAEnabled: this.MFAEnabled,
2871
+ MFAConfig: this.MFAConfig,
2872
+ RecoveryEmail: this.RecoveryEmail,
2873
+ FailedLoginAttemptCount: this.FailedLoginAttemptCount,
2874
+ LastFailedLoginAt: this.LastFailedLoginAt,
2875
+ LastPasswordChangedAt: this.LastPasswordChangedAt,
2876
+ NeedToChangePasswordYN: this.NeedToChangePasswordYN,
2877
+ CreatedById: this.CreatedById,
2878
+ CreatedAt: this.CreatedAt,
2879
+ UpdatedById: this.UpdatedById,
2880
+ UpdatedAt: this.UpdatedAt,
2881
+ PasscodeHash: this.PasscodeHash,
2882
+ PasscodeUpdatedAt: this.PasscodeUpdatedAt,
2883
+ };
2884
+
2885
+ await User._Repository.update(
2886
+ {
2887
+ Password: this.Password,
2888
+ LastPasswordChangedAt: this.LastPasswordChangedAt,
2889
+ },
2890
+ {
2891
+ where: {
2892
+ UserId: this.UserId,
2893
+ },
2894
+ transaction: dbTransaction,
2895
+ },
2896
+ );
2897
+
2898
+ // Record update activity using Activity class create method.
2899
+ const activity = new Activity();
2900
+ activity.ActivityId = activity.createId();
2901
+ activity.Action = ActionEnum.UPDATE;
2902
+ activity.Description = 'Update Password';
2903
+ activity.EntityType = 'LoginUser';
2904
+ activity.EntityId = this.UserId.toString();
2905
+ activity.EntityValueBefore = JSON.stringify(entityValueBefore);
2906
+ activity.EntityValueAfter = JSON.stringify(entityValueAfter);
2907
+
2908
+ await activity.create(loginUser.ObjectId, dbTransaction);
2909
+ } catch (error) {
2910
+ throw error;
2911
+ }
2912
+ }
2913
+
2803
2914
  async verifyPasscode(passcode: string) {
2804
2915
  try {
2805
2916
  // Return error if this._Userid is empty "User is not created yet."
@@ -260,6 +260,7 @@ export class System extends ObjectBase {
260
260
 
261
261
  //Call System._Repo findAll() method by passing queryObj and whereObj
262
262
  const result = await System._Repo.findAllWithPagination({
263
+ distinct: true,
263
264
  where: queryObj,
264
265
  ...whereObj,
265
266
  order: [['CreatedAt', 'DESC']],
@@ -252,6 +252,7 @@ export class SystemPrivilege extends ObjectBase {
252
252
 
253
253
  // Part 2: Retrieve listing
254
254
  const options: any = {
255
+ distinct: true,
255
256
  order: [['createdAt', 'DESC']],
256
257
  transaction: dbTransaction,
257
258
  };
@@ -140,6 +140,7 @@ export class UserPrivilege extends ObjectBase {
140
140
  }
141
141
 
142
142
  const options: any = {
143
+ distinct: true,
143
144
  where: {
144
145
  UserId: whereOption.UserId,
145
146
  },
@@ -138,6 +138,7 @@ export class UserSystemAccess extends ObjectBase {
138
138
  // Set up pagination logic using the pagination parameter:
139
139
  // Calculate offset based on page and limit.
140
140
  const options: any = {
141
+ distinct: true,
141
142
  where: {
142
143
  UserId: whereOption.UserId,
143
144
  },