Compare commits

...

3 Commits

40 changed files with 504 additions and 213 deletions

5
.gitignore vendored
View File

@@ -1,6 +1,8 @@
dist
frontend/dist
server/dist
dist
apiDocs/
# ---> Node
bun.lock
.nx
@@ -137,3 +139,6 @@ dist
.yarn/install-state.gz
.pnp.*
nssm.exe

View File

@@ -11,5 +11,5 @@ get {
}
auth:bearer {
token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7InVzZXJfaWQiOiIyYzYzODQ3Zi1mMWVjLTRkNTAtOGFkMi05ODBkMWYzZTgwZWIiLCJ1c2VybmFtZSI6Im1hdHRoZXMwMSIsImVtYWlsIjoiYmxha2UubWF0dGhlc0BhbHBsYS5jb20iLCJyb2xlcyI6W10sInJvbGUiOiJ1c2VyIn0sImlhdCI6MTc0MDY3NTMyMiwiZXhwIjoxNzQwNjc4OTIyfQ.D4rLIBAZfo0larMGUmXF6Z2KVnZvRLvth2116JyK2z8
token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7InVzZXJfaWQiOiIyYzYzODQ3Zi1mMWVjLTRkNTAtOGFkMi05ODBkMWYzZTgwZWIiLCJ1c2VybmFtZSI6Im1hdHRoZXMwMSIsImVtYWlsIjoiYmxha2UubWF0dGhlc0BhbHBsYS5jb20iLCJyb2xlcyI6W10sInJvbGUiOiJ1c2VyIn0sImlhdCI6MTc0MDg2ODMzNCwiZXhwIjoxNzQwODcxOTM0fQ.HpPh56_8API25IeO5FSfYSvAqf97_tKUT_GEBqbggO0
}

View File

@@ -1,28 +1,26 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
import js from "@eslint/js";
import globals from "globals";
import reactHooks from "eslint-plugin-react-hooks";
import reactRefresh from "eslint-plugin-react-refresh";
import tseslint from "typescript-eslint";
export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
{ignores: ["dist"]},
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ["**/*.{ts,tsx}"],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
"react-hooks": reactHooks,
"react-refresh": reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
"react-refresh/only-export-components": ["warn", {allowConstantExport: true}],
"no-console": ["error", {allow: ["warn", "error"]}],
},
}
);

View File

@@ -23,6 +23,7 @@
"@tanstack/react-router": "^1.111.11",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"dotenv": "^16.4.7",
"hono": "^4.7.2",
"js-cookie": "^3.0.5",
"jsonwebtoken": "^9.0.2",
@@ -3552,6 +3553,18 @@
"node": ">=0.3.1"
}
},
"node_modules/dotenv": {
"version": "16.4.7",
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz",
"integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==",
"license": "BSD-2-Clause",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://dotenvx.com"
}
},
"node_modules/ecdsa-sig-formatter": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",

View File

@@ -26,6 +26,7 @@
"@tanstack/react-router": "^1.111.11",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"dotenv": "^16.4.7",
"hono": "^4.7.2",
"js-cookie": "^3.0.5",
"jsonwebtoken": "^9.0.2",

View File

@@ -26,7 +26,7 @@ const LoginForm = () => {
// }
const data = await response.json();
console.log("Response", data.data);
// console.log("Response", data.data);
// Store token in localStorage
localStorage.setItem("auth_token", data.data.token);
@@ -41,6 +41,7 @@ const LoginForm = () => {
setUsername("");
setPassword("");
} catch (err) {
console.error(err);
setError("Invalid credentials");
}
};

View File

@@ -15,7 +15,6 @@ export function AppSidebar() {
const {user} = useSessionStore();
const {modules} = useModuleStore();
console.log(user);
return (
<Sidebar collapsible="icon">
<SidebarContent>

View File

@@ -1,14 +1,17 @@
import {QueryClient, QueryClientProvider} from "@tanstack/react-query";
import {useModuleStore} from "../../lib/store/useModuleStore";
import {useEffect} from "react";
//import {useGetUserRoles} from "@/lib/store/useGetRoles";
const queryClient = new QueryClient();
export const SessionProvider = ({children}: {children: React.ReactNode}) => {
const {fetchModules} = useModuleStore();
//const {fetchUserRoles} = useGetUserRoles();
useEffect(() => {
fetchModules();
//fetchUserRoles();
}, []);
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
};

View File

@@ -1,17 +1,6 @@
import {User} from "@/types/users";
import {create} from "zustand";
type User = {
user_id: string;
email: string;
username: string;
roles: keyof Roles[];
role: string;
};
interface Roles {
role: string;
}
export type SessionState = {
user: User | null;
token: string | null;

View File

@@ -1,17 +1,6 @@
import {create} from "zustand";
import {useSessionStore} from "./sessionStore";
//import useSWR from "swr";
interface Modules {
module_id: string;
name: string;
active: boolean;
roles: string;
add_user: string;
add_date: Date;
upd_user: string;
upd_date: Date;
}
import {Modules} from "@/types/modules";
interface SettingState {
userRoles: Modules[];
@@ -23,7 +12,7 @@ interface FetchModulesResponse {
data: Modules[];
}
export const useModuleStore = create<SettingState>()((set) => ({
export const useGetUserRoles = create<SettingState>()((set) => ({
userRoles: [],
setUserRoles: (userRoles) => set({userRoles}),
fetchUserRoles: async () => {

View File

@@ -1,17 +1,5 @@
import {Modules} from "@/types/modules";
import {create} from "zustand";
//import useSWR from "swr";
// interface Modules {
// module_id: string;
// name: string;
// active: boolean;
// roles: string;
// add_user: string;
// add_date: Date;
// upd_user: string;
// upd_date: Date;
// }
interface SettingState {
modules: Modules[];

View File

@@ -19,6 +19,7 @@ import { Route as IndexImport } from './routes/index'
import { Route as OcpIndexImport } from './routes/ocp/index'
import { Route as OcpLotsImport } from './routes/ocp/lots'
import { Route as AuthProfileImport } from './routes/_auth/profile'
import { Route as AdminModulesImport } from './routes/_admin/modules'
// Create/Update Routes
@@ -68,6 +69,12 @@ const AuthProfileRoute = AuthProfileImport.update({
getParentRoute: () => AuthRoute,
} as any)
const AdminModulesRoute = AdminModulesImport.update({
id: '/modules',
path: '/modules',
getParentRoute: () => AdminRoute,
} as any)
// Populate the FileRoutesByPath interface
declare module '@tanstack/react-router' {
@@ -107,6 +114,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof LoginImport
parentRoute: typeof rootRoute
}
'/_admin/modules': {
id: '/_admin/modules'
path: '/modules'
fullPath: '/modules'
preLoaderRoute: typeof AdminModulesImport
parentRoute: typeof AdminImport
}
'/_auth/profile': {
id: '/_auth/profile'
path: '/profile'
@@ -133,6 +147,16 @@ declare module '@tanstack/react-router' {
// Create and export the route tree
interface AdminRouteChildren {
AdminModulesRoute: typeof AdminModulesRoute
}
const AdminRouteChildren: AdminRouteChildren = {
AdminModulesRoute: AdminModulesRoute,
}
const AdminRouteWithChildren = AdminRoute._addFileChildren(AdminRouteChildren)
interface AuthRouteChildren {
AuthProfileRoute: typeof AuthProfileRoute
}
@@ -148,6 +172,7 @@ export interface FileRoutesByFullPath {
'': typeof AuthRouteWithChildren
'/about': typeof AboutRoute
'/login': typeof LoginRoute
'/modules': typeof AdminModulesRoute
'/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute
'/ocp': typeof OcpIndexRoute
@@ -158,6 +183,7 @@ export interface FileRoutesByTo {
'': typeof AuthRouteWithChildren
'/about': typeof AboutRoute
'/login': typeof LoginRoute
'/modules': typeof AdminModulesRoute
'/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute
'/ocp': typeof OcpIndexRoute
@@ -166,10 +192,11 @@ export interface FileRoutesByTo {
export interface FileRoutesById {
__root__: typeof rootRoute
'/': typeof IndexRoute
'/_admin': typeof AdminRoute
'/_admin': typeof AdminRouteWithChildren
'/_auth': typeof AuthRouteWithChildren
'/about': typeof AboutRoute
'/login': typeof LoginRoute
'/_admin/modules': typeof AdminModulesRoute
'/_auth/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute
'/ocp/': typeof OcpIndexRoute
@@ -177,9 +204,25 @@ export interface FileRoutesById {
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/' | '' | '/about' | '/login' | '/profile' | '/ocp/lots' | '/ocp'
fullPaths:
| '/'
| ''
| '/about'
| '/login'
| '/modules'
| '/profile'
| '/ocp/lots'
| '/ocp'
fileRoutesByTo: FileRoutesByTo
to: '/' | '' | '/about' | '/login' | '/profile' | '/ocp/lots' | '/ocp'
to:
| '/'
| ''
| '/about'
| '/login'
| '/modules'
| '/profile'
| '/ocp/lots'
| '/ocp'
id:
| '__root__'
| '/'
@@ -187,6 +230,7 @@ export interface FileRouteTypes {
| '/_auth'
| '/about'
| '/login'
| '/_admin/modules'
| '/_auth/profile'
| '/ocp/lots'
| '/ocp/'
@@ -195,7 +239,7 @@ export interface FileRouteTypes {
export interface RootRouteChildren {
IndexRoute: typeof IndexRoute
AdminRoute: typeof AdminRoute
AdminRoute: typeof AdminRouteWithChildren
AuthRoute: typeof AuthRouteWithChildren
AboutRoute: typeof AboutRoute
LoginRoute: typeof LoginRoute
@@ -205,7 +249,7 @@ export interface RootRouteChildren {
const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute,
AdminRoute: AdminRoute,
AdminRoute: AdminRouteWithChildren,
AuthRoute: AuthRouteWithChildren,
AboutRoute: AboutRoute,
LoginRoute: LoginRoute,
@@ -236,7 +280,10 @@ export const routeTree = rootRoute
"filePath": "index.tsx"
},
"/_admin": {
"filePath": "_admin.tsx"
"filePath": "_admin.tsx",
"children": [
"/_admin/modules"
]
},
"/_auth": {
"filePath": "_auth.tsx",
@@ -250,6 +297,10 @@ export const routeTree = rootRoute
"/login": {
"filePath": "login.tsx"
},
"/_admin/modules": {
"filePath": "_admin/modules.tsx",
"parent": "/_admin"
},
"/_auth/profile": {
"filePath": "_auth/profile.tsx",
"parent": "/_auth"

View File

@@ -0,0 +1,9 @@
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/_admin/modules')({
component: RouteComponent,
})
function RouteComponent() {
return <div>Hello "/_admin/modules"!</div>
}

View File

@@ -1,5 +1,5 @@
import {createFileRoute} from "@tanstack/react-router";
import GridLayout from "react-grid-layout";
//import GridLayout from "react-grid-layout";
import "../../node_modules/react-grid-layout/css/styles.css";
import "../../node_modules/react-resizable/css/styles.css";
@@ -32,7 +32,7 @@ function Index() {
return (
<>
{/* <AddCards addCard={addCard} cards={cards} /> */}
<GridLayout className="layout" cols={12} rowHeight={30} width={window.innerWidth}>
{/* <GridLayout className="layout" cols={12} rowHeight={30} width={window.innerWidth}>
<div className="bg-blue-400" key="a" data-grid={{x: 0, y: 0, w: 1, h: 2, static: true}}>
a
</div>
@@ -42,7 +42,10 @@ function Index() {
<div className="bg-blue-400" key="c" data-grid={{x: 4, y: 0, w: 1, h: 2}}>
c
</div>
</GridLayout>
</GridLayout> */}
<div className="m-auto">
<span>Empty Space why dont you add some cards?</span>
</div>
</>
);
}

View File

@@ -3,8 +3,8 @@ export interface Modules {
name: string;
active: boolean;
roles: string;
add_User: string;
add_Date: Date;
add_user: string;
add_date: Date;
upd_user: string;
upd_date: Date;
}

View File

@@ -0,0 +1,4 @@
export interface Roles {
role: string;
module_id: string;
}

View File

@@ -0,0 +1,9 @@
import {Roles} from "./roles";
export type User = {
user_id: string;
email: string;
username: string;
roles: Roles[];
role: string;
};

View File

@@ -4,12 +4,13 @@ type User = {
user_id: string;
email: string;
username: string;
roles: keyof Roles[];
roles: Roles[];
role: string;
};
interface Roles {
role: string;
module_id: string;
}
// user will need access to the module.
@@ -17,9 +18,8 @@ interface Roles {
export function hasAccess(user: User | null, moduleName: string | null, modules: Modules[]): boolean {
// get the modules for the id
const filteredModule = modules?.filter((f) => f.name === moduleName);
console.log(filteredModule);
//console.log(filteredModule[0].module_id);
// userroles and filter out by the module id,
console.log(user);
return false;
return user?.roles.find((role) => role.module_id === filteredModule[0].module_id) ? true : false;
}

View File

@@ -4,6 +4,10 @@ import tailwindcss from "@tailwindcss/vite";
import {TanStackRouterVite} from "@tanstack/router-plugin/vite";
import path from "path";
import dotenv from "dotenv";
import {fileURLToPath} from "url";
dotenv.config({path: path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.env")});
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), tailwindcss(), TanStackRouterVite({target: "react", autoCodeSplitting: true})],
@@ -14,7 +18,7 @@ export default defineConfig({
},
server: {
proxy: {
"/api": {target: `http://localhost:4400`, changeOrigin: true},
"/api": {target: `http://localhost:${Number(process.env.VITE_SERVER_PORT || 4400)}`, changeOrigin: true},
},
},
});

View File

@@ -7,14 +7,15 @@
"dev:server": "dotenvx run -f .env -- tsx watch server/index.ts",
"dev:frontend": "cd frontend && npm run dev",
"build": "npm run build:server && npm run build:frontend",
"build:server": "rimraf dist && tsc --build",
"build:server": "rimraf dist && tsc --build && xcopy server\\scripts dist\\server\\scripts /E /I /Y",
"build:frontend": "cd frontend && npm run build",
"start": "npm run start:server",
"start:server": "dotenvx run -f .env -- node dist/server/index.js",
"db:generate": "npx drizzle-kit generate",
"db:migrate": "npx drizzle-kit push",
"deploy": "standard-version --conventional-commits",
"commit": "cz"
"commit": "cz",
"prodinstall": "npm i --omit=dev"
},
"dependencies": {
"@dotenvx/dotenvx": "^1.38.3",
@@ -33,13 +34,13 @@
"jsonwebtoken": "^9.0.2",
"pg": "^8.13.3",
"postgres": "^3.4.5",
"zod": "^3.24.2"
"zod": "^3.24.2",
"drizzle-kit": "^0.30.4"
},
"devDependencies": {
"@types/node": "^22.13.5",
"concurrently": "^8.2.0",
"dotenv": "^16.3.1",
"drizzle-kit": "^0.30.4",
"tsx": "^4.7.1",
"@types/bcrypt": "^5.0.2",
"@types/js-cookie": "^3.0.6",

28
server/.gitignore vendored
View File

@@ -1,28 +0,0 @@
# dev
.yarn/
!.yarn/releases
.vscode/*
!.vscode/launch.json
!.vscode/*.code-snippets
.idea/workspace.xml
.idea/usage.statistics.xml
.idea/shelf
# deps
node_modules/
# env
.env
.env.production
# logs
logs/
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
# misc
.DS_Store

View File

@@ -1,10 +0,0 @@
import { defineConfig } from "drizzle-kit";
const database = process.env.DATABASE_URL || "";
export default defineConfig({
dialect: "postgresql",
schema: "database/schema",
out: "database/migrations",
dbCredentials: {
url: database,
},
});

View File

@@ -1,26 +0,0 @@
import type { Context } from "hono";
import { z } from "zod";
declare const requestSchema: z.ZodObject<{
ip: z.ZodOptional<z.ZodString>;
endpoint: z.ZodString;
action: z.ZodOptional<z.ZodString>;
stats: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
ip?: string;
endpoint?: string;
action?: string;
stats?: string;
}, {
ip?: string;
endpoint?: string;
action?: string;
stats?: string;
}>;
type ApiHitData = z.infer<typeof requestSchema>;
export declare const apiHit: (c: Context, data: unknown) => Promise<{
success: boolean;
data?: ApiHitData;
errors?: any[];
}>;
export {};
//# sourceMappingURL=apiHits.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"apiHits.d.ts","sourceRoot":"","sources":["apiHits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,MAAM,CAAC;AAClC,OAAO,EAAC,CAAC,EAAW,MAAM,KAAK,CAAC;AAGhC,QAAA,MAAM,aAAa;;;;;;;;;;;;;;;EAKjB,CAAC;AAEH,KAAK,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAEhD,eAAO,MAAM,MAAM,MACZ,OAAO,QACJ,OAAO,KACd,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,IAAI,CAAC,EAAE,UAAU,CAAC;IAAC,MAAM,CAAC,EAAE,GAAG,EAAE,CAAA;CAAC,CAuB/D,CAAC"}

View File

@@ -1,20 +0,0 @@
import { z, ZodError } from "zod";
const requestSchema = z.object({
ip: z.string().optional(),
endpoint: z.string(),
action: z.string().optional(),
stats: z.string().optional(),
});
export const apiHit = async (c, data) => {
try {
const forwarded = c.req.header("host");
const validatedData = requestSchema.parse(data);
return { success: true, data: validatedData };
}
catch (error) {
if (error instanceof ZodError) {
return { success: false, errors: error.errors };
}
return { success: false, errors: [{ message: "An unknown error occurred" }] };
}
};

View File

@@ -0,0 +1,15 @@
import type {Context} from "hono";
import type {ContentfulStatusCode} from "hono/utils/http-status";
export const apiReturn = async (
c: Context,
success: boolean,
message: string,
data: any,
code: ContentfulStatusCode
): Promise<Response> => {
/**
* This is just a global return function to reduce constacnt typing the same thing lol
*/
return c.json({success, message, data}, code);
};

View File

@@ -4,9 +4,6 @@ import {serveStatic} from "@hono/node-server/serve-static";
import {logger} from "hono/logger";
import {cors} from "hono/cors";
import {db} from "../database/dbclient.js";
import {modules} from "../database/schema/modules.js";
// custom routes
import scalar from "./services/general/route/scalar.js";
import system from "./services/server/systemServer.js";
@@ -63,7 +60,7 @@ app.use("*", serveStatic({path: "./frontend/dist/index.html"}));
serve(
{
fetch: app.fetch,
port: Number(process.env.SERVER_PORT),
port: Number(process.env.VITE_SERVER_PORT),
},
(info) => {
console.log(`Server is running on http://localhost:${info.port}`);

View File

@@ -0,0 +1,85 @@
param (
[string]$serviceName,
[string]$option,
[string]$appPath,
[string]$command, # just the command like run startadm or what ever you have in npm.
[string]$description
)
# Example string to run with the parameters in it.
# .\services.ps1 -serviceName "LST-Admin" -option "install" -appPath "C:\Users\matthes01\Documents\lstV2" -description "The Admin DashBoard" -command "npm run startadm"
$nssmPath = $AppPath + "\nssm.exe"
$npmPath = "C:\Program Files\nodejs\npm.cmd" # Path to npm.cmd
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "Error: This script must be run as Administrator."
exit 1
}
if(-not $serviceName -or -not $option){
Write-host "The service name or option is missing please enter one of them and try again."
exit 1
}
if ($option -eq "start"){
write-host "Starting $($serviceName)."
Start-Service $serviceName
}
if ($option -eq "stop"){
write-host "Stoping $($serviceName)."
Stop-Service $serviceName
}
if ($option -eq "restart"){
write-host "Stoping $($serviceName) to be restarted"
Stop-Service $serviceName
Start-Sleep 3 # so we give it enough time to fully stop
write-host "Starting $($serviceName)"
Start-Service $serviceName
}
if ($option -eq "delete"){
if(-not $appPath){
Write-host "The path to the app is missing please add it in and try again."
exit 1
}
& $nssmPath stop $serviceName
write-host "Removing $($serviceName)"
& $nssmPath remove $serviceName confirm
}
if($option -eq "install"){
if(-not $appPath -or -not $description -or -not $command){
Write-host "Please check all parameters are passed to install the app.."
exit 1
}
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if(-not $service){
write-host $serviceName "is not installed we will install it now"
Write-Host "Installing $serviceName..."
& $nssmPath install $serviceName $npmPath $command
& $nssmPath set $serviceName AppDirectory $appPath
& $nssmPath set $serviceName Description $description
# Set recovery options
sc.exe failure $serviceName reset= 0 actions= restart/5000/restart/5000/restart/5000
& $nssmPath start $serviceName
}else{
write-host $serviceName "is already installed will push the updated info"
Write-Host "Updating $serviceName..."
& $nssmPath stop $serviceName
& $nssmPath set $serviceName AppDirectory $appPath
& $nssmPath set $serviceName Description $description
# Set recovery options
sc.exe failure $serviceName reset= 0 actions= restart/5000/restart/5000/restart/5000
Start-Sleep 4
& $nssmPath start $serviceName
}
}

View File

@@ -1,10 +1,10 @@
import {OpenAPIHono} from "@hono/zod-openapi";
import {authMiddleware} from "./middleware/authMiddleware.js";
import login from "./routes/login.js";
import register from "./routes/register.js";
import session from "./routes/session.js";
import getAccess from "./routes/getUserRoles.js";
import {authMiddleware} from "./middleware/authMiddleware.js";
import getAccess from "./routes/userRoles/getUserRoles.js";
import setAccess from "./routes/userRoles/setUserRoles.js";
const app = new OpenAPIHono();
app.route("auth/login", login);
@@ -13,6 +13,10 @@ app.route("auth/session", session);
// required to login
app.use("auth/getuseraccess", authMiddleware);
app.route("/auth/getuseraccess", getAccess);
app.use("auth/setuseraccess", authMiddleware);
app.route("/auth/setuseraccess", setAccess);
export default app;

View File

@@ -3,7 +3,7 @@ import {db} from "../../../../database/dbclient.js";
import {users} from "../../../../database/schema/users.js";
import {eq, sql} from "drizzle-orm";
import {checkPassword} from "../utils/checkPassword.js";
import {roleCheck} from "./getUserAccess.js";
import {roleCheck} from "./userRoles/getUserAccess.js";
/**
* Authenticate a user and return a JWT.

View File

@@ -4,10 +4,13 @@ in the login route we attach it to user under roles.
*/
import {eq} from "drizzle-orm";
import {db} from "../../../../database/dbclient.js";
import {userRoles} from "../../../../database/schema/userRoles.js";
import {db} from "../../../../../database/dbclient.js";
import {userRoles} from "../../../../../database/schema/userRoles.js";
export const roleCheck = async (user_id: any) => {
export const roleCheck = async (user_id: string | undefined) => {
if (!user_id) {
throw Error("Missing user_id");
}
// get the user roles by the user_id
const roles = await db.select().from(userRoles).where(eq(userRoles.user_id, user_id));

View File

@@ -0,0 +1,35 @@
import {users} from "../../../../../database/schema/users.js";
import {eq} from "drizzle-orm";
import {db} from "../../../../../database/dbclient.js";
import {userRoles} from "../../../../../database/schema/userRoles.js";
import {modules} from "../../../../../database/schema/modules.js";
import {roles} from "../../../../../database/schema/roles.js";
export const setSysAdmin = async (user: any, roleName: any): Promise<void> => {
// remove all userRoles to prevent errors
try {
const remove = await db.delete(userRoles).where(eq(userRoles.user_id, user[0].user_id));
} catch (error) {
console.log(error);
}
// now we want to add the user to the system admin.
const module = await db.select().from(modules);
const role = await db.select().from(roles).where(eq(roles.name, roleName));
for (let i = 0; i < module.length; i++) {
try {
const userRole = await db.insert(userRoles).values({
user_id: user[0].user_id,
role_id: role[0].role_id,
module_id: module[i].module_id,
role: roleName,
});
console.log(`${user[0].username} has been granted access to ${module[i].name} with the role ${roleName}`);
} catch (error) {
console.log(error);
}
}
return;
};

View File

@@ -0,0 +1,57 @@
/*
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.js";
import {userRoles} from "../../../../../database/schema/userRoles.js";
import {users} from "../../../../../database/schema/users.js";
import {modules} from "../../../../../database/schema/modules.js";
import {roles} from "../../../../../database/schema/roles.js";
import {setSysAdmin} from "./setSysAdmin.js";
export const setUserAccess = async (username: string, moduleName: string, roleName: string, override?: string) => {
// get the user roles by the user_id
const user = await db.select().from(users).where(eq(users.username, username));
const module = await db.select().from(modules).where(eq(modules.name, moduleName));
if (process.env.SECRETOVERRIDECODE != override && roleName === "systemAdmin") {
return {success: false, message: "The override code provided is invalid."};
}
const role = await db.select().from(roles).where(eq(roles.name, roleName));
/**
* For system admin we want to do a little more
*/
if (roleName === "systemAdmin") {
await setSysAdmin(user, roleName);
return {
success: true,
message: `${username} has been granted access to ${moduleName} with the role ${roleName}`,
};
}
//console.log(user, module, role);
// set the user
try {
const userRole = await db
.insert(userRoles)
.values({user_id: user[0].user_id, role_id: role[0].role_id, module_id: module[0].module_id, role: roleName});
//.returning({user: users.username, email: users.email});
// return c.json({message: "User Registered", user}, 200);
return {
success: true,
message: `${username} has been granted access to ${moduleName} with the role ${roleName}`,
};
} catch (error) {
return {
success: false,
message: `There was an error granting ${username} access to ${moduleName} with the role ${roleName}`,
data: error,
};
}
};

View File

@@ -1,31 +0,0 @@
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
import {apiHit} from "../../../globalUtils/apiHits.js";
const app = new OpenAPIHono();
const responseSchema = z.object({
message: z.string().optional().openapi({example: "User Created"}),
});
app.openapi(
createRoute({
tags: ["Auth"],
summary: "Returns the useraccess table",
method: "get",
path: "/",
responses: {
200: {
content: {"application/json": {schema: responseSchema}},
description: "Retrieve the user",
},
},
}),
async (c) => {
// apit hit
apiHit(c, {endpoint: "api/auth/register"});
return c.json({message: "UserRoles coming over"});
}
);
export default app;

View File

@@ -0,0 +1,52 @@
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
import {apiHit} from "../../../../globalUtils/apiHits.js";
import jwt from "jsonwebtoken";
import {roleCheck} from "../../controllers/userRoles/getUserAccess.js";
import type {CustomJwtPayload} from "../../../../types/jwtToken.js";
const {verify} = jwt;
const app = new OpenAPIHono();
const responseSchema = z.object({
message: z.string().optional().openapi({example: "User Created"}),
});
app.openapi(
createRoute({
tags: ["Auth"],
summary: "Returns the useraccess table",
method: "get",
path: "/",
responses: {
200: {
content: {"application/json": {schema: responseSchema}},
description: "Retrieve the user",
},
},
}),
async (c) => {
// apit hit
apiHit(c, {endpoint: "api/auth/getUserRoles"});
const authHeader = c.req.header("Authorization");
const token = authHeader?.split("Bearer ")[1] || "";
try {
const secret = process.env.JWT_SECRET!;
if (!secret) {
throw new Error("JWT_SECRET is not defined in environment variables");
}
const payload = verify(token, secret) as CustomJwtPayload;
const canAccess = await roleCheck(payload.user?.user_id);
return c.json({sucess: true, message: `User ${payload.user?.username} can access`, data: canAccess}, 200);
} catch (error) {
console.log(error);
}
return c.json({message: "UserRoles coming over"});
}
);
export default app;

View File

@@ -0,0 +1,63 @@
import {createRoute, OpenAPIHono, z} from "@hono/zod-openapi";
import {setUserAccess} from "../../controllers/userRoles/setUserRoles.js";
import {apiHit} from "../../../../globalUtils/apiHits.js";
import {apiReturn} from "../../../../globalUtils/apiReturn.js";
const app = new OpenAPIHono();
const responseSchema = z.object({
success: z.boolean().openapi({example: true}),
message: z.string().optional().openapi({example: "user access"}),
data: z.array(z.object({})).optional().openapi({example: []}),
});
const UserAccess = z.object({
username: z
.string()
.regex(/^[a-zA-Z0-9_]{3,30}$/)
.openapi({example: "smith034"}),
module: z.string().openapi({example: "production"}),
role: z.string().openapi({example: "viewer"}),
override: z.string().optional().openapi({example: "secretString"}),
});
app.openapi(
createRoute({
tags: ["Auth"],
summary: "Sets Users access",
method: "post",
path: "/",
description: "When logged in you will be able to grant new permissions",
request: {
body: {
content: {
"application/json": {schema: UserAccess},
},
},
},
responses: {
200: {
content: {"application/json": {schema: responseSchema}},
description: "Retrieve the user",
},
400: {
content: {"application/json": {schema: responseSchema}},
description: "Failed to get user access",
},
},
}),
async (c) => {
apiHit(c, {endpoint: "api/auth/setUserRoles"});
const {username, module, role, override} = await c.req.json();
try {
const access = await setUserAccess(username, module, role, override);
//return apiReturn(c, true, access?.message, access?.data, 200);
return c.json({success: access.success, message: access.message, data: access.data}, 200);
} catch (error) {
console.log(error);
//return apiReturn(c, false, "Error in setting the user access", error, 400);
return c.json({success: false, message: "Error in setting the user access", data: error}, 400);
}
}
);
export default app;

6
server/types/jwtToken.ts Normal file
View File

@@ -0,0 +1,6 @@
import type {JwtPayload} from "jsonwebtoken";
import type {User} from "./users.js";
export type CustomJwtPayload = JwtPayload & {
user: User | undefined;
};

10
server/types/modules.ts Normal file
View File

@@ -0,0 +1,10 @@
export interface Modules {
module_id: string;
name: string;
active: boolean;
roles: string;
add_user: string;
add_date: Date;
upd_user: string;
upd_date: Date;
}

4
server/types/roles.ts Normal file
View File

@@ -0,0 +1,4 @@
export interface Roles {
role: string;
module_id: string;
}

9
server/types/users.ts Normal file
View File

@@ -0,0 +1,9 @@
import type {Roles} from "./roles.js";
export type User = {
user_id?: string;
email?: string;
username?: string;
roles?: Roles[];
role?: string;
};