@tomei/sso
Version:
Tomei SSO Package
337 lines • 17.3 kB
JavaScript
"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.SystemPrivilege = void 0;
const general_1 = require("@tomei/general");
const system_repository_1 = require("../system/system.repository");
const system_privilege_repository_1 = require("./system-privilege.repository");
const config_1 = require("@tomei/config");
const system_1 = require("../system/system");
const activity_history_1 = require("@tomei/activity-history");
const sequelize_1 = require("sequelize");
class SystemPrivilege extends general_1.ObjectBase {
get SystemPrivilegeId() {
return this.ObjectId;
}
set SystemPrivilegeId(value) {
this.ObjectId = value;
}
get CreatedAt() {
return this._CreatedAt;
}
get UpdatedAt() {
return this._UpdatedAt;
}
get CreatedById() {
return this._CreatedById;
}
get UpdatedById() {
return this._UpdatedById;
}
constructor(systemPrivilegeAttr) {
super();
this.ObjectType = 'SystemPrivilege';
this.TableName = 'sso_SystemPrivilege';
if (systemPrivilegeAttr) {
this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
this.SystemCode = systemPrivilegeAttr.SystemCode;
this.Description = systemPrivilegeAttr.Description;
this.Status = systemPrivilegeAttr.Status;
this._CreatedById = systemPrivilegeAttr.CreatedById;
this._CreatedAt = systemPrivilegeAttr.CreatedAt;
this._UpdatedById = systemPrivilegeAttr.UpdatedById;
this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
}
}
setAttributes(systemPrivilegeAttr) {
this.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
this.SystemPrivilegeId = systemPrivilegeAttr.SystemPrivilegeId;
this.SystemCode = systemPrivilegeAttr.SystemCode;
this.Description = systemPrivilegeAttr.Description;
this.Status = systemPrivilegeAttr.Status;
this._CreatedAt = systemPrivilegeAttr.CreatedAt;
this._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
this._CreatedById = systemPrivilegeAttr.CreatedById;
this._UpdatedById = systemPrivilegeAttr.UpdatedById;
}
static init(dbTransaction, SystemPrivilegeId) {
return __awaiter(this, void 0, void 0, function* () {
try {
const systemPrivilege = new SystemPrivilege();
if (SystemPrivilegeId) {
const systemPrivilegeAttr = yield this._Repository.findByPk(SystemPrivilegeId, {
transaction: dbTransaction,
});
if (systemPrivilegeAttr) {
systemPrivilege.PrivilegeCode = systemPrivilegeAttr.PrivilegeCode;
systemPrivilege.ObjectId = systemPrivilegeAttr.SystemPrivilegeId;
systemPrivilege.SystemCode = systemPrivilegeAttr.SystemCode;
systemPrivilege.Description = systemPrivilegeAttr.Description;
systemPrivilege.Status = systemPrivilegeAttr.Status;
systemPrivilege._CreatedById = systemPrivilegeAttr.CreatedById;
systemPrivilege._CreatedAt = systemPrivilegeAttr.CreatedAt;
systemPrivilege._UpdatedById = systemPrivilegeAttr.UpdatedById;
systemPrivilege._UpdatedAt = systemPrivilegeAttr.UpdatedAt;
}
else {
throw new general_1.ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg00', 'System Privilege Not Found');
}
}
return systemPrivilege;
}
catch (error) {
throw error;
}
});
}
static create(loginUser, dbTransaction, systemPrivilege) {
return __awaiter(this, void 0, void 0, function* () {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'PRIVILEGE_CREATE');
if (!isPrivileged) {
throw new general_1.ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg01', 'You do not have permission to create system privileges');
}
if (!systemPrivilege.SystemCode) {
throw new general_1.ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg02', 'System Code is required');
}
if (!systemPrivilege.PrivilegeCode) {
throw new general_1.ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg02', 'Privilege Code is required');
}
yield system_1.System.init(dbTransaction, systemPrivilege.SystemCode);
const existingSystemPrivilege = yield this._Repository.findByPk(systemPrivilege.PrivilegeCode, {
transaction: dbTransaction,
});
if (existingSystemPrivilege) {
throw new general_1.ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg03', 'System Privilege already exists');
}
const newSystemPrivilege = new SystemPrivilege();
newSystemPrivilege.ObjectId = newSystemPrivilege.createId();
newSystemPrivilege.PrivilegeCode = systemPrivilege.PrivilegeCode;
newSystemPrivilege.SystemCode = systemPrivilege.SystemCode;
newSystemPrivilege.Description = systemPrivilege.Description;
newSystemPrivilege.Status = 'Active';
newSystemPrivilege._CreatedById = loginUser.UserId;
newSystemPrivilege._UpdatedById = loginUser.UserId;
newSystemPrivilege._CreatedAt = new Date();
newSystemPrivilege._UpdatedAt = new Date();
yield this._Repository.create({
SystemPrivilegeId: newSystemPrivilege.ObjectId,
PrivilegeCode: newSystemPrivilege.PrivilegeCode,
SystemCode: newSystemPrivilege.SystemCode,
Description: newSystemPrivilege.Description,
Status: newSystemPrivilege.Status,
CreatedById: newSystemPrivilege._CreatedById,
UpdatedById: newSystemPrivilege._UpdatedById,
CreatedAt: newSystemPrivilege._CreatedAt,
UpdatedAt: newSystemPrivilege._UpdatedAt,
}, {
transaction: dbTransaction,
});
const EntityValueBefore = {};
const EntityValueAfter = newSystemPrivilege;
const activity = new activity_history_1.Activity();
activity.ActivityId = activity.createId();
activity.Action = activity_history_1.ActionEnum.CREATE;
activity.Description = 'Add System Privilege';
activity.EntityType = 'SystemPrivilege';
activity.EntityId = newSystemPrivilege.SystemPrivilegeId;
activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
yield activity.create(loginUser.ObjectId, dbTransaction);
return newSystemPrivilege;
}
catch (error) {
throw error;
}
});
}
static findAll(loginUser, dbTransaction, page, row, search) {
return __awaiter(this, void 0, void 0, function* () {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'PRIVILEGE_LIST');
if (!isPrivileged) {
throw new general_1.ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg01', 'You do not have permission to list system privileges');
}
const options = {
distinct: true,
order: [['createdAt', 'DESC']],
transaction: dbTransaction,
};
if (page && row) {
const offset = row * (page - 1);
const limit = row;
options.offset = offset;
options.limit = limit;
}
if (search) {
const queryObj = {};
Object.entries(search).forEach(([key, value]) => {
queryObj[key] = {
[sequelize_1.Op.substring]: value,
};
});
options.where = queryObj;
}
const result = yield SystemPrivilege._Repository.findAllWithPagination(options);
const systemPrivileges = [];
for (const systemPrivilegeAttr of result.rows) {
systemPrivileges.push(new SystemPrivilege(systemPrivilegeAttr.get({ plain: true })));
}
return {
count: result.count,
SystemPrivileges: systemPrivileges,
};
}
catch (error) {
throw error;
}
});
}
static loadAllPrivileges(dbTransaction, systemCode) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield system_1.System.init(dbTransaction, systemCode);
config_1.ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
const privilegesConfig = config_1.ComponentConfig.getComponentConfigValue('@tomei/sso', 'privileges');
const systemConfig = config_1.ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
const systemUserId = systemConfig.userId;
const existingSystemPrivileges = yield this._Repository.findAll({
where: {
SystemCode: systemCode,
},
transaction: dbTransaction,
});
const tobeCreatePrivileges = privilegesConfig.filter((privilegeConfig) => !existingSystemPrivileges.find((existingPrivilege) => existingPrivilege.PrivilegeCode === privilegeConfig.privilegeCode));
const np = new SystemPrivilege();
for (const privilegeConfig of tobeCreatePrivileges) {
yield this._Repository.create({
SystemPrivilegeId: np.createId(),
PrivilegeCode: privilegeConfig.privilegeCode,
SystemCode: systemCode,
Description: privilegeConfig.description,
Status: 'Active',
CreatedById: parseInt(systemUserId),
UpdatedById: parseInt(systemUserId),
CreatedAt: new Date(),
UpdatedAt: new Date(),
}, {
transaction: dbTransaction,
});
}
return 'Privileges Loaded';
}
catch (error) {
throw error;
}
});
}
update(loginUser, dbTransaction, privilege) {
return __awaiter(this, void 0, void 0, function* () {
try {
const entityValueBefore = {
SystemPrivilegeId: this.SystemPrivilegeId,
Description: this.Description,
PrivilegeCode: this.PrivilegeCode,
SystemCode: this.SystemCode,
Status: this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
};
yield SystemPrivilege._Repository.update({
PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
Description: privilege.Description || this.Description,
SystemCode: privilege.SystemCode || this.SystemCode,
Status: privilege.Status || this.Status,
UpdatedById: loginUser.UserId,
UpdatedAt: new Date(),
}, {
where: {
SystemPrivilegeId: this.SystemPrivilegeId,
},
transaction: dbTransaction,
});
const entityValueAfter = {
SystemPrivilegeId: this.SystemPrivilegeId,
PrivilegeCode: privilege.PrivilegeCode || this.PrivilegeCode,
Description: privilege.Description || this.Description,
SystemCode: privilege.SystemCode || this.SystemCode,
Status: privilege.Status || this.Status,
CreatedById: this.CreatedById,
CreatedAt: this.CreatedAt,
UpdatedById: this.UpdatedById,
UpdatedAt: this.UpdatedAt,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = activity.createId();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Update System Privilege';
activity.EntityType = 'SystemPrivilege';
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;
}
});
}
delete(dbTransaction, loginUser) {
return __awaiter(this, void 0, void 0, function* () {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'PRIVILEGE_DELETE');
if (!isPrivileged) {
throw new general_1.ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg0X', 'You do not have permission to delete system privileges');
}
if (!this.SystemPrivilegeId) {
throw new general_1.ClassError('SystemPrivilege', 'SystemPrivilegeErrMsg02', 'System Privilege Id is required');
}
yield SystemPrivilege._Repository.delete(this.SystemPrivilegeId, dbTransaction);
const EntityValueBefore = {
SystemPrivilegeId: this.ObjectId,
PrivilegeCode: this.PrivilegeCode,
SystemCode: this.SystemCode,
Description: this.Description,
Status: this.Status,
CreatedById: this._CreatedById,
UpdatedById: this._UpdatedById,
CreatedAt: this._CreatedAt,
UpdatedAt: this._UpdatedAt,
};
const EntityValueAfter = {};
const activity = new activity_history_1.Activity();
activity.ActivityId = activity.createId();
activity.Action = activity_history_1.ActionEnum.DELETE;
activity.Description = 'Delete System Privilege';
activity.EntityType = 'SystemPrivilege';
activity.EntityId = this.SystemPrivilegeId;
activity.EntityValueBefore = JSON.stringify(EntityValueBefore);
activity.EntityValueAfter = JSON.stringify(EntityValueAfter);
yield activity.create(loginUser.ObjectId, dbTransaction);
return this;
}
catch (error) {
throw error;
}
});
}
}
exports.SystemPrivilege = SystemPrivilege;
SystemPrivilege._Repository = new system_privilege_repository_1.SystemPrivilegeRepository();
SystemPrivilege._SystemRepository = new system_repository_1.SystemRepository();
//# sourceMappingURL=system-privilege.js.map