permission sidebar

This commit is contained in:
hamid zarghami
2025-12-27 09:51:58 +03:30
parent 0efaa5c5af
commit 23c8e973f2
4 changed files with 299 additions and 131 deletions
+59
View File
@@ -0,0 +1,59 @@
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)
);
};