no message

This commit is contained in:
DESKTOP-2S5P7D1\Windows 10 2024-10-21 16:08:01 +07:00
parent 2e2dc56f87
commit 00dea2d57e

View file

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