@tomei/sso 0.36.0 → 0.37.0
Sign up to get free protection for your applications and to get access to all the features.
- 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/user-group/user-group.ts +210 -2
package/package.json
CHANGED
@@ -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
|
}
|