UNPKG

@tomei/sso

Version:
410 lines 19.9 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserGroup = void 0; const general_1 = require("@tomei/general"); const user_group_repository_1 = require("./user-group.repository"); const config_1 = require("@tomei/config"); const activity_history_1 = require("@tomei/activity-history"); const group_system_access_entity_1 = require("../../models/group-system-access.entity"); const group_entity_1 = require("../../models/group.entity"); const system_entity_1 = require("../../models/system.entity"); const user_entity_1 = require("../../models/user.entity"); class UserGroup extends general_1.ObjectBase { get CreatedAt() { return this._CreatedAt; } get UpdatedAt() { return this._UpdatedAt; } get CreatedById() { return this._CreatedById; } get UpdatedById() { return this._UpdatedById; } constructor(userGroupAttr) { super(); this.ObjectType = 'UserGroup'; this.TableName = 'sso_UserGroup'; this.InheritGroupPrivilegeYN = 'Y'; this.InheritGroupSystemAccessYN = 'Y'; if (userGroupAttr) { this.UserGroupId = userGroupAttr.UserGroupId; this.UserId = userGroupAttr.UserId; this.GroupCode = userGroupAttr.GroupCode; this.Status = userGroupAttr.Status; this.InheritGroupPrivilegeYN = userGroupAttr.InheritGroupPrivilegeYN; this.InheritGroupSystemAccessYN = userGroupAttr.InheritGroupSystemAccessYN; this._CreatedById = userGroupAttr.CreatedById; this._CreatedAt = userGroupAttr.CreatedAt; this._UpdatedById = userGroupAttr.UpdatedById; this._UpdatedAt = userGroupAttr.UpdatedAt; } } static init(dbTransaction, UserGroupId) { return __awaiter(this, void 0, void 0, function* () { try { const userGroup = new UserGroup(); if (UserGroupId) { const userGroupAttr = yield this._Repository.findOne({ where: { UserGroupId }, transaction: dbTransaction, }); if (userGroupAttr) { userGroup.UserGroupId = userGroupAttr.UserGroupId; userGroup.UserId = userGroupAttr.UserId; userGroup.GroupCode = userGroupAttr.GroupCode; userGroup.Status = userGroupAttr.Status; userGroup.InheritGroupPrivilegeYN = userGroupAttr.InheritGroupPrivilegeYN; userGroup.InheritGroupSystemAccessYN = userGroupAttr.InheritGroupSystemAccessYN; userGroup._CreatedById = userGroupAttr.CreatedById; userGroup._CreatedAt = userGroupAttr.CreatedAt; userGroup._UpdatedById = userGroupAttr.UpdatedById; userGroup._UpdatedAt = userGroupAttr.UpdatedAt; } else { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg00', 'UserGroup Not Found'); } } return userGroup; } catch (error) { throw error; } }); } create(loginUser, dbTransaction, group, user) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_GROUP_CREATE'); if (!isPrivileged) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg0X', 'User does not have privilege to create user group.'); } if (!group.GroupCode) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg02', 'GroupCode is required.'); } if (!user.UserId) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg03', 'UserId is required.'); } const userGroup = yield UserGroup.findOne(dbTransaction, loginUser, group.GroupCode, user.UserId); if (userGroup) { return userGroup; } this.UserId = user.UserId; this.GroupCode = group.GroupCode; this.Status = 'Active'; this._CreatedById = loginUser.UserId; this._CreatedAt = new Date(); this._UpdatedById = loginUser.UserId; this._UpdatedAt = new Date(); const userData = yield UserGroup._Repository.create({ UserId: this.UserId, GroupCode: this.GroupCode, Status: this.Status, CreatedById: this._CreatedById, CreatedAt: this._CreatedAt, UpdatedById: this._UpdatedById, UpdatedAt: this._UpdatedAt, InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN, InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN, }, { transaction: dbTransaction, }); this.UserGroupId = userData.UserGroupId; const EntityValueAfter = { UserGroupId: this.UserGroupId, UserId: this.UserId, GroupCode: this.GroupCode, Status: this.Status, CreatedById: this._CreatedById, CreatedAt: this._CreatedAt, UpdatedById: this._UpdatedById, UpdatedAt: this._UpdatedAt, InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN, InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN, }; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Assign user to group.'; activity.EntityType = 'UserGroup'; activity.EntityId = this.UserGroupId.toString(); activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(EntityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } }); } static findOne(dbTransaction, loginUser, GroupCode, UserId) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_GROUP_VIEW'); if (!isPrivileged) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg0X', 'User does not have privilege to view user group.'); } const userGroupAttr = yield UserGroup._Repository.findOne({ where: { UserId, GroupCode, }, transaction: dbTransaction, }); if (userGroupAttr) { return new UserGroup(userGroupAttr.get({ plain: true })); } return null; } catch (error) { throw error; } }); } static getUser(dbTransaction, loginUser, GroupCode) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_GROUP_VIEW'); if (!isPrivileged) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg0X', 'User does not have privilege to view user group.'); } const userGroup = yield UserGroup._Repository.findAll({ where: { GroupCode, }, include: [ { model: user_entity_1.default, as: 'User', attributes: ['UserId', 'FullName', 'Email'], }, ], transaction: dbTransaction, }); return userGroup; return null; } catch (error) { throw error; } }); } static findAllInheritedSystemAccesses(UserId, loginUser, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_SYSTEM_ACCESS_LIST'); if (!isPrivileged) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg0X', 'User does not have privilege to view user system access.', 'findAllInheritedSystemAccesses', 403); } const userGroups = yield UserGroup._Repository.findAll({ where: { UserId, Status: 'Active', }, include: [ { model: group_entity_1.default, required: true, where: { Status: 'Active', }, include: [ { model: group_system_access_entity_1.default, where: { Status: 'Active', }, include: [ { model: system_entity_1.default, }, ], }, ], }, ], transaction: dbTransaction, }); const result = []; for (const userGroup of userGroups) { const groupData = { UserGroupId: userGroup.UserGroupId, GroupCode: userGroup.GroupCode, GroupName: userGroup.Group.Name, InheritGroupSystemAccessYN: userGroup.InheritGroupSystemAccessYN, CreatedAt: userGroup.CreatedAt, UpdatedAt: userGroup.UpdatedAt, Systems: [], }; if (userGroup.InheritGroupSystemAccessYN === 'Y') { groupData.Systems = userGroup.Group.GroupSystemAccesses.map((groupSystemAccess) => { return { SystemCode: groupSystemAccess.System.SystemCode, SystemName: groupSystemAccess.System.Name, AccessStatus: groupSystemAccess.Status, CreatedAt: groupSystemAccess.CreatedAt, UpdatedAt: groupSystemAccess.UpdatedAt, }; }); } result.push(groupData); } return result; } catch (error) { throw error; } }); } update(loginUser, dbTransaction, UpdatedProperties) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_GROUP_UPDATE'); if (!isPrivileged) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg0X', 'User does not have privilege to update user group.', 'update', 403); } if (!UpdatedProperties.InheritGroupPrivilegeYN && !UpdatedProperties.InheritGroupSystemAccessYN) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg04', 'At least one of the properties to update is required.', 'update', 400); } const entityValueBefore = { UserGroupId: this.UserGroupId, UserId: this.UserId, GroupCode: this.GroupCode, Status: this.Status, CreatedById: this._CreatedById, CreatedAt: this._CreatedAt, UpdatedById: this._UpdatedById, UpdatedAt: this._UpdatedAt, InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN, InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN, }; this._UpdatedById = loginUser.UserId; this._UpdatedAt = new Date(); if (UpdatedProperties.InheritGroupPrivilegeYN) { this.InheritGroupPrivilegeYN = UpdatedProperties.InheritGroupPrivilegeYN; } if (UpdatedProperties.InheritGroupSystemAccessYN) { this.InheritGroupSystemAccessYN = UpdatedProperties.InheritGroupSystemAccessYN; } yield UserGroup._Repository.update({ InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN, InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN, UpdatedById: this._UpdatedById, UpdatedAt: this._UpdatedAt, }, { where: { UserGroupId: this.UserGroupId, }, transaction: dbTransaction, }); const entityValueAfter = { UserGroupId: this.UserGroupId, UserId: this.UserId, GroupCode: this.GroupCode, Status: this.Status, CreatedById: this._CreatedById, CreatedAt: this._CreatedAt, UpdatedById: this._UpdatedById, UpdatedAt: this._UpdatedAt, InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN, InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN, }; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = 'Update User Group'; activity.EntityType = 'UserGroup'; activity.EntityId = this.UserGroupId.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(entityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } }); } static isUserMemberOfGroup(dbTransaction, loginUser, UserId, GroupCode) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_GROUP_VIEW'); if (!isPrivileged) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg0X', 'User does not have privilege to view user group.', 'isUserMemberOfGroup', 403); } const userGroup = yield UserGroup.findOne(dbTransaction, loginUser, GroupCode, UserId); return !!userGroup; } catch (error) { throw error; } }); } delete(loginUser, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_GROUP_DELETE'); if (!isPrivileged) { throw new general_1.ClassError('UserGroup', 'UserGroupErrMsg0X', 'User does not have privilege to delete user group.', 'delete', 403); } yield UserGroup._Repository.delete({ where: { UserGroupId: this.UserGroupId, }, transaction: dbTransaction, }); const entityValueBefore = { UserGroupId: this.UserGroupId, UserId: this.UserId, GroupCode: this.GroupCode, Status: this.Status, CreatedById: this._CreatedById, CreatedAt: this._CreatedAt, UpdatedById: this._UpdatedById, UpdatedAt: this._UpdatedAt, InheritGroupPrivilegeYN: this.InheritGroupPrivilegeYN, InheritGroupSystemAccessYN: this.InheritGroupSystemAccessYN, }; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.DELETE; activity.Description = `Delete User Group ${this.UserGroupId}`; activity.EntityType = 'UserGroup'; activity.EntityId = this.UserGroupId.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify({}); yield activity.create(loginUser.ObjectId, dbTransaction); } catch (error) { throw error; } }); } } exports.UserGroup = UserGroup; UserGroup._Repository = new user_group_repository_1.UserGroupRepository(); //# sourceMappingURL=user-group.js.map