Availability based on permissions
This commit is contained in:
25
frontend/lib/hasPermissions.ts
Normal file
25
frontend/lib/hasPermissions.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user