edit permission

This commit is contained in:
DESKTOP-2S5P7D1\Windows 10 2024-12-14 01:04:01 +07:00
parent 19381e99f7
commit 7a9e3c5c11
3 changed files with 193 additions and 163 deletions

View file

@ -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") && req.headers["api_key"] && req.headers["api_key"] == process.env.API_KEY) {
return null
@ -155,6 +158,41 @@ 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 {
if (!keycloakId) throw "Error calling API No KeycloakId"
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")
}