UNPKG

@tomei/sso

Version:
408 lines 20.6 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.UserPrivilege = void 0; const general_1 = require("@tomei/general"); const user_privilege_repository_1 = require("./user-privilege.repository"); const config_1 = require("@tomei/config"); const system_privilege_entity_1 = require("../../models/system-privilege.entity"); const system_entity_1 = require("../../models/system.entity"); const user_group_repository_1 = require("../user-group/user-group.repository"); const group_entity_1 = require("../../models/group.entity"); const user_entity_1 = require("../../models/user.entity"); const group_privilege_repository_1 = require("../group-privilege/group-privilege.repository"); const system_privilege_repository_1 = require("../system-privilege/system-privilege.repository"); const sequelize_1 = require("sequelize"); const activity_history_1 = require("@tomei/activity-history"); class UserPrivilege extends general_1.ObjectBase { get CreatedById() { return this._CreatedById; } get UpdatedById() { return this._UpdatedById; } get CreatedAt() { return this._CreatedAt; } get UpdatedAt() { return this._UpdatedAt; } constructor(userPrivilegeAttr) { super(); this.TableName = 'sso_UserPrivilege'; this.ObjectType = 'UserPrivilege'; if (userPrivilegeAttr) { this.UserPrivilegeId = userPrivilegeAttr.UserPrivilegeId; this.UserId = userPrivilegeAttr.UserId; this.SystemPrivilegeId = userPrivilegeAttr.SystemPrivilegeId; this.Status = userPrivilegeAttr.Status; this._CreatedById = userPrivilegeAttr.CreatedById; this._UpdatedById = userPrivilegeAttr.UpdatedById; this._CreatedAt = userPrivilegeAttr.CreatedAt; this._UpdatedAt = userPrivilegeAttr.UpdatedAt; } } static init(dbTransaction, UserPrivilegeId) { return __awaiter(this, void 0, void 0, function* () { try { let userPrivilege = new UserPrivilege(); if (UserPrivilegeId) { const userPrivilegeAttr = yield this._Repository.findOne({ where: { UserPrivilegeId }, transaction: dbTransaction, }); if (userPrivilegeAttr) { userPrivilege = new UserPrivilege(userPrivilegeAttr); } else { throw new general_1.ClassError('UserPrivilege', 'UserPrivilegeErrMsg00', 'UserPrivilege not found'); } } return userPrivilege; } 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_PRIVILEGE_LIST'; const isPrivileged = yield loginUser.checkPrivileges(systemCode, privilegeCode); if (!isPrivileged) { throw new general_1.ClassError('UserPrivilege', 'UserPrivilegeErrMsg01', '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_privilege_entity_1.default, attributes: ['PrivilegeCode'], include: [ { model: system_entity_1.default, attributes: ['Name'], }, ], }, { model: user_entity_1.default, as: 'CreatedByUser', attributes: ['FullName'], }, { model: user_entity_1.default, as: 'UpdatedByUser', attributes: ['FullName'], }, ], }; const { count, rows } = yield this._Repository.findAllWithPagination(options); return { records: rows.map((record) => { return { UserPrivilegeId: record.UserPrivilegeId, SystemPrivilegeId: record.SystemPrivilegeId, PrivilegeCode: record.Privilege.PrivilegeCode, SystemName: record.Privilege.System.Name, Status: record.Status, CreatedBy: record.CreatedByUser.FullName, CreatedAt: record.CreatedAt, UpdatedBy: record.UpdatedByUser.FullName, UpdatedAt: record.UpdatedAt, }; }), pagination: { currentPage: pagination.page, pageSize: pagination.limit, totalRecords: count, }, }; } catch (error) { throw error; } }); } static findAllInheritedPrivileges(UserId, loginUser, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const privilegeCode = 'USER_PRIVILEGE_LIST'; const isPrivileged = yield loginUser.checkPrivileges(systemCode, privilegeCode); if (!isPrivileged) { throw new general_1.ClassError('UserPrivilege', 'UserPrivilegeErrMsg01', 'You do not have permission to access this resource.'); } const userGroups = yield UserPrivilege._UserGroupRepository.findAll({ where: { UserId, }, include: [ { model: group_entity_1.default, attributes: ['GroupCode', 'Name', 'InheritParentPrivilegeYN'], }, ], transaction: dbTransaction, }); const listOfGroups = userGroups.map((groups) => { let inheritPrivilegeYN = groups.InheritGroupPrivilegeYN; if (inheritPrivilegeYN !== 'Y') { inheritPrivilegeYN = 'N'; } return { UserGroupId: groups.UserGroupId, GroupCode: groups.GroupCode, GroupName: groups.Group.Name, InheritPrivilegeYN: inheritPrivilegeYN, Status: groups.Status, }; }); const userGroupPrivilege = []; for (let i = 0; i < listOfGroups.length; i++) { const group = yield listOfGroups[i]; const data = { UserGroupId: group.UserGroupId, GroupCode: group.GroupCode, GroupName: group.GroupName, InheritPrivilegeYN: group.InheritPrivilegeYN, systems: [], }; if (group.InheritPrivilegeYN === 'Y') { if (group.Status === 'Active') { const options = { where: { GroupCode: group.GroupCode, Status: 'Active', }, transaction: dbTransaction, include: [ { model: system_privilege_entity_1.default, attributes: ['PrivilegeCode'], include: [ { model: system_entity_1.default, attributes: ['Name'], }, ], }, { model: user_entity_1.default, as: 'CreatedByUser', attributes: ['FullName'], }, { model: user_entity_1.default, as: 'UpdatedByUser', attributes: ['FullName'], }, ], }; const systemPrivilege = yield this._GroupPrivilegeRepository.findAll(options); const privilegeDetails = systemPrivilege.map((record) => { return { GroupPrivilegeId: record.GroupPrivilegeId, SystemPrivilegeId: record.SystemPrivilegeId, PrivilegeCode: record.Privilege.PrivilegeCode, Status: record.Status, CreatedBy: record.CreatedByUser.FullName, CreatedAt: record.CreatedAt, UpdatedBy: record.UpdatedByUser.FullName, UpdatedAt: record.UpdatedAt, }; }); data.systems = privilegeDetails; } } userGroupPrivilege.push(data); } return userGroupPrivilege; } catch (error) { throw error; } }); } static assignPrivileges(loginUser, dbTransaction, UserId, SystemPrivilegeId, Status) { return __awaiter(this, void 0, void 0, function* () { var _a; try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const privilegeCode = 'USER_PRIVILEGE_CREATE'; const isPrivileged = yield loginUser.checkPrivileges(systemCode, privilegeCode); if (!isPrivileged) { throw new general_1.ClassError('UserSystemPrivilege', 'UserSystemPrivilegeErrMsg01', 'You do not have permission to access this resource.'); } const isExist = yield UserPrivilege._SystemPrivilegeRepository.findAll({ where: { SystemPrivilegeId: SystemPrivilegeId }, transaction: dbTransaction, }); if ((isExist === null || isExist === void 0 ? void 0 : isExist.length) < 1) { throw new general_1.ClassError('UserSystemPrivilege', 'UserSystemPrivilegeErrMsg02', "system privileges don't exist"); } const isUserAlreadyAssign = yield UserPrivilege._Repository.findAll({ where: { [sequelize_1.Op.and]: [ { UserId: UserId }, { SystemPrivilegeId: SystemPrivilegeId }, ], }, transaction: dbTransaction, }); if ((isUserAlreadyAssign === null || isUserAlreadyAssign === void 0 ? void 0 : isUserAlreadyAssign.length) > 0) { throw new general_1.ClassError('UserSystemPrivilege', 'UserSystemPrivilegeErrMsg03', 'User already have access to this privilege'); } const newUserPrivilege = new UserPrivilege(); newUserPrivilege.UserId = parseInt(UserId); newUserPrivilege.SystemPrivilegeId = SystemPrivilegeId; newUserPrivilege.Status = Status; newUserPrivilege._CreatedById = loginUser.UserId; newUserPrivilege._CreatedAt = new Date(); newUserPrivilege._UpdatedById = loginUser.UserId; newUserPrivilege._UpdatedAt = new Date(); const payload = { UserId: newUserPrivilege.UserId, SystemPrivilegeId: newUserPrivilege.SystemPrivilegeId, Status: newUserPrivilege.Status, CreatedById: newUserPrivilege.CreatedById, CreatedAt: newUserPrivilege.CreatedAt, UpdatedById: newUserPrivilege.UpdatedById, UpdatedAt: newUserPrivilege.UpdatedAt, }; const userPrivilege = yield UserPrivilege._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 Privilege'; activity.EntityType = 'UserPrivilege'; activity.EntityId = (_a = userPrivilege.UserPrivilegeId) === null || _a === void 0 ? void 0 : _a.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(payload); yield activity.create(loginUser.ObjectId, dbTransaction); newUserPrivilege.UserPrivilegeId = userPrivilege.UserPrivilegeId; return newUserPrivilege; } catch (error) { throw error; } }); } update(loginUser, dbTransaction, Status) { return __awaiter(this, void 0, void 0, function* () { try { const entityValueBefore = { UserPrivilegeId: this.UserPrivilegeId, UserId: this.UserId, SystemPrivilegeId: this.SystemPrivilegeId, Status: this.Status, CreatedById: this.CreatedById, CreatedAt: this.CreatedAt, UpdatedById: this.UpdatedById, UpdatedAt: this.UpdatedAt, }; yield UserPrivilege._Repository.update({ Status: Status, UpdatedById: loginUser.UserId, UpdatedAt: new Date(), }, { where: { UserPrivilegeId: this.UserPrivilegeId, }, transaction: dbTransaction, }); const entityValueAfter = { UserPrivilegeId: this.UserPrivilegeId, UserId: this.UserId, SystemPrivilegeId: this.SystemPrivilegeId, 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 Privilege'; activity.EntityType = 'UserPrivilege'; activity.EntityId = this.SystemPrivilegeId + ''; 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, UserPrivilegeId) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const privilegeCode = 'USER_PRIVILEGE_REMOVE'; const isPrivileged = yield loginUser.checkPrivileges(systemCode, privilegeCode); if (!isPrivileged) { throw new general_1.ClassError('UserSystemPrivilege', 'UserSystemPrivilegeErrMsg01', 'You do not have permission to access this resource.'); } const userPrivilege = yield UserPrivilege._Repository.findOne({ where: { UserPrivilegeId: UserPrivilegeId, }, transaction: dbTransaction, }); if (!userPrivilege) { throw new general_1.ClassError('UserSystemPrivilege', 'UserSystemPrivilegeErrMsg01', 'User Privilege not Found'); } yield UserPrivilege._Repository.delete(UserPrivilegeId, dbTransaction); const entityValueBefore = { UserId: userPrivilege.UserId, SystemPrivilegeId: userPrivilege.SystemPrivilegeId, Status: userPrivilege.Status, CreatedById: userPrivilege.CreatedById, CreatedAt: userPrivilege.CreatedAt, UpdatedById: userPrivilege.UpdatedById, UpdatedAt: userPrivilege.UpdatedAt, }; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.DELETE; activity.Description = 'Delete User Privilege'; activity.EntityType = 'UserPrivilege'; activity.EntityId = UserPrivilegeId === null || UserPrivilegeId === void 0 ? void 0 : UserPrivilegeId.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify({}); yield activity.create(loginUser.ObjectId, dbTransaction); } catch (error) { throw error; } }); } } exports.UserPrivilege = UserPrivilege; UserPrivilege._Repository = new user_privilege_repository_1.UserPrivilegeRepository(); UserPrivilege._UserGroupRepository = new user_group_repository_1.UserGroupRepository(); UserPrivilege._GroupPrivilegeRepository = new group_privilege_repository_1.GroupPrivilegeRepository(); UserPrivilege._SystemPrivilegeRepository = new system_privilege_repository_1.SystemPrivilegeRepository(); //# sourceMappingURL=user-privilege.js.map