@page "/credential-migration" @using CredentialManager.Services @using CredentialManager.Models @inject ICredentialService CredentialService @inject NavigationManager Navigation @inject IJSRuntime JSRuntime @inject ILogger Logger Migrazione Credenziali

Migrazione Credenziali Richiesta

@if (isLoading) {
Caricamento...
} else if (problematicCredentials.Any()) {
Credenziali che richiedono re-inserimento:
@foreach (var credGroup in problematicCredentials.GroupBy(c => c.Type)) {
@GetCredentialTypeDisplayName(credGroup.Key)
@foreach (var cred in credGroup) {
@cred.Name

@cred.Description

@if (cred.Type == CredentialType.Database) { var editModel = GetEditModel(cred.Name);
} else if (cred.Type == CredentialType.RestApi) { var editModel = GetEditModel(cred.Name);
@if (IsSpecificServiceType(cred, "Salesforce")) {
}
}
}
} } else { }
@code { private bool isLoading = true; private List problematicCredentials = new(); private Dictionary editModels = new(); protected override async Task OnInitializedAsync() { await LoadProblematicCredentials(); isLoading = false; } private async Task LoadProblematicCredentials() { try { problematicCredentials.Clear(); // Carica tutte le credenziali e verifica quelle problematiche var allCredentials = new List(); // Database credentials try { var dbCreds = await CredentialService.GetAllDatabaseCredentialsAsync(); allCredentials.AddRange(dbCreds.Select(c => new CredentialSummary { Name = c.Name, Type = CredentialType.Database, Description = $"Database: {c.DatabaseName} su {c.Host}", HasProblematicPassword = NeedsReentry(c.Password) })); } catch (Exception ex) { Logger.LogError(ex, "Errore nel caricamento credenziali database"); } // REST API credentials try { var restCreds = await CredentialService.GetAllRestApiCredentialsAsync(); allCredentials.AddRange(restCreds.Select(c => new CredentialSummary { Name = c.Name, Type = CredentialType.RestApi, Description = $"Servizio: {c.ServiceType} - {c.BaseUrl}", ServiceType = c.ServiceType.ToString(), HasProblematicPassword = NeedsReentry(c.Password ?? ""), HasProblematicApiKey = NeedsReentry(c.ApiKey ?? ""), HasProblematicClientSecret = NeedsReentry(c.ClientSecret ?? "") })); } catch (Exception ex) { Logger.LogError(ex, "Errore nel caricamento credenziali REST API"); } problematicCredentials = allCredentials .Where(c => c.HasProblematicPassword || c.HasProblematicApiKey || c.HasProblematicClientSecret) .ToList(); // Inizializza i modelli di editing foreach (var cred in problematicCredentials) { editModels[cred.Name] = new CredentialEditModel(); } } catch (Exception ex) { Logger.LogError(ex, "Errore generale nel caricamento delle credenziali"); } } private bool NeedsReentry(string credentialValue) { return !string.IsNullOrEmpty(credentialValue) && (credentialValue.Contains("*** CREDENZIALI NON DISPONIBILI") || credentialValue.Contains("*** ERRORE DECRITTOGRAFIA ***")); } private string GetCredentialTypeDisplayName(CredentialType type) { return type switch { CredentialType.Database => "Credenziali Database", CredentialType.RestApi => "Credenziali REST API", _ => type.ToString() }; } private bool IsSpecificServiceType(CredentialSummary cred, string serviceType) { return cred.ServiceType?.Contains(serviceType, StringComparison.OrdinalIgnoreCase) == true; } private CredentialEditModel GetEditModel(string credentialName) { if (!editModels.ContainsKey(credentialName)) editModels[credentialName] = new CredentialEditModel(); return editModels[credentialName]; } private async Task UpdateDatabaseCredential(string credentialName) { try { var editModel = GetEditModel(credentialName); if (string.IsNullOrEmpty(editModel.Password)) { await JSRuntime.InvokeVoidAsync("alert", "La password è obbligatoria"); return; } // Trova la credenziale esistente var existingCred = (await CredentialService.GetAllDatabaseCredentialsAsync()) .FirstOrDefault(c => c.Name == credentialName); if (existingCred != null) { // Aggiorna solo la password existingCred.Password = editModel.Password; await CredentialService.SaveDatabaseCredentialAsync(existingCred); await JSRuntime.InvokeVoidAsync("alert", "Credenziali aggiornate con successo!"); await LoadProblematicCredentials(); StateHasChanged(); } } catch (Exception ex) { Logger.LogError(ex, "Errore nell'aggiornamento delle credenziali database: {Name}", credentialName); await JSRuntime.InvokeVoidAsync("alert", $"Errore nell'aggiornamento: {ex.Message}"); } } private async Task UpdateRestCredential(string credentialName) { try { var editModel = GetEditModel(credentialName); // Trova la credenziale esistente var existingCred = (await CredentialService.GetAllRestApiCredentialsAsync()) .FirstOrDefault(c => c.Name == credentialName); if (existingCred != null) { // Aggiorna i campi forniti if (!string.IsNullOrEmpty(editModel.Password)) existingCred.Password = editModel.Password; if (!string.IsNullOrEmpty(editModel.ApiKey)) existingCred.ApiKey = editModel.ApiKey; if (!string.IsNullOrEmpty(editModel.ClientSecret)) existingCred.ClientSecret = editModel.ClientSecret; if (!string.IsNullOrEmpty(editModel.SecurityToken)) existingCred.SecurityToken = editModel.SecurityToken; await CredentialService.SaveRestApiCredentialAsync(existingCred); await JSRuntime.InvokeVoidAsync("alert", "Credenziali aggiornate con successo!"); await LoadProblematicCredentials(); StateHasChanged(); } } catch (Exception ex) { Logger.LogError(ex, "Errore nell'aggiornamento delle credenziali REST: {Name}", credentialName); await JSRuntime.InvokeVoidAsync("alert", $"Errore nell'aggiornamento: {ex.Message}"); } } public class CredentialSummary { public string Name { get; set; } = ""; public CredentialType Type { get; set; } public string Description { get; set; } = ""; public string? ServiceType { get; set; } public bool HasProblematicPassword { get; set; } public bool HasProblematicApiKey { get; set; } public bool HasProblematicClientSecret { get; set; } } public class CredentialEditModel { public string? Password { get; set; } public string? ApiKey { get; set; } public string? ClientSecret { get; set; } public string? SecurityToken { get; set; } } }