react-antd-admin-panel
Version:
Modern TypeScript-first React admin panel builder with Ant Design 6
84 lines • 2.55 kB
TypeScript
/**
* Feature-based access check configuration
*/
export interface FeatureAccess {
/** Feature name/identifier */
feature: string;
/** Required access level (higher = more permissions) */
level?: number;
}
/**
* Access check configuration - can be feature-based or role-based
*/
export type AccessCheck = FeatureAccess | {
role: string;
} | {
roles: string[];
} | {
permission: string;
} | {
permissions: string[];
};
/**
* Return type for the useAccess hook
*/
export interface UseAccessResult {
/** Check if user can access based on configuration */
canAccess: (check: AccessCheck) => boolean;
/** Check if user has a specific role */
hasRole: (role: string) => boolean;
/** Check if user has any of the specified roles */
hasAnyRole: (roles: string[]) => boolean;
/** Check if user has a specific permission */
hasPermission: (permission: string) => boolean;
/** Check if user has all specified permissions */
hasAllPermissions: (permissions: string[]) => boolean;
/** Check if user has any of the specified permissions */
hasAnyPermission: (permissions: string[]) => boolean;
/** Check if user has access to a feature with optional level */
hasFeature: (feature: string, level?: number) => boolean;
/** Whether user is authenticated */
isAuthenticated: boolean;
/** Current user's role */
currentRole: string | undefined;
}
/**
* 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 declare function useAccess(): UseAccessResult;
//# sourceMappingURL=useAccess.d.ts.map