react-antd-admin-panel
Version:
Modern TypeScript-first React admin panel builder with Ant Design 6
142 lines • 4.3 kB
JavaScript
import { useCallback, useMemo } from 'react';
import { useUser } from '../main/MainContext';
/**
* useAccess - React hook for permission and access control checks
*
* Provides functions to check user roles, permissions, and feature access.
* Integrates with the UserState from Main context.
*
* @returns Hook result with access check functions
*
* @example
* ```tsx
* // Basic role check
* const { hasRole, isAuthenticated } = useAccess();
* if (!isAuthenticated) return <LoginPage />;
* if (hasRole('admin')) return <AdminPanel />;
*
* // Permission check
* const { hasPermission } = useAccess();
* const canEdit = hasPermission('users.edit');
*
* // Feature-based access
* const { canAccess } = useAccess();
* if (canAccess({ feature: 'Users', level: 3 })) {
* // Show delete button
* }
*
* // Multiple roles
* const { hasAnyRole } = useAccess();
* if (hasAnyRole(['admin', 'manager'])) {
* // Show management features
* }
*
* // Combined check
* const { canAccess } = useAccess();
* <button disabled={!canAccess({ role: 'admin' })}>
* Admin Action
* </button>
* ```
*/
export function useAccess() {
const user = useUser();
/**
* Check if user has a specific role
*/
const hasRole = useCallback((role) => {
return user?.role === role;
}, [user?.role]);
/**
* Check if user has any of the specified roles
*/
const hasAnyRole = useCallback((roles) => {
if (!user?.role)
return false;
return roles.includes(user.role);
}, [user?.role]);
/**
* Check if user has a specific permission
*/
const hasPermission = useCallback((permission) => {
return user?.permissions?.includes(permission) ?? false;
}, [user?.permissions]);
/**
* Check if user has all specified permissions
*/
const hasAllPermissions = useCallback((permissions) => {
if (!user?.permissions)
return false;
return permissions.every(p => user.permissions.includes(p));
}, [user?.permissions]);
/**
* Check if user has any of the specified permissions
*/
const hasAnyPermission = useCallback((permissions) => {
if (!user?.permissions)
return false;
return permissions.some(p => user.permissions.includes(p));
}, [user?.permissions]);
/**
* Check if user has access to a feature with optional level
*
* Features are stored in user.features as Record<string, number>
* where the number represents the access level
*/
const hasFeature = useCallback((feature, level = 1) => {
if (!user?.features)
return false;
const userLevel = user.features[feature];
if (userLevel === undefined)
return false;
return userLevel >= level;
}, [user?.features]);
/**
* Unified access check supporting multiple check types
*/
const canAccess = useCallback((check) => {
// Not authenticated = no access
if (!user)
return false;
// Feature-based check
if ('feature' in check) {
return hasFeature(check.feature, check.level);
}
// Single role check
if ('role' in check) {
return hasRole(check.role);
}
// Multiple roles check (any)
if ('roles' in check) {
return hasAnyRole(check.roles);
}
// Single permission check
if ('permission' in check) {
return hasPermission(check.permission);
}
// Multiple permissions check (all)
if ('permissions' in check) {
return hasAllPermissions(check.permissions);
}
return false;
}, [user, hasFeature, hasRole, hasAnyRole, hasPermission, hasAllPermissions]);
/**
* Whether user is authenticated
*/
const isAuthenticated = useMemo(() => user !== null, [user]);
/**
* Current user's role
*/
const currentRole = useMemo(() => user?.role, [user?.role]);
return {
canAccess,
hasRole,
hasAnyRole,
hasPermission,
hasAllPermissions,
hasAnyPermission,
hasFeature,
isAuthenticated,
currentRole,
};
}
//# sourceMappingURL=useAccess.js.map