Files
latosa-escrima/frontend/lib/hasPermissions.ts
2025-02-19 16:16:47 +01:00

26 lines
588 B
TypeScript

import { Role } from "@/types/types";
export default function hasPermissions(
roles: Role[],
permissions: { [key: string]: string[] },
) {
const permissionsSet: Map<string, null> = new Map();
for (const role of roles) {
if (!role.permissions) continue;
for (const perm of role?.permissions) {
const key = perm.resource + ":" + perm.action;
permissionsSet.set(key, null);
}
}
for (const [perm, actions] of Object.entries(permissions)) {
for (const action of actions) {
if (!permissionsSet.has(perm + ":" + action)) {
return false;
}
}
}
return true;
}