- Added centralized API exports for admin functionalities. - Created permissions API for managing system permissions, including listing, creating, updating, and deleting permissions. - Developed role-permissions API for managing permissions assigned to roles, including listing, assigning, and removing permissions. - Implemented roles API for managing company roles, including listing, creating, updating, and deleting roles. - Created user-permissions API for managing individual user permissions, including assigning and revoking permissions. - Developed user-roles API for managing user roles, including listing roles assigned to users and assigning/removing roles. - Added utility functions for permission checks and route guards based on user permissions. - Implemented admin dashboard layout and roles management page with UI components for role and permission management.
72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
/**
|
|
* Helper para verificar permisos del usuario
|
|
* Basado en el sistema de permisos RBAC del backend
|
|
*/
|
|
|
|
import { get } from 'svelte/store';
|
|
import { page } from '$app/stores';
|
|
|
|
export interface UserPermission {
|
|
module: string;
|
|
action: string;
|
|
}
|
|
|
|
/**
|
|
* Verifica si el usuario tiene un permiso específico
|
|
* @param module - El módulo (ej: 'invoices', 'pedimentos')
|
|
* @param action - La acción (ej: 'create', 'update', 'delete', 'read')
|
|
* @returns true si el usuario tiene el permiso, false si no
|
|
*/
|
|
export function hasPermission(module: string, action: string): boolean {
|
|
// TODO: Implementar verificación real contra permisos del usuario
|
|
// Por ahora retorna true para permitir desarrollo
|
|
// En producción esto debe:
|
|
// 1. Obtener los permisos del usuario desde el contexto/store
|
|
// 2. Verificar si existe un permiso con module y action
|
|
// 3. Retornar true/false basado en la verificación
|
|
|
|
console.warn('hasPermission() no está implementado - retornando true por defecto');
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Verifica si el usuario tiene alguno de varios permisos
|
|
* @param permissions - Array de permisos a verificar
|
|
* @returns true si el usuario tiene al menos uno de los permisos
|
|
*/
|
|
export function hasAnyPermission(permissions: UserPermission[]): boolean {
|
|
return permissions.some(p => hasPermission(p.module, p.action));
|
|
}
|
|
|
|
/**
|
|
* Verifica si el usuario tiene todos los permisos especificados
|
|
* @param permissions - Array de permisos a verificar
|
|
* @returns true si el usuario tiene todos los permisos
|
|
*/
|
|
export function hasAllPermissions(permissions: UserPermission[]): boolean {
|
|
return permissions.every(p => hasPermission(p.module, p.action));
|
|
}
|
|
|
|
/**
|
|
* Guard para proteger rutas basado en permisos
|
|
* Puede ser usado en +page.server.ts o +layout.server.ts
|
|
* @param module - El módulo requerido
|
|
* @param action - La acción requerida
|
|
* @returns objeto con allowed (boolean) y redirect (string opcional)
|
|
*/
|
|
export function requirePermission(module: string, action: string): {
|
|
allowed: boolean;
|
|
redirect?: string;
|
|
} {
|
|
const allowed = hasPermission(module, action);
|
|
|
|
if (!allowed) {
|
|
return {
|
|
allowed: false,
|
|
redirect: '/dashboard?error=forbidden'
|
|
};
|
|
}
|
|
|
|
return { allowed: true };
|
|
}
|