58 lines
2 KiB
TypeScript
58 lines
2 KiB
TypeScript
import {
|
|
Controller,
|
|
Request,
|
|
Get,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Patch,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Path,
|
|
} from "tsoa";
|
|
import axios from "axios";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import CallAPI from "./call-api";
|
|
import HttpError from "./http-error";
|
|
import HttpStatus from "./http-status";
|
|
|
|
class CheckAuth {
|
|
public async Permission(req: RequestWithUser, system: string, action: string) {
|
|
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 (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 (role.attrOwnership == "OWNER") permission = true;
|
|
if (permission == false) throw "ไม่มีสิทธิ์ใช้งานระบบนี้";
|
|
return role.attrPrivilege;
|
|
})
|
|
.catch((x) => {
|
|
throw new HttpError(HttpStatus.FORBIDDEN, x);
|
|
});
|
|
}
|
|
public async PermissionCreate(req: RequestWithUser, system: string) {
|
|
this.Permission(req, system, "CREATE");
|
|
}
|
|
public async PermissionDelete(req: RequestWithUser, system: string) {
|
|
this.Permission(req, system, "DELETE");
|
|
}
|
|
public async PermissionGet(req: RequestWithUser, system: string) {
|
|
this.Permission(req, system, "GET");
|
|
}
|
|
public async PermissionList(req: RequestWithUser, system: string) {
|
|
this.Permission(req, system, "LIST");
|
|
}
|
|
public async PermissionUpdate(req: RequestWithUser, system: string) {
|
|
this.Permission(req, system, "UPDATE");
|
|
}
|
|
}
|
|
|
|
export default CheckAuth;
|