60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import { useStore } from './store';
|
|
|
|
export type UserRole = 'user' | 'admin' | 'super_admin';
|
|
|
|
interface User {
|
|
id: string;
|
|
email: string;
|
|
name: string;
|
|
role: UserRole;
|
|
avatar_url?: string;
|
|
}
|
|
|
|
// Check if user has admin access
|
|
export function isAdmin(user: User | null): boolean {
|
|
if (!user) return false;
|
|
return user.role === 'admin' || user.role === 'super_admin';
|
|
}
|
|
|
|
// Check if user has super admin access
|
|
export function isSuperAdmin(user: User | null): boolean {
|
|
if (!user) return false;
|
|
return user.role === 'super_admin';
|
|
}
|
|
|
|
// Check if user can access a specific feature
|
|
export function canAccess(user: User | null, feature: string): boolean {
|
|
if (!user) return false;
|
|
|
|
const adminOnlyFeatures = [
|
|
'admin_panel',
|
|
'usage_reporting',
|
|
'user_management',
|
|
'api_keys_management',
|
|
'system_settings',
|
|
'audit_logs',
|
|
];
|
|
|
|
if (adminOnlyFeatures.includes(feature)) {
|
|
return isAdmin(user);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Hook for checking admin status
|
|
export function useIsAdmin(): boolean {
|
|
const { user } = useStore();
|
|
return isAdmin(user as User | null);
|
|
}
|
|
|
|
// Hook for checking super admin status
|
|
export function useIsSuperAdmin(): boolean {
|
|
const { user } = useStore();
|
|
return isSuperAdmin(user as User | null);
|
|
}
|
|
|
|
// Admin access check function - use AdminGuard component instead of HOC
|
|
export function requireAdmin(user: User | null): boolean {
|
|
return isAdmin(user);
|
|
}
|