mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { useAuth } from '@/contexts/AuthContext';
|
|
import { Permission, Role, hasPermission, hasAnyPermission, hasAllPermissions } from '@/types/permissions';
|
|
|
|
/**
|
|
* Hook for checking user permissions
|
|
*/
|
|
export const usePermissions = () => {
|
|
const { user } = useAuth();
|
|
const role: Role = (user?.role as Role) || 'user';
|
|
|
|
const checkPermission = useMemo(
|
|
() => (permission: Permission) => {
|
|
return hasPermission(role, permission);
|
|
},
|
|
[role]
|
|
);
|
|
|
|
const checkAnyPermission = useMemo(
|
|
() => (permissions: Permission[]) => {
|
|
return hasAnyPermission(role, permissions);
|
|
},
|
|
[role]
|
|
);
|
|
|
|
const checkAllPermissions = useMemo(
|
|
() => (permissions: Permission[]) => {
|
|
return hasAllPermissions(role, permissions);
|
|
},
|
|
[role]
|
|
);
|
|
|
|
const isAdmin = useMemo(() => role === 'admin', [role]);
|
|
const isContentManager = useMemo(() => role === 'content_manager', [role]);
|
|
const isViewer = useMemo(() => role === 'viewer', [role]);
|
|
const isRegularUser = useMemo(() => role === 'user', [role]);
|
|
|
|
return {
|
|
role,
|
|
checkPermission,
|
|
checkAnyPermission,
|
|
checkAllPermissions,
|
|
isAdmin,
|
|
isContentManager,
|
|
isViewer,
|
|
isRegularUser,
|
|
hasPermission: checkPermission,
|
|
hasAnyPermission: checkAnyPermission,
|
|
hasAllPermissions: checkAllPermissions,
|
|
};
|
|
};
|
|
|