test(auth): more auth work

This commit is contained in:
2025-03-01 15:22:34 -06:00
parent 6c3199fecc
commit d3acdfb481
12 changed files with 275 additions and 78 deletions

View File

@@ -1,52 +1,25 @@
interface User {
id: number;
import {Modules} from "@/types/modules";
type User = {
user_id: string;
email: string;
username: string;
role: keyof Roles;
}
roles: keyof Roles[];
role: string;
};
interface Roles {
[roleName: string]: RolePermissions;
role: string;
}
interface RolePermissions {
[moduleName: string]: Feature[];
}
type Feature = string;
const rolePermissions: Roles = {
admin: {
production: ["view", "manage", "update", "admin"],
logistics: ["view", "manage", "update", "admin"],
quality: ["view", "request", "manage", "update", "admin"],
forklift: ["view", "manage", "update", "admin"],
admin: ["view", "view_logs", "manage", "update", "admin"],
},
manager: {
production: ["view", "manage"],
logistics: ["view", "manage"],
quality: ["view", "manage"],
forklift: ["view", "manage"],
admin: ["view_logs"],
},
supervisor: {
production: ["view", "update"],
logistics: ["view", "update"],
quality: ["view", "update"],
forklift: ["view"],
admin: [],
},
user: {
production: ["view"],
logistics: ["view"],
quality: ["view"],
forklift: [],
admin: [],
},
};
// user will need access to the module.
// users role will determine there visual access
export function hasAccess(user: User | null, moduleName: string, feature: Feature): boolean {
return user?.role ? rolePermissions[user.role]?.[moduleName]?.includes(feature) || false : false;
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);
// userroles and filter out by the module id,
console.log(user);
return false;
}