feat(lst): added in modules activation, roles and userRoles

This commit is contained in:
2025-02-23 21:20:26 -06:00
parent f320118880
commit 7df5143835
20 changed files with 1576 additions and 20 deletions

View File

@@ -8,6 +8,7 @@ import {OpenAPIHono} from "@hono/zod-openapi";
import auth from "./services/auth/authService";
import scalar from "./services/general/route/scalar";
import apiHits from "./services/general/route/apitHits";
import getModules from "./services/general/route/getModules";
// services
import {ocmeService} from "./services/ocme/ocmeServer";
@@ -39,7 +40,7 @@ app.all("/ocme/*", async (c) => {
return ocmeService(c);
});
const routes = [scalar, auth, apiHits] as const;
const routes = [scalar, auth, apiHits, getModules] as const;
routes.forEach((route) => {
app.route("/api/", route);

View File

@@ -0,0 +1,15 @@
/*
pass over a users uuid and return all modules they have permission too.
in the login route we attach it to user under roles.
*/
import {eq} from "drizzle-orm";
import {db} from "../../../../database/dbClient";
import {userRoles} from "../../../../database/schema/userRoles";
export const roleCheck = async (user_id: any) => {
// get the user roles by the user_id
const roles = await db.select().from(userRoles).where(eq(userRoles.user_id, user_id));
return roles;
};

View File

@@ -3,6 +3,7 @@ import {db} from "../../../../database/dbClient";
import {users} from "../../../../database/schema/users";
import {eq} from "drizzle-orm";
import {checkPassword} from "../utils/checkPassword";
import {roleCheck} from "./getUserAccess";
/**
* Authenticate a user and return a JWT.
@@ -26,14 +27,17 @@ export async function login(
// Create a JWT
const secret: string = process.env.JWT_SECRET! || "bnghsjhsd";
const expiresIn: string = process.env.JWT_EXPIRES! || "1h";
const expiresIn = Number(process.env.JWT_EXPIRES!) || 60;
// get the user roles
const roles = await roleCheck(user[0].user_id);
const userData = {
user_id: user[0].user_id,
username: user[0].username,
email: user[0].email,
roles: roles || null,
};
const token = sign({user: userData}, secret, {expiresIn: 60 * 60});
const token = sign({user: userData}, secret, {expiresIn: expiresIn * 60});
return {token, user: {user_id: user[0].user_id, username: user[0].username}};
}

View File

@@ -41,7 +41,7 @@ app.openapi(
async (c) => {
const data = await c.req.json();
apiHit(data);
//apiHit(data);
// Return response with the received data
return c.json({

View File

@@ -0,0 +1,70 @@
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
import {modules} from "../../../../database/schema/modules";
import {db} from "../../../../database/dbClient";
import {eq} from "drizzle-orm";
// Define the request body schema
const requestSchema = z.object({
ip: z.string().optional(),
endpoint: z.string().optional(),
action: z.string().optional(),
stats: z.string().optional(),
});
// Define the response schema
const responseSchema = z.object({
message: z.string().optional(),
module_id: z.string().openapi({example: "6c922c6c-7de3-4ec4-acb0-f068abdc"}).optional(),
name: z.string().openapi({example: "Production"}).optional(),
active: z.boolean().openapi({example: true}).optional(),
roles: z.string().openapi({example: `["viewer","technician"]`}).optional(),
});
const app = new OpenAPIHono();
app.openapi(
createRoute({
tags: ["server"],
summary: "Returns all modules in the server",
method: "get",
path: "/server/modules",
responses: {
200: {
content: {
"application/json": {schema: responseSchema},
},
description: "Response message",
},
},
}),
async (c) => {
//const data = await c.req.json();
//apiHit(data);
// get the modules that are active
let module: any = [];
try {
module = await db.select().from(modules).where(eq(modules.active, true));
} catch (error) {
console.log(error);
module = [];
}
// parse the roles
const updateModules = module.map((m: any) => {
if (m.roles) {
return {...m, roles: JSON.parse(m?.roles)};
}
return m;
}); //JSON.parse(module[0]?.roles);
// Return response with the received data
return c.json({
message: `All active modules`,
data: updateModules,
});
}
);
export default app;