Add decisiones de autorización puras

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Carlos Narro
2026-05-30 19:35:41 +02:00
parent 4e4cc8545e
commit 2cc19147ff
2 changed files with 73 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
export type Role = 'reformista' | 'admin';
export type UserStatus = 'activo' | 'deshabilitado';
export type AuthUser = {
id: string;
email: string;
nombre: string | null;
role: Role;
tenantId: string | null;
status: UserStatus;
};
export function isAdmin(user: AuthUser): boolean {
return user.role === 'admin';
}
export function assertAdmin(user: AuthUser | null): asserts user is AuthUser {
if (!user || user.role !== 'admin') {
throw new Error('Acceso restringido a administradores.');
}
}
export function resolveTenantId(user: AuthUser | null): string {
if (!user || !user.tenantId) {
throw new Error('El usuario no tiene un tenant asociado.');
}
return user.tenantId;
}
export function canAccessTenant(user: AuthUser, tenantId: string): boolean {
if (user.role === 'admin') return true;
return user.tenantId === tenantId;
}