Files
encoach_frontend/src/utils/permissions.ts

41 lines
970 B
TypeScript

import {PermissionType} from "@/interfaces/permissions";
import {User, Type, userTypes} from "@/interfaces/user";
import axios from "axios";
export function checkAccess(user: User, types: Type[], permissions?: PermissionType[], permission?: PermissionType) {
if (!user) {
return false;
}
// if(user.type === '') {
if (!user.type) {
return false;
}
if (types.length === 0) {
return false;
}
if (!types.includes(user.type)) {
return false;
}
// we may not want a permission check as most screens dont even havr a specific permission
if (permission) {
// this works more like a blacklist
// therefore if we don't find the permission here, he can't do it
if (!(permissions || []).includes(permission)) {
return false;
}
}
return true;
}
export function getTypesOfUser(types: Type[]) {
// basicly generate a list of all types except the excluded ones
return userTypes.filter((userType) => {
return !types.includes(userType);
});
}