Files
btpxpress-frontend/types/auth.ts
2025-10-13 05:29:32 +02:00

212 lines
4.8 KiB
TypeScript

/**
* Types d'authentification pour BTP Xpress
* Synchronisé avec le backend Java et Keycloak
*/
// Énumération des rôles utilisateur (synchronisée avec UserRole.java)
export enum UserRole {
ADMIN = 'ADMIN',
MANAGER = 'MANAGER',
CHEF_CHANTIER = 'CHEF_CHANTIER',
OUVRIER = 'OUVRIER',
COMPTABLE = 'COMPTABLE',
GESTIONNAIRE_PROJET = 'GESTIONNAIRE_PROJET',
CLIENT = 'CLIENT'
}
// Statut utilisateur (synchronisé avec UserStatus.java)
export enum UserStatus {
PENDING = 'PENDING',
APPROVED = 'APPROVED',
REJECTED = 'REJECTED',
SUSPENDED = 'SUSPENDED',
ACTIVE = 'ACTIVE',
INACTIVE = 'INACTIVE'
}
// Interface utilisateur complète
export interface User {
id: string;
email: string;
nom: string;
prenom: string;
username: string;
firstName?: string;
lastName?: string;
fullName?: string;
role: UserRole;
roles: string[];
permissions: string[];
highestRole?: string;
actif: boolean;
status: UserStatus;
telephone?: string;
adresse?: string;
codePostal?: string;
ville?: string;
entreprise?: string;
siret?: string;
secteurActivite?: string;
effectif?: number;
commentaireAdmin?: string;
dateCreation: Date;
dateModification: Date;
derniereConnexion?: Date;
// Propriétés calculées pour la compatibilité
isAdmin: boolean;
isManager: boolean;
isEmployee: boolean;
isClient: boolean;
}
// Interface pour les informations utilisateur simplifiées (AuthContext)
export interface UserInfo {
id: string;
username: string;
email: string;
firstName?: string;
lastName?: string;
fullName?: string;
roles: string[];
permissions: string[];
highestRole?: string;
isAdmin: boolean;
isManager: boolean;
isEmployee: boolean;
isClient: boolean;
}
// État d'authentification
export interface AuthState {
isAuthenticated: boolean;
isLoading: boolean;
user: UserInfo | null;
token: string | null;
refreshToken: string | null;
error: string | null;
}
// Contexte d'authentification
export interface AuthContextType extends AuthState {
login: () => Promise<void>;
logout: () => Promise<void>;
refreshAuth: () => Promise<void>;
hasRole: (role: string) => boolean;
hasAnyRole: (roles: string[]) => boolean;
hasPermission: (permission: string) => boolean;
isRoleHigher: (role: string) => boolean;
updateToken: (minValidity?: number) => Promise<boolean>;
}
// Permissions système
export interface Permission {
id: string;
name: string;
description: string;
resource: string;
action: string;
}
// Données de connexion
export interface LoginCredentials {
email: string;
password: string;
}
// Réponse d'authentification
export interface AuthResponse {
user: UserInfo;
token: string;
refreshToken: string;
expiresIn: number;
}
// Données d'inscription
export interface RegisterData {
email: string;
nom: string;
prenom: string;
password: string;
telephone?: string;
adresse?: string;
codePostal?: string;
ville?: string;
entreprise?: string;
siret?: string;
secteurActivite?: string;
effectif?: number;
}
// Données de profil utilisateur
export interface UserProfile {
id: string;
email: string;
nom: string;
prenom: string;
telephone?: string;
adresse?: string;
codePostal?: string;
ville?: string;
entreprise?: string;
siret?: string;
secteurActivite?: string;
effectif?: number;
}
// Changement de mot de passe
export interface PasswordChangeData {
currentPassword: string;
newPassword: string;
confirmPassword: string;
}
// Reset de mot de passe
export interface PasswordResetData {
email: string;
}
export interface PasswordResetConfirmData {
token: string;
newPassword: string;
confirmPassword: string;
}
// Utilitaires de rôles
export const RoleHierarchy: Record<UserRole, number> = {
[UserRole.ADMIN]: 1,
[UserRole.MANAGER]: 2,
[UserRole.GESTIONNAIRE_PROJET]: 3,
[UserRole.CHEF_CHANTIER]: 4,
[UserRole.COMPTABLE]: 4,
[UserRole.OUVRIER]: 5,
[UserRole.CLIENT]: 6
};
export const RoleDisplayNames: Record<UserRole, string> = {
[UserRole.ADMIN]: 'Administrateur',
[UserRole.MANAGER]: 'Manager',
[UserRole.GESTIONNAIRE_PROJET]: 'Gestionnaire de projet',
[UserRole.CHEF_CHANTIER]: 'Chef de chantier',
[UserRole.COMPTABLE]: 'Comptable',
[UserRole.OUVRIER]: 'Ouvrier',
[UserRole.CLIENT]: 'Client'
};
// Fonctions utilitaires
export const isRoleHigher = (role1: UserRole, role2: UserRole): boolean => {
return RoleHierarchy[role1] < RoleHierarchy[role2];
};
export const isManagementRole = (role: UserRole): boolean => {
return [UserRole.ADMIN, UserRole.MANAGER, UserRole.GESTIONNAIRE_PROJET].includes(role);
};
export const isFieldRole = (role: UserRole): boolean => {
return [UserRole.CHEF_CHANTIER, UserRole.OUVRIER].includes(role);
};
export const isAdministrativeRole = (role: UserRole): boolean => {
return [UserRole.ADMIN, UserRole.COMPTABLE].includes(role);
};