Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.
import { useRouteData } from "@carbon/react";
import { useCallback } from "react";
import type { Permission } from "~/modules/users/types";
import type { Role } from "~/types";
import { path } from "~/utils/path";
import { useUser } from "./useUser";
export function usePermissions() {
const data = useRouteData<{
permissions: Record<string, Permission>;
role: "employee" | "supplier" | "customer";
}>(path.to.authenticatedRoot);
const {
id: userId,
company: { id: companyId, ownerId }
} = useUser();
if (!isPermissions(data?.permissions) || !isRole(data?.role)) {
// TODO: force logout -- the likely cause is development changes
throw new Error(
"usePermissions must be used within an authenticated route. If you are seeing this error, you are likely in development and have changed the session variables. Try deleting the cookies."
);
}
const can = useCallback(
(action: "view" | "create" | "update" | "delete", feature: string) => {
return data?.permissions[feature]?.[action].includes(companyId);
},
[companyId, data?.permissions]
);
const has = useCallback(
(feature: string) => {
return !!data?.permissions && feature in data.permissions;
},
[data?.permissions]
);
const is = useCallback(
(role: Role) => {
return data?.role === role;
},
[data?.role]
);
const isOwner = useCallback(() => {
return ownerId === userId;
}, [ownerId, userId]);
return {
can,
has,
is,
isOwner
};
}
function isPermissions(value: unknown): value is Record<string, Permission> {
if (
typeof value === "object" &&
Array.isArray(value) === false &&
value !== null
) {
return Object.values(value as object).every(
(permission) =>
"view" in permission &&
"create" in permission &&
"update" in permission &&
"delete" in permission
);
} else {
return false;
}
}
function isRole(value: unknown): value is Role {
return value === "employee" || value === "customer" || value === "supplier";
}