add rootId for log
This commit is contained in:
parent
a4647e102a
commit
212b9d1466
5 changed files with 100 additions and 6 deletions
51
package-lock.json
generated
51
package-lock.json
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
@ -16,7 +16,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 {
|
||||
|
|
@ -27,7 +27,7 @@ class CallAPI {
|
|||
api_key: process.env.API_KEY,
|
||||
},
|
||||
});
|
||||
addLogSequence(request, {
|
||||
if(log) addLogSequence(request, {
|
||||
action: "request",
|
||||
status: "success",
|
||||
description: "connected",
|
||||
|
|
@ -39,7 +39,7 @@ class CallAPI {
|
|||
});
|
||||
return response.data.result;
|
||||
} catch (error) {
|
||||
addLogSequence(request, {
|
||||
if(log) addLogSequence(request, {
|
||||
action: "request",
|
||||
status: "error",
|
||||
description: "unconnected",
|
||||
|
|
|
|||
|
|
@ -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,40 @@ 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 {
|
||||
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");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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.");
|
||||
|
|
@ -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,6 +45,10 @@ 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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue