@tomei/sso 0.35.8 → 0.36.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomei/sso",
3
- "version": "0.35.8",
3
+ "version": "0.36.0",
4
4
  "description": "Tomei SSO Package",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -33,6 +33,7 @@ import * as speakeasy from 'speakeasy';
33
33
  import { LoginStatusEnum } from '../../enum/login-status.enum';
34
34
  import { RedisService } from '../../redis-client/redis.service';
35
35
  import { LoginUser } from './login-user';
36
+ import { SessionService } from 'session';
36
37
 
37
38
  export class User extends UserBase {
38
39
  ObjectId: string;
@@ -2388,4 +2389,96 @@ export class User extends UserBase {
2388
2389
  throw error;
2389
2390
  }
2390
2391
  }
2392
+
2393
+ public static async findByEmail(
2394
+ loginUser: LoginUser,
2395
+ dbTransaction: any,
2396
+ Email: string,
2397
+ ): Promise<User> {
2398
+ //This method search user record by their email.
2399
+ try {
2400
+ // Part 1: Privilege Checking
2401
+ // Call loginUser.checkPrivilege() by passing:
2402
+ // SystemCode: "<get_from_app_config>"
2403
+ // PrivilegeCode: "USER_VIEW"
2404
+ const systemCode =
2405
+ ApplicationConfig.getComponentConfigValue('system-code');
2406
+ const isPrivileged = await loginUser.checkPrivileges(
2407
+ systemCode,
2408
+ 'USER_VIEW',
2409
+ );
2410
+
2411
+ // If user does not have privilege to update user, throw a ClassError
2412
+ if (!isPrivileged) {
2413
+ throw new ClassError(
2414
+ 'LoginUser',
2415
+ 'LoginUserErrMsg0X',
2416
+ 'You do not have the privilege to find user',
2417
+ );
2418
+ }
2419
+
2420
+ // Part 2: Retrieve User & Returns
2421
+ // Call User._Repo findOne method by passing:
2422
+ // where:
2423
+ // Email: Param.Email
2424
+ // Status: 'Active'
2425
+ // dbTransaction
2426
+
2427
+ const user = await User._Repository.findOne({
2428
+ where: {
2429
+ Email: Email,
2430
+ },
2431
+ include: [
2432
+ {
2433
+ model: Staff,
2434
+ },
2435
+ ],
2436
+ transaction: dbTransaction,
2437
+ });
2438
+ // Instantiate new User by mapping all user info returned from above step.
2439
+ if (!user) {
2440
+ // If user not found, throw new ClassError by passing:
2441
+ // Classname: "User"
2442
+ // MethodName: "findByEmail"
2443
+ // MessageCode: "UserErrMsg0X"
2444
+ // Message: "User not found."
2445
+
2446
+ throw new ClassError('User', 'UserErrMsg0X', 'User not found.');
2447
+ }
2448
+
2449
+ const userAttr: IUserAttr = {
2450
+ UserId: user.UserId,
2451
+ UserName: user.UserName,
2452
+ FullName: user?.FullName || null,
2453
+ IDNo: user?.IdNo || null,
2454
+ IDType: user?.IdType || null,
2455
+ ContactNo: user?.ContactNo || null,
2456
+ Email: user.Email,
2457
+ Password: user.Password,
2458
+ Status: user.Status,
2459
+ DefaultPasswordChangedYN: user.DefaultPasswordChangedYN,
2460
+ FirstLoginAt: user.FirstLoginAt,
2461
+ LastLoginAt: user.LastLoginAt,
2462
+ MFAEnabled: user.MFAEnabled,
2463
+ MFAConfig: user.MFAConfig,
2464
+ RecoveryEmail: user.RecoveryEmail,
2465
+ FailedLoginAttemptCount: user.FailedLoginAttemptCount,
2466
+ LastFailedLoginAt: user.LastFailedLoginAt,
2467
+ LastPasswordChangedAt: user.LastPasswordChangedAt,
2468
+ NeedToChangePasswordYN: user.NeedToChangePasswordYN,
2469
+ CreatedById: user.CreatedById,
2470
+ CreatedAt: user.CreatedAt,
2471
+ UpdatedById: user.UpdatedById,
2472
+ UpdatedAt: user.UpdatedAt,
2473
+ staffs: user?.Staff,
2474
+ };
2475
+ const sessionService = await SessionService.init(undefined);
2476
+ const usr = new User(sessionService, undefined, userAttr);
2477
+
2478
+ return usr;
2479
+ // Return the user instance.
2480
+ } catch (error) {
2481
+ throw error;
2482
+ }
2483
+ }
2391
2484
  }