updated throw error

This commit is contained in:
Warunee Tamkoo 2024-12-18 17:47:39 +07:00
parent 19381e99f7
commit 4cf5bb4b2e
2 changed files with 241 additions and 199 deletions

View file

@ -13,6 +13,8 @@ import {
} from "tsoa"; } from "tsoa";
import axios from "axios"; import axios from "axios";
import { addLogSequence } from "./utils"; import { addLogSequence } from "./utils";
import HttpError from "./http-error";
import HttpStatus from "./http-status";
class CallAPI { class CallAPI {
//Get //Get
@ -49,7 +51,11 @@ class CallAPI {
response: JSON.stringify(error), response: JSON.stringify(error),
}, },
}); });
throw error; if (error instanceof Error) {
throw new HttpError(HttpStatus.FORBIDDEN, error.message);
} else {
throw new HttpError(HttpStatus.FORBIDDEN, "Something went wrong!");
}
} }
} }
//Post //Post
@ -88,7 +94,11 @@ class CallAPI {
response: JSON.stringify(error), response: JSON.stringify(error),
}, },
}); });
throw error; if (error instanceof Error) {
throw new HttpError(HttpStatus.FORBIDDEN, error.message);
} else {
throw new HttpError(HttpStatus.FORBIDDEN, "Something went wrong!");
}
} }
} }
} }

View file

@ -1,207 +1,239 @@
import axios from "axios" import axios from "axios";
import { RequestWithUser } from "../middlewares/user" import { RequestWithUser } from "../middlewares/user";
import CallAPI from "./call-api" import CallAPI from "./call-api";
import HttpError from "./http-error" import HttpError from "./http-error";
import HttpStatus from "./http-status" import HttpStatus from "./http-status";
class CheckAuth { class CheckAuth {
public async Permission(req: RequestWithUser, system: string, action: string) { public async Permission(req: RequestWithUser, system: string, action: string) {
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) { if (
return null req.headers.hasOwnProperty("api_key") &&
} req.headers["api_key"] &&
return await new CallAPI() req.headers["api_key"] == process.env.API_KEY
.GetData(req, "/org/permission") ) {
.then(x => { return null;
let permission = false }
let role = x.roles.find((x: any) => x.authSysId == system) return await new CallAPI()
if (!role) throw "ไม่มีสิทธิ์เข้าระบบ" .GetData(req, "/org/permission")
if (role.attrOwnership == "OWNER") return "OWNER" .then((x) => {
if (action.trim().toLocaleUpperCase() == "CREATE") permission = role.attrIsCreate let permission = false;
if (action.trim().toLocaleUpperCase() == "DELETE") permission = role.attrIsDelete let role = x.roles.find((x: any) => x.authSysId == system);
if (action.trim().toLocaleUpperCase() == "GET") permission = role.attrIsGet if (!role) throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์เข้าระบบ");
if (action.trim().toLocaleUpperCase() == "LIST") permission = role.attrIsList if (role.attrOwnership == "OWNER") return "OWNER";
if (action.trim().toLocaleUpperCase() == "UPDATE") permission = role.attrIsUpdate if (action.trim().toLocaleUpperCase() == "CREATE") permission = role.attrIsCreate;
if (permission == false) throw "ไม่มีสิทธิ์ใช้งานระบบนี้" if (action.trim().toLocaleUpperCase() == "DELETE") permission = role.attrIsDelete;
return role.attrPrivilege if (action.trim().toLocaleUpperCase() == "GET") permission = role.attrIsGet;
}) if (action.trim().toLocaleUpperCase() == "LIST") permission = role.attrIsList;
.catch(x => { if (action.trim().toLocaleUpperCase() == "UPDATE") permission = role.attrIsUpdate;
if (x.status != undefined) { if (permission == false)
throw new HttpError(x.status, x.message) throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์ใช้งานระบบนี้");
} else { return role.attrPrivilege;
throw new HttpError(HttpStatus.FORBIDDEN, x) })
} .catch((x) => {
}) if (x.status != undefined) {
} throw new HttpError(x.status, x.message);
public async PermissionOrg(req: RequestWithUser, system: string, action: string) { } else {
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) { throw new HttpError(HttpStatus.FORBIDDEN, x);
return { }
root: null, });
child1: null, }
child2: null, public async PermissionOrg(req: RequestWithUser, system: string, action: string) {
child3: null, if (
child4: null, req.headers.hasOwnProperty("api_key") &&
} req.headers["api_key"] &&
} req.headers["api_key"] == process.env.API_KEY
return await new CallAPI() ) {
.GetData(req, `/org/permission/org/${system}/${action}`) return {
.then(async x => { root: null,
let privilege = x.privilege child1: null,
child2: null,
child3: null,
child4: null,
};
}
return await new CallAPI()
.GetData(req, `/org/permission/org/${system}/${action}`)
.then(async (x) => {
let privilege = x.privilege;
let data: any = { let data: any = {
root: [null], root: [null],
child1: [null], child1: [null],
child2: [null], child2: [null],
child3: [null], child3: [null],
child4: [null], child4: [null],
privilege: [null], privilege: [null],
} };
let node = 4 let node = 4;
if (x.orgChild1Id == null) { if (x.orgChild1Id == null) {
node = 0 node = 0;
} else if (x.orgChild2Id == null) { } else if (x.orgChild2Id == null) {
node = 1 node = 1;
} else if (x.orgChild3Id == null) { } else if (x.orgChild3Id == null) {
node = 2 node = 2;
} else if (x.orgChild4Id == null) { } else if (x.orgChild4Id == null) {
node = 3 node = 3;
} }
if (privilege == "OWNER") { if (privilege == "OWNER") {
data = { data = {
root: null, root: null,
child1: null, child1: null,
child2: null, child2: null,
child3: null, child3: null,
child4: null, child4: null,
privilege: "OWNER", privilege: "OWNER",
} };
} else if (privilege == "ROOT") { } else if (privilege == "ROOT") {
data = { data = {
root: [x.orgRootId], root: [x.orgRootId],
child1: null, child1: null,
child2: null, child2: null,
child3: null, child3: null,
child4: null, child4: null,
privilege: "ROOT", privilege: "ROOT",
} };
} else if (privilege == "CHILD") { } else if (privilege == "CHILD") {
data = { data = {
root: node >= 0 ? [x.orgRootId] : null, root: node >= 0 ? [x.orgRootId] : null,
child1: node >= 1 ? [x.orgChild1Id] : null, child1: node >= 1 ? [x.orgChild1Id] : null,
child2: node >= 2 ? [x.orgChild2Id] : null, child2: node >= 2 ? [x.orgChild2Id] : null,
child3: node >= 3 ? [x.orgChild3Id] : null, child3: node >= 3 ? [x.orgChild3Id] : null,
child4: node >= 4 ? [x.orgChild4Id] : null, child4: node >= 4 ? [x.orgChild4Id] : null,
privilege: "CHILD", privilege: "CHILD",
} };
} else if (privilege == "NORMAL") { } else if (privilege == "NORMAL") {
data = { data = {
root: [x.orgRootId], root: [x.orgRootId],
child1: [x.orgChild1Id], child1: [x.orgChild1Id],
child2: [x.orgChild2Id], child2: [x.orgChild2Id],
child3: [x.orgChild3Id], child3: [x.orgChild3Id],
child4: [x.orgChild4Id], child4: [x.orgChild4Id],
privilege: "NORMAL", privilege: "NORMAL",
} };
} else if (privilege == "SPECIFIC") { } else if (privilege == "SPECIFIC") {
} }
return data return data;
}) })
.catch(x => { .catch((x) => {
if (x.status != undefined) { if (x.status != undefined) {
throw new HttpError(x.status, x.message) throw new HttpError(x.status, x.message);
} else { } else {
throw new HttpError(HttpStatus.FORBIDDEN, x) throw new HttpError(HttpStatus.FORBIDDEN, x);
} }
}) });
} }
public async PermissionOrgByUser(req: RequestWithUser, system: string, action: string, profileId: string) { public async PermissionOrgByUser(
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) { req: RequestWithUser,
return true system: string,
} action: string,
return await new CallAPI() profileId: string,
.GetData(req, `/org/permission/user/${system}/${action}/${profileId}`) ) {
.then(async x => { if (
let org = x.org req.headers.hasOwnProperty("api_key") &&
req.headers["api_key"] &&
req.headers["api_key"] == process.env.API_KEY
) {
return true;
}
return await new CallAPI()
.GetData(req, `/org/permission/user/${system}/${action}/${profileId}`)
.then(async (x) => {
let org = x.org;
if (org.root != null) if (x.orgRootId != org.root[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (org.root != null)
if (org.child1 != null) if (x.orgChild1Id != org.child1[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (x.orgRootId != org.root[0])
if (org.child2 != null) if (x.orgChild2Id != org.child2[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์เข้าถึงข้อมูล");
if (org.child3 != null) if (x.orgChild3Id != org.child3[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (org.child1 != null)
if (org.child4 != null) if (x.orgChild4Id != org.child4[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล" if (x.orgChild1Id != org.child1[0])
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์เข้าถึงข้อมูล");
if (org.child2 != null)
if (x.orgChild2Id != org.child2[0])
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์เข้าถึงข้อมูล");
if (org.child3 != null)
if (x.orgChild3Id != org.child3[0])
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์เข้าถึงข้อมูล");
if (org.child4 != null)
if (x.orgChild4Id != org.child4[0])
throw new HttpError(HttpStatus.FORBIDDEN, "ไม่มีสิทธิ์เข้าถึงข้อมูล");
return true return true;
}) })
.catch(x => { .catch((x) => {
if (x.status != undefined) { if (x.status != undefined) {
throw new HttpError(x.status, x.message) throw new HttpError(x.status, x.message);
} else { } else {
throw new HttpError(HttpStatus.FORBIDDEN, x) throw new HttpError(HttpStatus.FORBIDDEN, x);
} }
}) });
} }
public async Workflow(req: RequestWithUser, id: string, sysName: string) { public async Workflow(req: RequestWithUser, id: string, sysName: string) {
if (req.headers.hasOwnProperty("api_key") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) { if (
return null req.headers.hasOwnProperty("api_key") &&
} req.headers["api_key"] &&
return await new CallAPI() req.headers["api_key"] == process.env.API_KEY
.PostData(req, "/org/workflow/keycloak/isofficer", { ) {
refId: id, return null;
sysName: sysName, }
}) return await new CallAPI()
.then(x => { .PostData(req, "/org/workflow/keycloak/isofficer", {
return true refId: id,
}) sysName: sysName,
.catch(x => { })
return false .then((x) => {
}) return true;
} })
public async PermissionCreate(req: RequestWithUser, system: string) { .catch((x) => {
return await this.Permission(req, system, "CREATE") return false;
} });
public async PermissionDelete(req: RequestWithUser, system: string) { }
return await this.Permission(req, system, "DELETE") public async PermissionCreate(req: RequestWithUser, system: string) {
} return await this.Permission(req, system, "CREATE");
public async PermissionGet(req: RequestWithUser, system: string) { }
return await this.Permission(req, system, "GET") public async PermissionDelete(req: RequestWithUser, system: string) {
} return await this.Permission(req, system, "DELETE");
public async PermissionList(req: RequestWithUser, system: string) { }
return await this.Permission(req, system, "LIST") public async PermissionGet(req: RequestWithUser, system: string) {
} return await this.Permission(req, system, "GET");
public async PermissionUpdate(req: RequestWithUser, system: string) { }
return await this.Permission(req, system, "UPDATE") public async PermissionList(req: RequestWithUser, system: string) {
} return await this.Permission(req, system, "LIST");
}
public async PermissionUpdate(req: RequestWithUser, system: string) {
return await this.Permission(req, system, "UPDATE");
}
public async PermissionOrgCreate(req: RequestWithUser, system: string) { public async PermissionOrgCreate(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "CREATE") return await this.PermissionOrg(req, system, "CREATE");
} }
public async PermissionOrgDelete(req: RequestWithUser, system: string) { public async PermissionOrgDelete(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "DELETE") return await this.PermissionOrg(req, system, "DELETE");
} }
public async PermissionOrgGet(req: RequestWithUser, system: string) { public async PermissionOrgGet(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "GET") return await this.PermissionOrg(req, system, "GET");
} }
public async PermissionOrgList(req: RequestWithUser, system: string) { public async PermissionOrgList(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "LIST") return await this.PermissionOrg(req, system, "LIST");
} }
public async PermissionOrgUpdate(req: RequestWithUser, system: string) { public async PermissionOrgUpdate(req: RequestWithUser, system: string) {
return await this.PermissionOrg(req, system, "UPDATE") return await this.PermissionOrg(req, system, "UPDATE");
} }
public async PermissionOrgUserCreate(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserCreate(req: RequestWithUser, system: string, profileId: string) {
return await this.PermissionOrgByUser(req, system, "CREATE", profileId) return await this.PermissionOrgByUser(req, system, "CREATE", profileId);
} }
public async PermissionOrgUserDelete(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserDelete(req: RequestWithUser, system: string, profileId: string) {
return await this.PermissionOrgByUser(req, system, "DELETE", profileId) return await this.PermissionOrgByUser(req, system, "DELETE", profileId);
} }
public async PermissionOrgUserGet(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserGet(req: RequestWithUser, system: string, profileId: string) {
return await this.PermissionOrgByUser(req, system, "GET", profileId) return await this.PermissionOrgByUser(req, system, "GET", profileId);
} }
public async PermissionOrgUserList(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserList(req: RequestWithUser, system: string, profileId: string) {
return await this.PermissionOrgByUser(req, system, "LIST", profileId) return await this.PermissionOrgByUser(req, system, "LIST", profileId);
} }
public async PermissionOrgUserUpdate(req: RequestWithUser, system: string, profileId: string) { public async PermissionOrgUserUpdate(req: RequestWithUser, system: string, profileId: string) {
return await this.PermissionOrgByUser(req, system, "UPDATE", profileId) return await this.PermissionOrgByUser(req, system, "UPDATE", profileId);
} }
} }
export default CheckAuth export default CheckAuth;