import { get } from "svelte/store"; import { permission as currentPermissions } from "$lib/core/stores/permissions"; const splitPermCache = new Map(); 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]); }); }); }