UNPKG

@tomei/sso

Version:
431 lines 21.4 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.UserSystemAccess = void 0; const general_1 = require("@tomei/general"); const user_system_access_repository_1 = require("./user-system-access.repository"); const config_1 = require("@tomei/config"); const system_entity_1 = require("../../models/system.entity"); const system_privilege_entity_1 = require("../../models/system-privilege.entity"); const user_system_access_entity_1 = require("../../models/user-system-access.entity"); const group_entity_1 = require("../../models/group.entity"); const group_system_access_entity_1 = require("../../models/group-system-access.entity"); const user_entity_1 = require("../../models/user.entity"); const activity_history_1 = require("@tomei/activity-history"); const sequelize_1 = require("sequelize"); const user_privilege_repository_1 = require("../user-privilege/user-privilege.repository"); const enum_1 = require("../../enum"); class UserSystemAccess 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(userSystemAccessAttr) { super(); this.ObjectType = 'UserSystemAccess'; this.TableName = 'sso_UserSystemAccess'; if (userSystemAccessAttr) { this.UserSystemAccessId = userSystemAccessAttr.UserSystemAccessId; this.UserId = userSystemAccessAttr.UserId; this.SystemCode = userSystemAccessAttr.SystemCode; this.Status = userSystemAccessAttr.Status; this._CreatedById = userSystemAccessAttr.CreatedById; this._CreatedAt = userSystemAccessAttr.CreatedAt; this._UpdatedById = userSystemAccessAttr.UpdatedById; this._UpdatedAt = userSystemAccessAttr.UpdatedAt; } } static init(dbTransaction, UserSystemAccessId) { return __awaiter(this, void 0, void 0, function* () { try { const userSystemAccess = new UserSystemAccess(); if (UserSystemAccessId) { const userSystemAccessAttr = yield this._Repository.findOne({ where: { UserSystemAccessId }, transaction: dbTransaction, }); if (userSystemAccessAttr) { return new UserSystemAccess(userSystemAccessAttr.get({ plain: true })); } else { throw new general_1.ClassError('UserSystemAccess', 'UserSystemAccessErrMsg00', 'UserSystemAccess not found'); } } return userSystemAccess; } catch (error) { throw error; } }); } static findAll(loginUser, dbTransaction, whereOption, pagination) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const privilegeCode = 'USER_SYSTEM_ACCESS_LIST'; const isPrivileged = yield loginUser.checkPrivileges(systemCode, privilegeCode); if (!isPrivileged) { throw new general_1.ClassError('UserSystemAccess', 'UserSystemAccessErrMsg01', 'You do not have permission to access this resource.'); } const options = { distinct: true, where: { UserId: whereOption.UserId, }, offset: (pagination.page - 1) * pagination.limit, limit: pagination.limit, transaction: dbTransaction, include: [ { model: system_entity_1.default, attributes: ['Name', 'SystemCode'], }, { model: user_entity_1.default, as: 'CreatedBy', attributes: ['FullName'], }, { model: user_entity_1.default, as: 'UpdatedBy', attributes: ['FullName'], }, ], }; const userSystemAccesses = yield this._Repository.findAllWithPagination(options); return { records: userSystemAccesses.rows.map((userSystemAccess) => { return { UserSystemAccessId: userSystemAccess.UserSystemAccessId, SystemName: userSystemAccess.System.Name, SystemCode: userSystemAccess.System.SystemCode, Status: userSystemAccess.Status, CreatedBy: userSystemAccess.CreatedBy.FullName, CreatedAt: userSystemAccess.CreatedAt, UpdatedBy: userSystemAccess.UpdatedBy.FullName, UpdatedAt: userSystemAccess.UpdatedAt, }; }), pagination: { currentPage: pagination.page, pageSize: pagination.limit, totalRecords: userSystemAccesses.count, }, }; } catch (error) { throw error; } }); } static findAllUsers(loginUser, dbTransaction, SystemCode, Page, Rows, Search) { return __awaiter(this, void 0, void 0, function* () { 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('UserSystemAccessUser', 'UserSystemAccessUserErrMsg01', 'You do not have permission to view system access users.'); } try { const queryObj = { SystemCode: SystemCode }; if (Search) { Object.entries(Search).forEach(([key, value]) => { queryObj[key] = value; }); } let options = { where: queryObj, distinct: true, transaction: dbTransaction, }; if (Page && Rows) { options = Object.assign(Object.assign({}, options), { limit: Rows, offset: Rows * (Page - 1), order: [['CreatedAt', 'DESC']], include: [ { model: system_entity_1.default, attributes: ['Name', 'SystemCode'], }, { model: user_entity_1.default, where: { Status: enum_1.UserStatus.ACTIVE, }, as: 'User', attributes: ['UserId', 'FullName', 'Email'], }, ] }); } const userSystemAccesses = yield this._Repository.findAndCountAll(options); return userSystemAccesses; } catch (error) { throw error; } }); } static findAllUserPrivileges(loginUser, dbTransaction, SystemCode, search) { return __awaiter(this, void 0, void 0, function* () { 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('UserSystemAccessUser', 'UserSystemAccessUserErrMsg01', 'You do not have permission to view system access users.'); } try { let systemWhere = {}; if (SystemCode) { systemWhere = { SystemCode: { [sequelize_1.Op.substring]: SystemCode, }, }; } const allSystemAccessUsers = yield user_system_access_entity_1.default.findAll({ include: [ { model: system_entity_1.default, where: systemWhere, }, { model: user_entity_1.default, as: 'User', attributes: ['UserId', 'FullName'], }, ], transaction: dbTransaction, }); const allPrivileges = yield system_privilege_entity_1.default.findAll({ where: systemWhere, transaction: dbTransaction, }); const systemAccessUserPrivileges = allPrivileges.map((privilege) => __awaiter(this, void 0, void 0, function* () { const filteredUsers = allSystemAccessUsers .map((userAccess) => userAccess.User) .filter((user) => search.UserId.includes(String(user.UserId))); return Object.assign(Object.assign({}, privilege.get({ plain: true })), { Users: filteredUsers }); })); return systemAccessUserPrivileges; } catch (error) { throw error; } }); } static findAllUserRoles(loginUser, dbTransaction, SystemCode, search) { return __awaiter(this, void 0, void 0, function* () { 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('UserSystemAccessUser', 'UserSystemAccessUserErrMsg01', 'You do not have permission to view system access users.'); } try { let systemWhere = {}; if (SystemCode) { systemWhere = { SystemCode: { [sequelize_1.Op.substring]: SystemCode, }, }; } const allGroupSystemAccess = yield group_system_access_entity_1.default.findAll({ where: systemWhere, include: [ { model: group_entity_1.default, where: { Type: 'Role', }, }, ], transaction: dbTransaction, }); const allSystemAccessUsers = yield user_system_access_entity_1.default.findAll({ include: [ { model: system_entity_1.default, where: systemWhere, }, { model: user_entity_1.default, as: 'User', attributes: ['UserId', 'FullName'], }, ], transaction: dbTransaction, }); const systemAccessUserRoles = allGroupSystemAccess.map((groupSystemAccess) => { const filteredUsers = allSystemAccessUsers .map((userAccess) => userAccess.User) .filter((user) => search.UserId.includes(String(user.UserId))); return Object.assign(Object.assign({}, groupSystemAccess.Group.get({ plain: true })), { Users: filteredUsers }); }); return systemAccessUserRoles; } catch (error) { throw error; } }); } static createAccess(loginUser, dbTransaction, UserId, SystemCode, Status) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const privilegeCode = 'USER_SYSTEM_ACCESS_CREATE'; const isPrivileged = yield loginUser.checkPrivileges(systemCode, privilegeCode); if (!isPrivileged) { throw new general_1.ClassError('UserSystemAccess', 'UserSystemAccessErrMsg01', 'You do not have permission to access this resource.'); } const isExist = yield UserSystemAccess._Repository.findAll({ where: { [sequelize_1.Op.and]: [{ UserId: UserId }, { SystemCode: SystemCode }] }, transaction: dbTransaction, }); if ((isExist === null || isExist === void 0 ? void 0 : isExist.length) > 0) { throw new general_1.ClassError('UserSystemAccess', 'UserSystemAccessErrMsg01', 'User already have access to this system'); } const newUserSystemAccess = new UserSystemAccess(); newUserSystemAccess.UserId = parseInt(UserId); newUserSystemAccess.SystemCode = SystemCode; newUserSystemAccess.Status = Status; newUserSystemAccess._CreatedById = loginUser.UserId; newUserSystemAccess._CreatedAt = new Date(); newUserSystemAccess._UpdatedById = loginUser.UserId; newUserSystemAccess._UpdatedAt = new Date(); const payload = { UserId: newUserSystemAccess.UserId, SystemCode: newUserSystemAccess.SystemCode, Status: newUserSystemAccess.Status, CreatedById: newUserSystemAccess.CreatedById, CreatedAt: newUserSystemAccess.CreatedAt, UpdatedById: newUserSystemAccess.UpdatedById, UpdatedAt: newUserSystemAccess.UpdatedAt, }; const systemAccess = yield UserSystemAccess._Repository.create(payload, { transaction: dbTransaction, }); const entityValueBefore = {}; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Create User System Access'; activity.EntityType = 'UserSystemAccess'; activity.EntityId = (_a = systemAccess.UserSystemAccessId) === null || _a === void 0 ? void 0 : _a.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(payload); yield activity.create(loginUser.ObjectId, dbTransaction); newUserSystemAccess.UserSystemAccessId = systemAccess.UserSystemAccessId; return newUserSystemAccess; } catch (error) { throw error; } }); } update(loginUser, dbTransaction, Status) { return __awaiter(this, void 0, void 0, function* () { try { const entityValueBefore = { UserId: this.UserId, SystemCode: this.SystemCode, Status: this.Status, CreatedById: this.CreatedById, CreatedAt: this.CreatedAt, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }; yield UserSystemAccess._Repository.update({ Status: Status, UpdatedById: loginUser.UserId, UpdatedAt: new Date(), }, { where: { UserSystemAccessId: this.UserSystemAccessId, }, transaction: dbTransaction, }); const entityValueAfter = { UserId: this.UserId, SystemCode: this.SystemCode, Status: Status, CreatedById: this.CreatedById, CreatedAt: this.CreatedAt, UpdatedById: loginUser.UserId, UpdatedAt: new Date(), }; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = 'Update User System Access'; activity.EntityType = 'UserSystemAccess'; activity.EntityId = this.UserSystemAccessId + ''; activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(entityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return entityValueAfter; } catch (error) { throw error; } }); } static remove(loginUser, dbTransaction, UserSystemAccessId) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const privilegeCode = 'USER_SYSTEM_ACCESS_REMOVE'; const isPrivileged = yield loginUser.checkPrivileges(systemCode, privilegeCode); if (!isPrivileged) { throw new general_1.ClassError('UserSystemAccess', 'UserSystemAccessErrMsg01', 'You do not have permission to access this resource.'); } const userSystemAccess = yield UserSystemAccess._Repository.findOne({ where: { UserSystemAccessId: UserSystemAccessId, }, transaction: dbTransaction, }); if (!userSystemAccess) { throw new general_1.ClassError('UserSystemAccess', 'UserSystemAccessErrMsg02', 'User System Access not Found'); } yield UserSystemAccess._Repository.delete(UserSystemAccessId, dbTransaction); const entityValueBefore = { UserId: userSystemAccess.UserId, SystemCode: userSystemAccess.SystemCode, Status: userSystemAccess.Status, CreatedById: userSystemAccess.CreatedById, CreatedAt: userSystemAccess.CreatedAt, UpdatedById: userSystemAccess.UpdatedById, UpdatedAt: userSystemAccess.UpdatedAt, }; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.DELETE; activity.Description = 'Delete User System Access'; activity.EntityType = 'UserSystemAccess'; activity.EntityId = UserSystemAccessId === null || UserSystemAccessId === void 0 ? void 0 : UserSystemAccessId.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify({}); yield activity.create(loginUser.ObjectId, dbTransaction); } catch (error) { throw error; } }); } } exports.UserSystemAccess = UserSystemAccess; UserSystemAccess._Repository = new user_system_access_repository_1.UserSystemAccessRepository(); UserSystemAccess._UserPrivilegeRepo = new user_privilege_repository_1.UserPrivilegeRepository(); //# sourceMappingURL=user-system-access.js.map