Solved an issue where, for developers, because of the amount of permissions, the cookie was too big, so I separated the permissions logic into a hook

This commit is contained in:
Tiago Ribeiro
2024-08-12 19:35:11 +01:00
parent cb489bf0ca
commit 58300e32ff
17 changed files with 3060 additions and 4025 deletions

View File

@@ -1,45 +1,42 @@
import { PermissionType } from "@/interfaces/permissions";
import { User, Type, userTypes } from "@/interfaces/user";
import {PermissionType} from "@/interfaces/permissions";
import {User, Type, userTypes} from "@/interfaces/user";
import axios from "axios";
export function checkAccess(
user: User,
types: Type[],
permission?: PermissionType
) {
if (!user) {
return false;
}
export function checkAccess(user: User, types: Type[], permissions?: PermissionType[], permission?: PermissionType) {
if (!user) {
return false;
}
// if(user.type === '') {
if (!user.type) {
console.warn("User type is empty");
return false;
}
// if(user.type === '') {
if (!user.type) {
console.warn("User type is empty");
return false;
}
if (types.length === 0) {
console.warn("No types provided");
return false;
}
if (types.length === 0) {
console.warn("No types provided");
return false;
}
if (!types.includes(user.type)) {
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 (!(user.permissions || []).includes(permission)) {
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;
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);
})
// basicly generate a list of all types except the excluded ones
return userTypes.filter((userType) => {
return !types.includes(userType);
});
}