diff --git a/package-lock.json b/package-lock.json index 349ccc2..4ec9070 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "mysql2": "^3.9.1", "node-cron": "^3.0.3", "promise.any": "^2.0.6", + "redis": "~3.1.2", "reflect-metadata": "^0.2.1", "swagger-ui-express": "^5.0.0", "tsoa": "^6.0.1", @@ -3609,6 +3610,56 @@ "node": ">=8.10.0" } }, + "node_modules/redis": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/redis/-/redis-3.1.2.tgz", + "integrity": "sha512-grn5KoZLr/qrRQVwoSkmzdbw6pwF+/rwODtrOr6vuBRiR/f3rjSTGupbF90Zpqm2oenix8Do6RV7pYEkGwlKkw==", + "dependencies": { + "denque": "^1.5.0", + "redis-commands": "^1.7.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-redis" + } + }, + "node_modules/redis-commands": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.7.0.tgz", + "integrity": "sha512-nJWqw3bTFy21hX/CPKHth6sfhZbdiHP6bTawSgQBlKOVRG7EZkfHbbHwQJnrE4vsQf0CMNE+3gJ4Fmm16vdVlQ==" + }, + "node_modules/redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==", + "engines": { + "node": ">=4" + } + }, + "node_modules/redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==", + "dependencies": { + "redis-errors": "^1.0.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/redis/node_modules/denque": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz", + "integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==", + "engines": { + "node": ">=0.10" + } + }, "node_modules/reflect-metadata": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.1.tgz", diff --git a/package.json b/package.json index 67445e7..9f2d6c7 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "tsoa": "^6.0.1", "typeorm": "^0.3.19", "typeorm-cli": "^1.0.7", - "xlsx": "^0.18.5" + "xlsx": "^0.18.5", + "redis": "~3.1.2" } } diff --git a/src/interfaces/call-api.ts b/src/interfaces/call-api.ts index 711dc95..3748715 100644 --- a/src/interfaces/call-api.ts +++ b/src/interfaces/call-api.ts @@ -4,7 +4,7 @@ import { addLogSequence } from "./utils"; class CallAPI { //Get - public async GetData(request: any, @Path() path: any) { + public async GetData(request: any, @Path() path: any, log = true) { const token = "Bearer " + request.headers.authorization.replace("Bearer ", ""); const url = process.env.API_URL + path; try { @@ -15,7 +15,7 @@ class CallAPI { api_key: process.env.API_KEY, }, }); - addLogSequence(request, { + if(log) addLogSequence(request, { action: "request", status: "success", description: "connected", @@ -27,7 +27,7 @@ class CallAPI { }); return response.data.result; } catch (error) { - addLogSequence(request, { + if(log) addLogSequence(request, { action: "request", status: "error", description: "unconnected", diff --git a/src/interfaces/permission.ts b/src/interfaces/permission.ts index 870d2fe..407c08f 100644 --- a/src/interfaces/permission.ts +++ b/src/interfaces/permission.ts @@ -3,8 +3,11 @@ 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"); + public async Permission(req: RequestWithUser, system: string, action: string) { if ( req.headers.hasOwnProperty("api_key") && @@ -180,6 +183,62 @@ class CheckAuth { 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); + let reply = await getAsync("org_" + keycloakId); + if (reply != null) { + reply = JSON.parse(reply); + } else { + // await new CallAPI() + // .GetData( + // { + // headers: { authorization: token }, + // }, `/org/permission/checkOrg/${keycloakId}`,false) + // .then((x) => { + // console.log("[In Then]"); + // console.log("[res]",x); + // let data: any = { + // orgRootId: x.orgRootId, + // orgChild1Id: x.orgChild1Id, + // orgChild2Id: x.orgChild2Id, + // orgChild3Id: x.orgChild3Id, + // orgChild4Id: x.orgChild4Id, + // }; + // console.log("[data]",data); + // return data; + // }, + // ) + // .catch((error) => { + // console.error("Error calling API:", error); + // }); + try { + 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, + }; + + 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"); } diff --git a/src/middlewares/logs.ts b/src/middlewares/logs.ts index e5acc5b..308df41 100644 --- a/src/middlewares/logs.ts +++ b/src/middlewares/logs.ts @@ -1,5 +1,6 @@ import { NextFunction, Request, Response } from "express"; import { Client } from "@elastic/elasticsearch"; +import permission from "../interfaces/permission"; if (!process.env.ELASTICSEARCH_INDEX) { throw new Error("Require ELASTICSEARCH_INDEX to store log."); @@ -22,7 +23,7 @@ const elasticsearch = new Client({ async function logMiddleware(req: Request, res: Response, next: NextFunction) { if (!req.url.startsWith("/api/")) return next(); - let data: any; + let data: any; const originalJson = res.json; @@ -36,7 +37,7 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) { req.app.locals.logData = {}; - res.on("finish", () => { + res.on("finish", async() => { if (!req.url.startsWith("/api/")) return; const level = LOG_LEVEL_MAP[process.env.LOG_LEVEL ?? "debug"] || 4; @@ -44,10 +45,15 @@ async function logMiddleware(req: Request, res: Response, next: NextFunction) { if (level === 1 && res.statusCode < 500) return; if (level === 2 && res.statusCode < 400) return; if (level === 3 && res.statusCode < 200) return; - + let token: any; + token = req.headers['authorization'] + + const rootId = await new permission().checkOrg(token,req.app.locals.logData.userId); + const obj = { logType: res.statusCode >= 500 ? "error" : res.statusCode >= 400 ? "warning" : "info", ip: req.ip, + rootId: rootId?rootId.orgRootId:null, systemName: "development", startTimeStamp: timestamp, endTimeStamp: new Date().toISOString(),