@tomei/sso 0.35.8 → 0.37.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/src/components/login-user/user.d.ts +1 -0
- package/dist/src/components/login-user/user.js +58 -0
- package/dist/src/components/login-user/user.js.map +1 -1
- package/dist/src/components/user-group/user-group.d.ts +4 -0
- package/dist/src/components/user-group/user-group.js +95 -0
- package/dist/src/components/user-group/user-group.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/components/login-user/user.ts +93 -0
- package/src/components/user-group/user-group.ts +210 -2
package/package.json
CHANGED
@@ -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
|
}
|
@@ -1,6 +1,10 @@
|
|
1
1
|
import { ClassError, ObjectBase } from '@tomei/general';
|
2
2
|
import { UserGroupRepository } from './user-group.repository';
|
3
3
|
import { IUserGroupAttr } from '../../interfaces/user-group.interface';
|
4
|
+
import { LoginUser, User } from 'components/login-user';
|
5
|
+
import { Group } from 'components/group';
|
6
|
+
import { ApplicationConfig } from '@tomei/config';
|
7
|
+
import { ActionEnum, Activity } from '@tomei/activity-history';
|
4
8
|
|
5
9
|
export class UserGroup extends ObjectBase {
|
6
10
|
ObjectType = 'UserGroup';
|
@@ -10,8 +14,8 @@ export class UserGroup extends ObjectBase {
|
|
10
14
|
UserGroupId: number;
|
11
15
|
UserId: number;
|
12
16
|
GroupCode: string;
|
13
|
-
InheritGroupPrivilegeYN
|
14
|
-
InheritGroupSystemAccessYN
|
17
|
+
InheritGroupPrivilegeYN = 'Y';
|
18
|
+
InheritGroupSystemAccessYN = 'Y';
|
15
19
|
Status: string;
|
16
20
|
private _CreatedAt: Date;
|
17
21
|
private _UpdatedAt: Date;
|
@@ -87,4 +91,208 @@ export class UserGroup extends ObjectBase {
|
|
87
91
|
throw error;
|
88
92
|
}
|
89
93
|
}
|
94
|
+
|
95
|
+
async create(
|
96
|
+
loginUser: LoginUser,
|
97
|
+
dbTransaction: any,
|
98
|
+
group: Group,
|
99
|
+
user: User,
|
100
|
+
) {
|
101
|
+
//This method will create a user group record.
|
102
|
+
try {
|
103
|
+
// Part 1: Privilege Checking
|
104
|
+
// Call loginUser.checkPrivileges() by passing:
|
105
|
+
// SystemCode: "<get_from_app_config>"
|
106
|
+
// PrivilegeCode: "USER_GROUP_CREATE"
|
107
|
+
const systemCode =
|
108
|
+
ApplicationConfig.getComponentConfigValue('system-code');
|
109
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
110
|
+
systemCode,
|
111
|
+
'USER_GROUP_CREATE',
|
112
|
+
);
|
113
|
+
|
114
|
+
// If user does not have privilege to update user, throw a ClassError
|
115
|
+
if (!isPrivileged) {
|
116
|
+
throw new ClassError(
|
117
|
+
'UserGroup',
|
118
|
+
'UserGroupErrMsg0X',
|
119
|
+
'User does not have privilege to create user group.',
|
120
|
+
);
|
121
|
+
}
|
122
|
+
|
123
|
+
// Part 2: Validation
|
124
|
+
// Make sure group.GroupCode exists, if not throw new ClassError by passing:
|
125
|
+
// Classname: "UserGroup"
|
126
|
+
// MethodName: "create"
|
127
|
+
// MessageCode: "UserGroupErrMsg02"
|
128
|
+
// Message: "GroupCode is required."
|
129
|
+
if (!group.GroupCode) {
|
130
|
+
throw new ClassError(
|
131
|
+
'UserGroup',
|
132
|
+
'UserGroupErrMsg02',
|
133
|
+
'GroupCode is required.',
|
134
|
+
);
|
135
|
+
}
|
136
|
+
|
137
|
+
// Make sure user.UserId exists, if not throw new ClassError by passing:
|
138
|
+
// Classname: "UserGroup"
|
139
|
+
// MethodName: "create"
|
140
|
+
// MessageCode: "UserGroupErrMsg03"
|
141
|
+
// Message: "UserId is required."
|
142
|
+
if (!user.UserId) {
|
143
|
+
throw new ClassError(
|
144
|
+
'UserGroup',
|
145
|
+
'UserGroupErrMsg03',
|
146
|
+
'UserId is required.',
|
147
|
+
);
|
148
|
+
}
|
149
|
+
|
150
|
+
// Call UserGroup.findOne static method by passing:
|
151
|
+
// loginUser
|
152
|
+
// dbTransaction
|
153
|
+
// GroupCode: group.GroupCode
|
154
|
+
// UserId: user.UserId
|
155
|
+
const userGroup = await UserGroup.findOne(
|
156
|
+
dbTransaction,
|
157
|
+
loginUser,
|
158
|
+
group.GroupCode,
|
159
|
+
user.UserId,
|
160
|
+
);
|
161
|
+
|
162
|
+
if (userGroup) {
|
163
|
+
return userGroup;
|
164
|
+
}
|
165
|
+
|
166
|
+
// Part 3: Create
|
167
|
+
// Set below attributes:
|
168
|
+
// UserGroupId: this.createId()
|
169
|
+
// UserId: Params.user.UserId
|
170
|
+
// GroupCode: Params.group.GroupCode
|
171
|
+
// Status: "Active"
|
172
|
+
// CreatedById: loginUser.ObjectId
|
173
|
+
// CreatedAt: current timestamp
|
174
|
+
// UpdatedById: loginUser.ObjectId
|
175
|
+
// UpdatedAt: current timestamp
|
176
|
+
this.UserId = user.UserId;
|
177
|
+
this.GroupCode = group.GroupCode;
|
178
|
+
this.Status = 'Active';
|
179
|
+
this._CreatedById = loginUser.UserId;
|
180
|
+
this._CreatedAt = new Date();
|
181
|
+
this._UpdatedById = loginUser.UserId;
|
182
|
+
this._UpdatedAt = new Date();
|
183
|
+
|
184
|
+
// Call UserGroup._Repo create() method by passing:
|
185
|
+
// populate this instance attributes
|
186
|
+
// dbTransaction
|
187
|
+
|
188
|
+
const userData = await UserGroup._Repository.create(
|
189
|
+
{
|
190
|
+
UserId: this.UserId,
|
191
|
+
GroupCode: this.GroupCode,
|
192
|
+
Status: this.Status,
|
193
|
+
CreatedById: this._CreatedById,
|
194
|
+
CreatedAt: this._CreatedAt,
|
195
|
+
UpdatedById: this._UpdatedById,
|
196
|
+
UpdatedAt: this._UpdatedAt,
|
197
|
+
InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN,
|
198
|
+
InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN,
|
199
|
+
},
|
200
|
+
{
|
201
|
+
transaction: dbTransaction,
|
202
|
+
},
|
203
|
+
);
|
204
|
+
|
205
|
+
this.UserGroupId = userData.UserGroupId;
|
206
|
+
|
207
|
+
// Part 4: Record Create UserGroup Activity
|
208
|
+
// Initialise EntityValueAfter variable and set to this instance
|
209
|
+
const EntityValueAfter = {
|
210
|
+
UserGroupId: this.UserGroupId,
|
211
|
+
UserId: this.UserId,
|
212
|
+
GroupCode: this.GroupCode,
|
213
|
+
Status: this.Status,
|
214
|
+
CreatedById: this._CreatedById,
|
215
|
+
CreatedAt: this._CreatedAt,
|
216
|
+
UpdatedById: this._UpdatedById,
|
217
|
+
UpdatedAt: this._UpdatedAt,
|
218
|
+
InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN,
|
219
|
+
InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN,
|
220
|
+
};
|
221
|
+
// Instantiate new activity from Activity class, call createId() method, then set:
|
222
|
+
// Action: ActionEnum.Create
|
223
|
+
// Description: Assign user to group.
|
224
|
+
// EntityType: "UserGroup"
|
225
|
+
// EntityId: this.UserGroupId
|
226
|
+
// EntityValueBefore: <stringify of empty object>
|
227
|
+
// EntityValueAfter: EntityValueAfter
|
228
|
+
const activity = new Activity();
|
229
|
+
activity.Action = ActionEnum.ADD;
|
230
|
+
activity.Description = 'Assign user to group.';
|
231
|
+
activity.EntityType = 'UserGroup';
|
232
|
+
activity.EntityId = this.UserGroupId.toString();
|
233
|
+
activity.EntityValueBefore = JSON.stringify({});
|
234
|
+
activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
|
235
|
+
// Call new activity create method by passing:
|
236
|
+
// dbTransaction
|
237
|
+
// userId: loginUser.ObjectId
|
238
|
+
// return this instance
|
239
|
+
await activity.create(loginUser.ObjectId, dbTransaction);
|
240
|
+
|
241
|
+
return this;
|
242
|
+
} catch (error) {
|
243
|
+
throw error;
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
public static async findOne(
|
248
|
+
dbTransaction: any,
|
249
|
+
loginUser: LoginUser,
|
250
|
+
GroupCode: string,
|
251
|
+
UserId: number,
|
252
|
+
): Promise<UserGroup> {
|
253
|
+
try {
|
254
|
+
// Part 1: Privilege Checking
|
255
|
+
// Call loginUser.checkPrivileges() by passing:
|
256
|
+
// SystemCode: "<get_from_app_config>"
|
257
|
+
// PrivilegeCode: "USER_GROUP_VIEW"
|
258
|
+
const systemCode =
|
259
|
+
ApplicationConfig.getComponentConfigValue('system-code');
|
260
|
+
const isPrivileged = await loginUser.checkPrivileges(
|
261
|
+
systemCode,
|
262
|
+
'USER_GROUP_VIEW',
|
263
|
+
);
|
264
|
+
|
265
|
+
// If user does not have privilege to view user group, throw a ClassError
|
266
|
+
if (!isPrivileged) {
|
267
|
+
throw new ClassError(
|
268
|
+
'UserGroup',
|
269
|
+
'UserGroupErrMsg0X',
|
270
|
+
'User does not have privilege to view user group.',
|
271
|
+
);
|
272
|
+
}
|
273
|
+
|
274
|
+
// Part 2: Retrieve Record
|
275
|
+
// Call UserGroup._Repo findOne method by passing:
|
276
|
+
// where:
|
277
|
+
// [Op.AND]:
|
278
|
+
// UserId: Params.UserId
|
279
|
+
// GroupCode: Params.GroupCode
|
280
|
+
// dbTransaction
|
281
|
+
const userGroupAttr = await UserGroup._Repository.findOne({
|
282
|
+
where: {
|
283
|
+
UserId,
|
284
|
+
GroupCode,
|
285
|
+
},
|
286
|
+
transaction: dbTransaction,
|
287
|
+
});
|
288
|
+
// If record exists, instantiate UserGroup by calling the private constructor and passing the attributes. Then, returns the instance
|
289
|
+
if (userGroupAttr) {
|
290
|
+
return new UserGroup(userGroupAttr.get({ plain: true }));
|
291
|
+
}
|
292
|
+
// If record not exists, return null.
|
293
|
+
return null;
|
294
|
+
} catch (error) {
|
295
|
+
throw error;
|
296
|
+
}
|
297
|
+
}
|
90
298
|
}
|