init
This commit is contained in:
commit
451223816b
338 changed files with 9938 additions and 0 deletions
41
src/lib/core/handlers/permissionHandler.ts
Normal file
41
src/lib/core/handlers/permissionHandler.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
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]);
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue