60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import type { PermissionType } from '@/pages/roles/types/Types';
|
|
|
|
/**
|
|
* Checks if user has a specific permission
|
|
* @param permissions - Array of user permissions
|
|
* @param permissionName - Name of the permission to check
|
|
* @returns boolean indicating if user has the permission
|
|
*/
|
|
export const hasPermission = (
|
|
permissions: PermissionType[] | undefined,
|
|
permissionName: string
|
|
): boolean => {
|
|
if (!permissions || permissions.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
return permissions.some(
|
|
(permission) => permission.name === permissionName
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Checks if user has any of the specified permissions
|
|
* @param permissions - Array of user permissions
|
|
* @param permissionNames - Array of permission names to check
|
|
* @returns boolean indicating if user has at least one of the permissions
|
|
*/
|
|
export const hasAnyPermission = (
|
|
permissions: PermissionType[] | undefined,
|
|
permissionNames: string[]
|
|
): boolean => {
|
|
if (!permissions || permissions.length === 0 || permissionNames.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
return permissionNames.some((permissionName) =>
|
|
hasPermission(permissions, permissionName)
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Checks if user has all of the specified permissions
|
|
* @param permissions - Array of user permissions
|
|
* @param permissionNames - Array of permission names to check
|
|
* @returns boolean indicating if user has all of the permissions
|
|
*/
|
|
export const hasAllPermissions = (
|
|
permissions: PermissionType[] | undefined,
|
|
permissionNames: string[]
|
|
): boolean => {
|
|
if (!permissions || permissions.length === 0 || permissionNames.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
return permissionNames.every((permissionName) =>
|
|
hasPermission(permissions, permissionName)
|
|
);
|
|
};
|
|
|