add permission check

This commit is contained in:
smanylov
2026-05-19 16:54:20 +07:00
parent ced17ae4a6
commit 5117878185
8 changed files with 237 additions and 18 deletions
+153
View File
@@ -0,0 +1,153 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { fetchEmployeInfo } from '@/app/actions/stuffActions';
export type PermissionKey =
| 'subscriptions'
| 'mailings'
| 'complaints'
| 'agreements'
| 'staff'
| 'content_moderation'
| 'users'
| 'kyc'
| 'money'
| 'claims'
| 'api'
| 'dash'
| 'tariffs';
export type PermissionLevel = 0 | 1 | 3;
export interface UserData {
id: number;
full_name: string;
email: string;
is_active: boolean;
is_super_admin: boolean;
created_at: string;
active_password: boolean;
permissions: Record<PermissionKey, PermissionLevel>;
}
interface UseEmployeInfoReturn {
data: UserData | null;
isLoading: boolean;
isError: boolean;
error: Error | null;
isFetching: boolean;
refetch: () => void;
hasPermission: (key: PermissionKey, requiredLevel?: PermissionLevel) => boolean;
getPermissionLevel: (key: PermissionKey) => PermissionLevel;
canRead: (key: PermissionKey) => boolean;
canFullAccess: (key: PermissionKey) => boolean;
hasAllPermissions: (permissions: Partial<Record<PermissionKey, PermissionLevel>>) => boolean;
hasAnyPermission: (permissions: Partial<Record<PermissionKey, PermissionLevel>>) => boolean;
getPermissionsArray: () => Array<{ key: PermissionKey; level: PermissionLevel; label: string }>;
isSuperAdmin: boolean;
}
const PERMISSION_LABELS: Record<PermissionKey, string> = {
subscriptions: 'Подписки',
mailings: 'Рассылки',
complaints: 'Жалобы',
agreements: 'Соглашения',
staff: 'Сотрудники',
content_moderation: 'Модерация контента',
users: 'Пользователи',
kyc: 'Верификация',
money: 'Финансы',
claims: 'Претензии',
api: 'API',
dash: 'Дешборд',
tariffs: 'Тарифы'
};
export const useEmployeInfo = (options?: UseQueryOptions<UserData | null, Error>) => {
const query = useQuery({
queryKey: ['employeInfo'],
queryFn: () => fetchEmployeInfo(),
select: (data: UserData | null) => data,
staleTime: 5 * 60 * 1000,
gcTime: 5 * 60 * 1000,
retry: 1,
refetchOnWindowFocus: true,
refetchOnMount: true,
...options
});
const permissions = query.data?.permissions || null;
const hasPermission = (key: PermissionKey, requiredLevel: PermissionLevel = 1): boolean => {
if (!permissions) return false;
const userLevel = permissions[key] || 0;
return userLevel >= requiredLevel;
};
const getPermissionLevel = (key: PermissionKey): PermissionLevel => {
if (!permissions) return 0;
const level = permissions[key] || 0;
if (level === 3) return 3;
if (level === 1) return 1;
return 0;
};
const canRead = (key: PermissionKey): boolean => hasPermission(key, 1);
const canFullAccess = (key: PermissionKey): boolean => hasPermission(key, 3);
const hasAllPermissions = (requiredPermissions: Partial<Record<PermissionKey, PermissionLevel>>): boolean => {
if (!permissions) return false;
for (const [key, requiredLevel] of Object.entries(requiredPermissions)) {
const userLevel = permissions[key as PermissionKey] || 0;
if (userLevel < (requiredLevel as PermissionLevel)) {
return false;
}
}
return true;
};
const hasAnyPermission = (requiredPermissions: Partial<Record<PermissionKey, PermissionLevel>>): boolean => {
if (!permissions) return false;
for (const [key, requiredLevel] of Object.entries(requiredPermissions)) {
const userLevel = permissions[key as PermissionKey] || 0;
if (userLevel >= (requiredLevel as PermissionLevel)) {
return true;
}
}
return false;
};
const getPermissionsArray = (): Array<{ key: PermissionKey; level: PermissionLevel; label: string }> => {
if (!permissions) return [];
return (Object.keys(PERMISSION_LABELS) as PermissionKey[]).map(key => ({
key,
level: permissions[key] || 0,
label: PERMISSION_LABELS[key]
}));
};
return {
data: query.data,
isLoading: query.isLoading,
isError: query.isError,
error: query.error as Error | null,
isFetching: query.isFetching,
refetch: query.refetch,
hasPermission,
getPermissionLevel,
canRead,
canFullAccess,
hasAllPermissions,
hasAnyPermission,
getPermissionsArray,
isSuperAdmin: query.data?.is_super_admin || false,
};
};