hrms-api-org/src/controllers/PermissionController.ts

167 lines
5.7 KiB
TypeScript
Raw Normal View History

2024-07-23 19:06:51 +07:00
import { Body, Controller, Get, Path, Post, Request, Route, Security, Tags } from "tsoa";
import { AppDataSource } from "../database/data-source";
import { RequestWithUser } from "../middlewares/user";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import { AuthRole } from "../entities/AuthRole";
import { AuthRoleAttr } from "../entities/AuthRoleAttr";
import { PosMaster } from "../entities/PosMaster";
import { Profile } from "../entities/Profile";
2024-07-24 15:44:34 +07:00
import { AuthSys } from "../entities/AuthSys";
2024-07-25 09:54:44 +07:00
import { promisify } from "util";
import { In } from "typeorm";
2024-07-24 09:42:34 +07:00
const REDIS_HOST = process.env.REDIS_HOST;
const REDIS_PORT = process.env.REDIS_PORT;
2024-07-23 19:06:51 +07:00
@Route("api/v1/org/permission")
@Tags("Permission")
@Security("bearerAuth")
export class PermissionController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private posMasterRepository = AppDataSource.getRepository(PosMaster);
private authRoleRepo = AppDataSource.getRepository(AuthRole);
private authRoleAttrRepo = AppDataSource.getRepository(AuthRoleAttr);
2024-07-24 15:44:34 +07:00
private authSysRepo = AppDataSource.getRepository(AuthSys);
2024-07-24 09:42:34 +07:00
private redis = require("redis");
2024-07-23 19:06:51 +07:00
@Get("")
public async getPermission(@Request() request: { user: Record<string, any> }) {
2024-07-24 09:42:34 +07:00
const redisClient = await this.redis.createClient({
host: REDIS_HOST,
port: REDIS_PORT,
2024-07-23 19:06:51 +07:00
});
2024-07-25 09:54:44 +07:00
const getAsync = promisify(redisClient.get).bind(redisClient);
2024-07-23 19:06:51 +07:00
2024-07-25 09:54:44 +07:00
let reply = await getAsync("role_" + request.user.sub);
2024-08-06 11:34:46 +07:00
if (reply != null) {
reply = JSON.parse(reply);
} else {
const profile = await this.profileRepo.findOne({
select: ["id"],
where: { keycloak: request.user.sub },
});
if (!profile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
}
2024-07-23 19:06:51 +07:00
2024-08-06 11:34:46 +07:00
const posMaster = await this.posMasterRepository.findOne({
// select: ["authRoleId"],
where: {
current_holderId: profile.id,
orgRevision: {
orgRevisionIsDraft: false,
orgRevisionIsCurrent: true,
},
2024-08-05 14:34:27 +07:00
},
2024-08-06 11:34:46 +07:00
});
if (!posMaster) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
}
2024-07-23 19:06:51 +07:00
2024-08-06 11:34:46 +07:00
const getDetail = await this.authRoleRepo.findOne({
select: ["id", "roleName", "roleDescription"],
where: { id: posMaster.authRoleId },
});
if (!getDetail) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-07-25 09:54:44 +07:00
2024-08-06 11:34:46 +07:00
const roleAttrData = await this.authRoleAttrRepo.find({
select: [
"authSysId",
"parentNode",
"attrOwnership",
"attrIsCreate",
"attrIsList",
"attrIsGet",
"attrIsUpdate",
"attrIsDelete",
"attrPrivilege",
],
where: { authRoleId: getDetail.id },
});
2024-07-24 10:07:00 +07:00
2024-08-06 11:34:46 +07:00
reply = {
...getDetail,
roles: roleAttrData,
};
redisClient.setex("role_" + request.user.sub, 86400, JSON.stringify(reply));
}
2024-07-25 09:54:44 +07:00
return new HttpSuccess(reply);
2024-07-23 19:06:51 +07:00
}
2024-07-24 15:44:34 +07:00
@Get("menu")
2024-07-25 09:54:44 +07:00
public async listAuthSys(@Request() request: { user: Record<string, any> }) {
const redisClient = await this.redis.createClient({
host: REDIS_HOST,
port: REDIS_PORT,
2024-07-24 15:44:34 +07:00
});
2024-07-25 09:54:44 +07:00
const getAsync = promisify(redisClient.get).bind(redisClient);
2024-07-24 15:44:34 +07:00
2024-08-06 11:59:59 +07:00
let reply = await getAsync("menu_" + request.user.sub);
2024-07-25 09:54:44 +07:00
if (reply != null) {
reply = JSON.parse(reply);
} else {
const profile = await this.profileRepo.findOne({
select: ["id"],
where: { keycloak: request.user.sub },
});
if (!profile) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
}
const posMaster = await this.posMasterRepository.findOne({
// select: ["authRoleId"],
where: {
current_holderId: profile.id,
orgRevision: {
orgRevisionIsDraft: false,
orgRevisionIsCurrent: true,
},
},
});
if (!posMaster) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งในโครงสร้าง");
}
const authRole = await this.authRoleRepo.findOne({
select: ["id"],
where: { id: posMaster.authRoleId },
});
if (!authRole) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลสิทธิ์");
}
const roleAttrData = await this.authRoleAttrRepo.find({
2024-08-01 16:51:17 +07:00
select: ["authSysId", "parentNode"],
2024-07-31 09:43:23 +07:00
where: { authRoleId: authRole.id, attrIsList: true },
2024-07-25 09:54:44 +07:00
});
2024-08-01 16:51:17 +07:00
const parentNode = roleAttrData.map((x) => x.parentNode);
const authSysId = roleAttrData.map((x) => x.authSysId);
const sysId = parentNode.concat(authSysId);
2024-07-24 15:44:34 +07:00
2024-07-25 09:54:44 +07:00
const getList = await this.authSysRepo.find({
select: ["id", "parentId", "sysName", "sysDescription", "icon", "path", "order"],
where: {
2024-08-01 16:51:17 +07:00
id: In(sysId),
2024-07-25 09:54:44 +07:00
},
});
2024-08-06 11:34:46 +07:00
reply = getList
2024-07-25 09:54:44 +07:00
.filter((x) => x.parentId == null)
.map((item) => {
return {
...item,
children: getList
.filter((x) => x.parentId == item.id)
.sort((a, b) => a.order - b.order),
};
})
.sort((a, b) => a.order - b.order);
2024-07-31 09:43:23 +07:00
redisClient.setex("menu_" + request.user.sub, 86400, JSON.stringify(reply));
2024-07-25 09:54:44 +07:00
}
2024-07-24 15:44:34 +07:00
2024-07-25 09:54:44 +07:00
return new HttpSuccess(reply);
2024-07-24 15:44:34 +07:00
}
2024-07-23 19:06:51 +07:00
}