Availability based on permissions

This commit is contained in:
cdricms
2025-02-19 16:16:47 +01:00
parent 446813315d
commit 2011ae93b6
26 changed files with 1071 additions and 794 deletions

View File

@@ -0,0 +1,25 @@
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;
}