51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import { useMemo } from "react";
|
|
import { useGetPermissions } from "@/pages/roles/hooks/useRolesData";
|
|
import {
|
|
hasPermission,
|
|
hasAnyPermission,
|
|
hasAllPermissions,
|
|
} from "@/helpers/permissions";
|
|
|
|
/**
|
|
* Custom hook for managing user permissions
|
|
* @returns Object with permissions data and helper functions
|
|
*/
|
|
export const usePermissions = () => {
|
|
const { data: permissionsResponse, isLoading, error } = useGetPermissions();
|
|
|
|
const permissions = useMemo(() => {
|
|
return permissionsResponse?.data || [];
|
|
}, [permissionsResponse]);
|
|
|
|
/**
|
|
* Checks if user has a specific permission
|
|
*/
|
|
const checkPermission = (permissionName: string): boolean => {
|
|
return hasPermission(permissions, permissionName);
|
|
};
|
|
|
|
/**
|
|
* Checks if user has any of the specified permissions
|
|
*/
|
|
const checkAnyPermission = (permissionNames: string[]): boolean => {
|
|
return hasAnyPermission(permissions, permissionNames);
|
|
};
|
|
|
|
/**
|
|
* Checks if user has all of the specified permissions
|
|
*/
|
|
const checkAllPermissions = (permissionNames: string[]): boolean => {
|
|
return hasAllPermissions(permissions, permissionNames);
|
|
};
|
|
|
|
return {
|
|
permissions,
|
|
isLoading,
|
|
error,
|
|
checkPermission,
|
|
checkAnyPermission,
|
|
checkAllPermissions,
|
|
hasPermission: checkPermission,
|
|
};
|
|
};
|