add permission brother
This commit is contained in:
parent
5c274cf916
commit
84dbbf67c3
2 changed files with 426 additions and 278 deletions
|
|
@ -28,7 +28,7 @@ import CallAPI from "../interfaces/call-api";
|
|||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง"
|
||||
)
|
||||
export class PersonalController extends Controller {
|
||||
private personalRepository = AppDataSource.getRepository(Personal);
|
||||
|
|
@ -41,7 +41,10 @@ export class PersonalController extends Controller {
|
|||
*
|
||||
*/
|
||||
@Post("add")
|
||||
async AddPersonal(@Body() requestBody: PostPersonal, @Request() request: RequestWithUser) {
|
||||
async AddPersonal(
|
||||
@Body() requestBody: PostPersonal,
|
||||
@Request() request: RequestWithUser
|
||||
) {
|
||||
try {
|
||||
await new permission().PermissionCreate(request, "SYS_PROBATION");
|
||||
|
||||
|
|
@ -51,19 +54,31 @@ export class PersonalController extends Controller {
|
|||
if (checkPersonal > 0) {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.BAD_REQUEST,
|
||||
"ผู้ทดลองปฏิบัติหน้าที่ราชการนี้มีอยู่แล้ว",
|
||||
"ผู้ทดลองปฏิบัติหน้าที่ราชการนี้มีอยู่แล้ว"
|
||||
);
|
||||
}
|
||||
|
||||
let organization = await (requestBody.orgChild4Name ? requestBody.orgChild4Name + "\n" : "");
|
||||
organization += await (requestBody.orgChild3Name ? requestBody.orgChild3Name + "\n" : "");
|
||||
organization += await (requestBody.orgChild2Name ? requestBody.orgChild2Name + "\n" : "");
|
||||
organization += await (requestBody.orgChild1Name ? requestBody.orgChild1Name + "\n" : "");
|
||||
organization += await (requestBody.orgRootName ? requestBody.orgRootName : "");
|
||||
let organization = await (requestBody.orgChild4Name
|
||||
? requestBody.orgChild4Name + "\n"
|
||||
: "");
|
||||
organization += await (requestBody.orgChild3Name
|
||||
? requestBody.orgChild3Name + "\n"
|
||||
: "");
|
||||
organization += await (requestBody.orgChild2Name
|
||||
? requestBody.orgChild2Name + "\n"
|
||||
: "");
|
||||
organization += await (requestBody.orgChild1Name
|
||||
? requestBody.orgChild1Name + "\n"
|
||||
: "");
|
||||
organization += await (requestBody.orgRootName
|
||||
? requestBody.orgRootName
|
||||
: "");
|
||||
|
||||
const personalData = Object.assign(new Personal());
|
||||
personalData.personal_id = requestBody.id;
|
||||
personalData.order_number = requestBody.order_number ? requestBody.order_number : "";
|
||||
personalData.order_number = requestBody.order_number
|
||||
? requestBody.order_number
|
||||
: "";
|
||||
personalData.probation_status = 1;
|
||||
personalData.createdUserId = request.user.sub;
|
||||
personalData.createdFullName = request.user.name;
|
||||
|
|
@ -75,8 +90,12 @@ export class PersonalController extends Controller {
|
|||
personalData.firstName = requestBody.firstName;
|
||||
personalData.lastName = requestBody.lastName;
|
||||
personalData.isProbation = requestBody.isProbation ? 1 : 0;
|
||||
personalData.positionLevelName = requestBody.posLevelName ? requestBody.posLevelName : "";
|
||||
personalData.positionName = requestBody.position ? requestBody.position : "";
|
||||
personalData.positionLevelName = requestBody.posLevelName
|
||||
? requestBody.posLevelName
|
||||
: "";
|
||||
personalData.positionName = requestBody.position
|
||||
? requestBody.position
|
||||
: "";
|
||||
personalData.positionLineName = requestBody.posLineName;
|
||||
personalData.positionTypeName = requestBody.posTypeName;
|
||||
personalData.posNo = requestBody.posNo ? requestBody.posNo : "";
|
||||
|
|
@ -95,7 +114,8 @@ export class PersonalController extends Controller {
|
|||
personalData.child3Dna = requestBody.orgChild3Dna;
|
||||
personalData.child4Dna = requestBody.orgChild4Dna;
|
||||
|
||||
const getFieldValue = (field: string | null | undefined): string => field ?? "";
|
||||
const getFieldValue = (field: string | null | undefined): string =>
|
||||
field ?? "";
|
||||
personalData.orgChild1Name = getFieldValue(requestBody.orgChild1Name);
|
||||
personalData.orgChild2Name = getFieldValue(requestBody.orgChild2Name);
|
||||
personalData.orgChild3Name = getFieldValue(requestBody.orgChild3Name);
|
||||
|
|
@ -111,7 +131,11 @@ export class PersonalController extends Controller {
|
|||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
|
||||
} else
|
||||
throw new HttpError(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
error.message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -129,13 +153,16 @@ export class PersonalController extends Controller {
|
|||
@Query("page") page: number = 1,
|
||||
@Query("pageSize") pageSize: number = 10,
|
||||
@Query("sortBy") sortBy?: string,
|
||||
@Query("descending") descending?: boolean,
|
||||
@Query("descending") descending?: boolean
|
||||
) {
|
||||
try {
|
||||
// await new permission().PermissionList(request, "SYS_PROBATION");
|
||||
// const _data = await new permission().PermissionOrgList(request, "SYS_PROBATION");
|
||||
|
||||
let _data = await new permission().PermissionOrgList(request, "SYS_PROBATION");
|
||||
let _data = await new permission().PermissionOrgList(
|
||||
request,
|
||||
"SYS_PROBATION"
|
||||
);
|
||||
await new CallAPI()
|
||||
.PostData(request, "/org/finddna", _data)
|
||||
.then((x) => {
|
||||
|
|
@ -208,17 +235,17 @@ export class PersonalController extends Controller {
|
|||
: "1=1",
|
||||
{
|
||||
root: _data.root,
|
||||
},
|
||||
}
|
||||
)
|
||||
.andWhere(
|
||||
_data.child1 != undefined && _data.child1 != null
|
||||
? _data.child1[0] != null
|
||||
? `personal.child1Dna IN (:...child1)`
|
||||
: `personal.child1Dna is null`
|
||||
: `personal.child1Dna is ${_data.privilege == "PARENT" ? "not null" : "null"}`
|
||||
: "1=1",
|
||||
{
|
||||
child1: _data.child1,
|
||||
},
|
||||
}
|
||||
)
|
||||
.andWhere(
|
||||
_data.child2 != undefined && _data.child2 != null
|
||||
|
|
@ -228,7 +255,7 @@ export class PersonalController extends Controller {
|
|||
: "1=1",
|
||||
{
|
||||
child2: _data.child2,
|
||||
},
|
||||
}
|
||||
)
|
||||
.andWhere(
|
||||
_data.child3 != undefined && _data.child3 != null
|
||||
|
|
@ -238,7 +265,7 @@ export class PersonalController extends Controller {
|
|||
: "1=1",
|
||||
{
|
||||
child3: _data.child3,
|
||||
},
|
||||
}
|
||||
)
|
||||
.andWhere(
|
||||
_data.child4 != undefined && _data.child4 != null
|
||||
|
|
@ -248,7 +275,7 @@ export class PersonalController extends Controller {
|
|||
: "1=1",
|
||||
{
|
||||
child4: _data.child4,
|
||||
},
|
||||
}
|
||||
)
|
||||
.andWhere(
|
||||
new Brackets((qb) => {
|
||||
|
|
@ -258,36 +285,60 @@ export class PersonalController extends Controller {
|
|||
: "1=1",
|
||||
{
|
||||
keyword: `%${searchKeyword}%`,
|
||||
},
|
||||
}
|
||||
);
|
||||
qb.orWhere(searchKeyword ? `positionName like '%${keyword}%'` : "1=1", {
|
||||
keyword: `%${searchKeyword}%`,
|
||||
});
|
||||
qb.orWhere(searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1", {
|
||||
keyword: `%${searchKeyword}%`,
|
||||
});
|
||||
qb.orWhere(searchKeyword ? `organization like '%${keyword}%'` : "1=1", {
|
||||
keyword: `%${searchKeyword}%`,
|
||||
});
|
||||
qb.orWhere(searchKeyword ? `order_number like '%${keyword}%'` : "1=1", {
|
||||
keyword: `%${searchKeyword}%`,
|
||||
});
|
||||
}),
|
||||
qb.orWhere(
|
||||
searchKeyword ? `positionName like '%${keyword}%'` : "1=1",
|
||||
{
|
||||
keyword: `%${searchKeyword}%`,
|
||||
}
|
||||
);
|
||||
qb.orWhere(
|
||||
searchKeyword ? `positionLevelName like '%${keyword}%'` : "1=1",
|
||||
{
|
||||
keyword: `%${searchKeyword}%`,
|
||||
}
|
||||
);
|
||||
qb.orWhere(
|
||||
searchKeyword ? `organization like '%${keyword}%'` : "1=1",
|
||||
{
|
||||
keyword: `%${searchKeyword}%`,
|
||||
}
|
||||
);
|
||||
qb.orWhere(
|
||||
searchKeyword ? `order_number like '%${keyword}%'` : "1=1",
|
||||
{
|
||||
keyword: `%${searchKeyword}%`,
|
||||
}
|
||||
);
|
||||
})
|
||||
);
|
||||
if (sortBy) {
|
||||
if (sortBy === "position_line") {
|
||||
query = query.orderBy(`personal.positionName`, descending ? "DESC" : "ASC");
|
||||
query = query.orderBy(
|
||||
`personal.positionName`,
|
||||
descending ? "DESC" : "ASC"
|
||||
);
|
||||
} else if (sortBy === "position_level") {
|
||||
query = query.orderBy(`personal.positionLevelName`, descending ? "DESC" : "ASC");
|
||||
query = query.orderBy(
|
||||
`personal.positionLevelName`,
|
||||
descending ? "DESC" : "ASC"
|
||||
);
|
||||
} else if (sortBy === "position_type") {
|
||||
query = query.orderBy(`personal.positionTypeName`, descending ? "DESC" : "ASC");
|
||||
query = query.orderBy(
|
||||
`personal.positionTypeName`,
|
||||
descending ? "DESC" : "ASC"
|
||||
);
|
||||
} else if (sortBy === "name") {
|
||||
query = query
|
||||
.orderBy(`personal.prefixName`, descending ? "DESC" : "ASC")
|
||||
.addOrderBy(`personal.firstName`, descending ? "DESC" : "ASC")
|
||||
.addOrderBy(`personal.lastName`, descending ? "DESC" : "ASC");
|
||||
} else {
|
||||
query = query.orderBy(`personal.${sortBy}`, descending ? "DESC" : "ASC");
|
||||
query = query.orderBy(
|
||||
`personal.${sortBy}`,
|
||||
descending ? "DESC" : "ASC"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
query = query.orderBy("updatedAt", "DESC");
|
||||
|
|
@ -299,7 +350,10 @@ export class PersonalController extends Controller {
|
|||
.getManyAndCount();
|
||||
|
||||
if (!lists) {
|
||||
throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, "ไม่สามารถแสดงข้อมูลได้");
|
||||
throw new HttpError(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"ไม่สามารถแสดงข้อมูลได้"
|
||||
);
|
||||
}
|
||||
|
||||
let result: any = [];
|
||||
|
|
@ -312,7 +366,8 @@ export class PersonalController extends Controller {
|
|||
await result.push({
|
||||
personal_id: lists[i].personal_id,
|
||||
ordering: i + 1,
|
||||
name: lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName,
|
||||
name:
|
||||
lists[i].prefixName + lists[i].firstName + " " + lists[i].lastName,
|
||||
prefixName: lists[i].prefixName,
|
||||
firstName: lists[i].firstName,
|
||||
lastName: lists[i].lastName,
|
||||
|
|
@ -357,7 +412,11 @@ export class PersonalController extends Controller {
|
|||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
|
||||
} else
|
||||
throw new HttpError(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
error.message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -368,10 +427,18 @@ export class PersonalController extends Controller {
|
|||
*
|
||||
*/
|
||||
@Get("")
|
||||
async GetPersonal(@Request() request: RequestWithUser, @Query() personal_id: string) {
|
||||
async GetPersonal(
|
||||
@Request() request: RequestWithUser,
|
||||
@Query() personal_id: string
|
||||
) {
|
||||
try {
|
||||
let _workflow = await new permission().Workflow(request, personal_id, "SYS_PROBATION");
|
||||
if (_workflow == false) await new permission().PermissionGet(request, "SYS_PROBATION");
|
||||
let _workflow = await new permission().Workflow(
|
||||
request,
|
||||
personal_id,
|
||||
"SYS_PROBATION"
|
||||
);
|
||||
if (_workflow == false)
|
||||
await new permission().PermissionGet(request, "SYS_PROBATION");
|
||||
const person = await this.personalRepository.findOne({
|
||||
where: { personal_id: personal_id },
|
||||
});
|
||||
|
|
@ -400,7 +467,11 @@ export class PersonalController extends Controller {
|
|||
} catch (error: any) {
|
||||
if (error instanceof HttpError) {
|
||||
throw error;
|
||||
} else throw new HttpError(HttpStatusCode.INTERNAL_SERVER_ERROR, error.message);
|
||||
} else
|
||||
throw new HttpError(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
error.message
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,245 +1,322 @@
|
|||
import axios from "axios"
|
||||
import { RequestWithUser } from "../middlewares/user"
|
||||
import CallAPI from "./call-api"
|
||||
import HttpError from "./http-error"
|
||||
import HttpStatus from "./http-status"
|
||||
import { promisify } from "util"
|
||||
import axios from "axios";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
import CallAPI from "./call-api";
|
||||
import HttpError from "./http-error";
|
||||
import HttpStatus from "./http-status";
|
||||
import { promisify } from "util";
|
||||
|
||||
class CheckAuth {
|
||||
private redis = require("redis")
|
||||
private redis = require("redis");
|
||||
|
||||
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) {
|
||||
return null
|
||||
}
|
||||
return await new CallAPI()
|
||||
.GetData(req, "/org/permission")
|
||||
.then(x => {
|
||||
let permission = false
|
||||
let role = x.roles.find((x: any) => x.authSysId == system)
|
||||
if (!role) throw "ไม่มีสิทธิ์เข้าระบบ"
|
||||
if (role.attrOwnership == "OWNER") return "OWNER"
|
||||
if (action.trim().toLocaleUpperCase() == "CREATE") permission = role.attrIsCreate
|
||||
if (action.trim().toLocaleUpperCase() == "DELETE") permission = role.attrIsDelete
|
||||
if (action.trim().toLocaleUpperCase() == "GET") permission = role.attrIsGet
|
||||
if (action.trim().toLocaleUpperCase() == "LIST") permission = role.attrIsList
|
||||
if (action.trim().toLocaleUpperCase() == "UPDATE") permission = role.attrIsUpdate
|
||||
if (permission == false) throw "ไม่มีสิทธิ์ใช้งานระบบนี้"
|
||||
return role.attrPrivilege
|
||||
})
|
||||
.catch(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,
|
||||
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
|
||||
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
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return await new CallAPI()
|
||||
.GetData(req, "/org/permission")
|
||||
.then((x) => {
|
||||
let permission = false;
|
||||
let role = x.roles.find((x: any) => x.authSysId == system);
|
||||
if (!role) throw "ไม่มีสิทธิ์เข้าระบบ";
|
||||
if (role.attrOwnership == "OWNER") return "OWNER";
|
||||
if (action.trim().toLocaleUpperCase() == "CREATE")
|
||||
permission = role.attrIsCreate;
|
||||
if (action.trim().toLocaleUpperCase() == "DELETE")
|
||||
permission = role.attrIsDelete;
|
||||
if (action.trim().toLocaleUpperCase() == "GET")
|
||||
permission = role.attrIsGet;
|
||||
if (action.trim().toLocaleUpperCase() == "LIST")
|
||||
permission = role.attrIsList;
|
||||
if (action.trim().toLocaleUpperCase() == "UPDATE")
|
||||
permission = role.attrIsUpdate;
|
||||
if (permission == false) throw "ไม่มีสิทธิ์ใช้งานระบบนี้";
|
||||
return role.attrPrivilege;
|
||||
})
|
||||
.catch((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,
|
||||
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 = {
|
||||
root: [null],
|
||||
child1: [null],
|
||||
child2: [null],
|
||||
child3: [null],
|
||||
child4: [null],
|
||||
privilege: [null],
|
||||
}
|
||||
let node = 4
|
||||
if (x.orgChild1Id == null) {
|
||||
node = 0
|
||||
} else if (x.orgChild2Id == null) {
|
||||
node = 1
|
||||
} else if (x.orgChild3Id == null) {
|
||||
node = 2
|
||||
} else if (x.orgChild4Id == null) {
|
||||
node = 3
|
||||
}
|
||||
if (privilege == "OWNER") {
|
||||
data = {
|
||||
root: null,
|
||||
child1: null,
|
||||
child2: null,
|
||||
child3: null,
|
||||
child4: null,
|
||||
privilege: "OWNER",
|
||||
}
|
||||
} else if (privilege == "ROOT") {
|
||||
data = {
|
||||
root: [x.orgRootId],
|
||||
child1: null,
|
||||
child2: null,
|
||||
child3: null,
|
||||
child4: null,
|
||||
privilege: "ROOT",
|
||||
}
|
||||
} else if (privilege == "CHILD") {
|
||||
data = {
|
||||
root: node >= 0 ? [x.orgRootId] : null,
|
||||
child1: node >= 1 ? [x.orgChild1Id] : null,
|
||||
child2: node >= 2 ? [x.orgChild2Id] : null,
|
||||
child3: node >= 3 ? [x.orgChild3Id] : null,
|
||||
child4: node >= 4 ? [x.orgChild4Id] : null,
|
||||
privilege: "CHILD",
|
||||
}
|
||||
} else if (privilege == "NORMAL") {
|
||||
data = {
|
||||
root: [x.orgRootId],
|
||||
child1: [x.orgChild1Id],
|
||||
child2: [x.orgChild2Id],
|
||||
child3: [x.orgChild3Id],
|
||||
child4: [x.orgChild4Id],
|
||||
privilege: "NORMAL",
|
||||
}
|
||||
} else if (privilege == "SPECIFIC") {
|
||||
}
|
||||
let data: any = {
|
||||
root: [null],
|
||||
child1: [null],
|
||||
child2: [null],
|
||||
child3: [null],
|
||||
child4: [null],
|
||||
privilege: [null],
|
||||
};
|
||||
let node = 4;
|
||||
if (x.orgChild1Id == null) {
|
||||
node = 0;
|
||||
} else if (x.orgChild2Id == null) {
|
||||
node = 1;
|
||||
} else if (x.orgChild3Id == null) {
|
||||
node = 2;
|
||||
} else if (x.orgChild4Id == null) {
|
||||
node = 3;
|
||||
}
|
||||
if (privilege == "OWNER") {
|
||||
data = {
|
||||
root: null,
|
||||
child1: null,
|
||||
child2: null,
|
||||
child3: null,
|
||||
child4: null,
|
||||
privilege: "OWNER",
|
||||
};
|
||||
} else if (privilege == "ROOT") {
|
||||
data = {
|
||||
root: [x.orgRootId],
|
||||
child1: null,
|
||||
child2: null,
|
||||
child3: null,
|
||||
child4: null,
|
||||
privilege: "ROOT",
|
||||
};
|
||||
} else if (privilege == "PARENT") {
|
||||
data = {
|
||||
root: [x.orgRootId],
|
||||
child1: [null],
|
||||
child2: null,
|
||||
child3: null,
|
||||
child4: null,
|
||||
privilege: "PARENT",
|
||||
};
|
||||
} else if (privilege == "CHILD") {
|
||||
data = {
|
||||
root: node >= 0 ? [x.orgRootId] : null,
|
||||
child1: node >= 1 ? [x.orgChild1Id] : null,
|
||||
child2: node >= 2 ? [x.orgChild2Id] : null,
|
||||
child3: node >= 3 ? [x.orgChild3Id] : null,
|
||||
child4: node >= 4 ? [x.orgChild4Id] : null,
|
||||
privilege: "CHILD",
|
||||
};
|
||||
} else if (privilege == "BROTHER") {
|
||||
data = {
|
||||
// root: node >= 0 ? [x.orgRootId] : null,
|
||||
root: node >= 0 ? [x.orgRootId] : null,
|
||||
child1: node >= 2 ? [x.orgChild1Id] : null,
|
||||
child2: node >= 3 ? [x.orgChild2Id] : null,
|
||||
child3: node >= 4 ? [x.orgChild3Id] : null,
|
||||
privilege: "BROTHER",
|
||||
};
|
||||
} else if (privilege == "NORMAL") {
|
||||
data = {
|
||||
root: [x.orgRootId],
|
||||
child1: [x.orgChild1Id],
|
||||
child2: [x.orgChild2Id],
|
||||
child3: [x.orgChild3Id],
|
||||
child4: [x.orgChild4Id],
|
||||
privilege: "NORMAL",
|
||||
};
|
||||
} else if (privilege == "SPECIFIC") {
|
||||
}
|
||||
|
||||
return data
|
||||
})
|
||||
.catch(x => {
|
||||
if (x.status != undefined) {
|
||||
throw new HttpError(x.status, x.message)
|
||||
} else {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, x)
|
||||
}
|
||||
})
|
||||
}
|
||||
public async PermissionOrgByUser(req: RequestWithUser, system: string, action: string, profileId: string) {
|
||||
if (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
|
||||
return data;
|
||||
})
|
||||
.catch((x) => {
|
||||
if (x.status != undefined) {
|
||||
throw new HttpError(x.status, x.message);
|
||||
} else {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, x);
|
||||
}
|
||||
});
|
||||
}
|
||||
public async PermissionOrgByUser(
|
||||
req: RequestWithUser,
|
||||
system: string,
|
||||
action: string,
|
||||
profileId: string
|
||||
) {
|
||||
if (
|
||||
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.child1 != null) if (x.orgChild1Id != org.child1[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล"
|
||||
if (org.child2 != null) if (x.orgChild2Id != org.child2[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล"
|
||||
if (org.child3 != null) if (x.orgChild3Id != org.child3[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล"
|
||||
if (org.child4 != null) if (x.orgChild4Id != org.child4[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล"
|
||||
if (org.root != null)
|
||||
if (x.orgRootId != org.root[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล";
|
||||
if (org.child1 != null)
|
||||
if (x.orgChild1Id != org.child1[0]) throw "ไม่มีสิทธิ์เข้าถึงข้อมูล";
|
||||
if (org.child2 != null)
|
||||
if (x.orgChild2Id != org.child2[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
|
||||
})
|
||||
.catch(x => {
|
||||
if (x.status != undefined) {
|
||||
throw new HttpError(x.status, x.message)
|
||||
} else {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, x)
|
||||
}
|
||||
})
|
||||
}
|
||||
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) {
|
||||
return null
|
||||
}
|
||||
return await new CallAPI()
|
||||
.PostData(req, "/org/workflow/keycloak/isofficer", {
|
||||
refId: id,
|
||||
sysName: sysName,
|
||||
})
|
||||
.then(x => {
|
||||
return true
|
||||
})
|
||||
.catch(x => {
|
||||
return false
|
||||
})
|
||||
}
|
||||
public async checkOrg(token: any, keycloakId: string) {
|
||||
const redisClient = await this.redis.createClient({
|
||||
host: process.env.REDIS_HOST,
|
||||
port: process.env.REDIS_PORT,
|
||||
})
|
||||
const getAsync = promisify(redisClient.get).bind(redisClient)
|
||||
try {
|
||||
let reply = await getAsync("org_" + keycloakId)
|
||||
if (reply != null) {
|
||||
reply = JSON.parse(reply)
|
||||
} else {
|
||||
if (!keycloakId) throw new Error("No KeycloakId provided")
|
||||
const x = await new CallAPI().GetData(
|
||||
{
|
||||
headers: { authorization: token },
|
||||
},
|
||||
`/org/permission/checkOrg/${keycloakId}`,
|
||||
false
|
||||
)
|
||||
return true;
|
||||
})
|
||||
.catch((x) => {
|
||||
if (x.status != undefined) {
|
||||
throw new HttpError(x.status, x.message);
|
||||
} else {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, x);
|
||||
}
|
||||
});
|
||||
}
|
||||
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
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return await new CallAPI()
|
||||
.PostData(req, "/org/workflow/keycloak/isofficer", {
|
||||
refId: id,
|
||||
sysName: sysName,
|
||||
})
|
||||
.then((x) => {
|
||||
return true;
|
||||
})
|
||||
.catch((x) => {
|
||||
return false;
|
||||
});
|
||||
}
|
||||
public async checkOrg(token: any, keycloakId: string) {
|
||||
const redisClient = await this.redis.createClient({
|
||||
host: process.env.REDIS_HOST,
|
||||
port: process.env.REDIS_PORT,
|
||||
});
|
||||
const getAsync = promisify(redisClient.get).bind(redisClient);
|
||||
try {
|
||||
let reply = await getAsync("org_" + keycloakId);
|
||||
if (reply != null) {
|
||||
reply = JSON.parse(reply);
|
||||
} else {
|
||||
if (!keycloakId) throw new Error("No KeycloakId provided");
|
||||
const x = await new CallAPI().GetData(
|
||||
{
|
||||
headers: { authorization: token },
|
||||
},
|
||||
`/org/permission/checkOrg/${keycloakId}`,
|
||||
false
|
||||
);
|
||||
|
||||
const data = {
|
||||
orgRootId: x.orgRootId,
|
||||
orgChild1Id: x.orgChild1Id,
|
||||
orgChild2Id: x.orgChild2Id,
|
||||
orgChild3Id: x.orgChild3Id,
|
||||
orgChild4Id: x.orgChild4Id,
|
||||
}
|
||||
const data = {
|
||||
orgRootId: x.orgRootId,
|
||||
orgChild1Id: x.orgChild1Id,
|
||||
orgChild2Id: x.orgChild2Id,
|
||||
orgChild3Id: x.orgChild3Id,
|
||||
orgChild4Id: x.orgChild4Id,
|
||||
};
|
||||
|
||||
return data
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error calling API:", error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
public async PermissionCreate(req: RequestWithUser, system: string) {
|
||||
return await this.Permission(req, system, "CREATE")
|
||||
}
|
||||
public async PermissionDelete(req: RequestWithUser, system: string) {
|
||||
return await this.Permission(req, system, "DELETE")
|
||||
}
|
||||
public async PermissionGet(req: RequestWithUser, system: string) {
|
||||
return await this.Permission(req, system, "GET")
|
||||
}
|
||||
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")
|
||||
}
|
||||
return data;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error calling API:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
public async PermissionCreate(req: RequestWithUser, system: string) {
|
||||
return await this.Permission(req, system, "CREATE");
|
||||
}
|
||||
public async PermissionDelete(req: RequestWithUser, system: string) {
|
||||
return await this.Permission(req, system, "DELETE");
|
||||
}
|
||||
public async PermissionGet(req: RequestWithUser, system: string) {
|
||||
return await this.Permission(req, system, "GET");
|
||||
}
|
||||
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) {
|
||||
return await this.PermissionOrg(req, system, "CREATE")
|
||||
}
|
||||
public async PermissionOrgDelete(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "DELETE")
|
||||
}
|
||||
public async PermissionOrgGet(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "GET")
|
||||
}
|
||||
public async PermissionOrgList(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "LIST")
|
||||
}
|
||||
public async PermissionOrgUpdate(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "UPDATE")
|
||||
}
|
||||
public async PermissionOrgCreate(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "CREATE");
|
||||
}
|
||||
public async PermissionOrgDelete(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "DELETE");
|
||||
}
|
||||
public async PermissionOrgGet(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "GET");
|
||||
}
|
||||
public async PermissionOrgList(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "LIST");
|
||||
}
|
||||
public async PermissionOrgUpdate(req: RequestWithUser, system: string) {
|
||||
return await this.PermissionOrg(req, system, "UPDATE");
|
||||
}
|
||||
|
||||
public async PermissionOrgUserCreate(req: RequestWithUser, system: string, profileId: string) {
|
||||
return await this.PermissionOrgByUser(req, system, "CREATE", profileId)
|
||||
}
|
||||
public async PermissionOrgUserDelete(req: RequestWithUser, system: string, profileId: string) {
|
||||
return await this.PermissionOrgByUser(req, system, "DELETE", profileId)
|
||||
}
|
||||
public async PermissionOrgUserGet(req: RequestWithUser, system: string, profileId: string) {
|
||||
return await this.PermissionOrgByUser(req, system, "GET", profileId)
|
||||
}
|
||||
public async PermissionOrgUserList(req: RequestWithUser, system: string, profileId: string) {
|
||||
return await this.PermissionOrgByUser(req, system, "LIST", profileId)
|
||||
}
|
||||
public async PermissionOrgUserUpdate(req: RequestWithUser, system: string, profileId: string) {
|
||||
return await this.PermissionOrgByUser(req, system, "UPDATE", profileId)
|
||||
}
|
||||
public async PermissionOrgUserCreate(
|
||||
req: RequestWithUser,
|
||||
system: string,
|
||||
profileId: string
|
||||
) {
|
||||
return await this.PermissionOrgByUser(req, system, "CREATE", profileId);
|
||||
}
|
||||
public async PermissionOrgUserDelete(
|
||||
req: RequestWithUser,
|
||||
system: string,
|
||||
profileId: string
|
||||
) {
|
||||
return await this.PermissionOrgByUser(req, system, "DELETE", profileId);
|
||||
}
|
||||
public async PermissionOrgUserGet(
|
||||
req: RequestWithUser,
|
||||
system: string,
|
||||
profileId: string
|
||||
) {
|
||||
return await this.PermissionOrgByUser(req, system, "GET", profileId);
|
||||
}
|
||||
public async PermissionOrgUserList(
|
||||
req: RequestWithUser,
|
||||
system: string,
|
||||
profileId: string
|
||||
) {
|
||||
return await this.PermissionOrgByUser(req, system, "LIST", profileId);
|
||||
}
|
||||
public async PermissionOrgUserUpdate(
|
||||
req: RequestWithUser,
|
||||
system: string,
|
||||
profileId: string
|
||||
) {
|
||||
return await this.PermissionOrgByUser(req, system, "UPDATE", profileId);
|
||||
}
|
||||
}
|
||||
|
||||
export default CheckAuth
|
||||
export default CheckAuth;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue