Supra_App/src/lib/core/handlers/permissionHandler.ts
pakintada@gmail.com 451223816b init
2026-02-17 14:30:02 +07:00

41 lines
No EOL
1.3 KiB
TypeScript

import { get } from "svelte/store";
import { permission as currentPermissions } from "$lib/core/stores/permissions";
const splitPermCache = new Map<string, string[]>();
function splitPerm(p: string): string[]{
if(!splitPermCache.has(p)){
splitPermCache.set(p, p.split("."));
}
return splitPermCache.get(p)!;
}
/// Check if current user has exacted permissions
export function requirePermission(...permissions: string[]): boolean {
// let perms = get(currentPermissions);
// let countOk = 0;
// for(let perm of perms){
// if(permissions.includes(perm)){
// countOk += 1;
// }
// }
// return countOk > 0 && countOk == perms.length;
const userPerms = get(currentPermissions);
return permissions.every(req => {
return userPerms.includes(req);
});
}
/// Check permission of user by
export function needPermission(...permissions: string[]): boolean {
const userPerms = get(currentPermissions).map(p => splitPerm(p));
return permissions.every(req => {
const reqParts = splitPerm(req);
return userPerms.some(userParts => {
if(userParts.length !== reqParts.length) return false;
return reqParts.every((part, i) => part === "*" || part === userParts[i]);
});
});
}