c22b4a2613
NUOVE FUNZIONALITÀ: - Aggiunto modulo CredentialManager per gestione centralizzata credenziali - Implementata UI Blazor per gestione credenziali (CredentialManagement.razor) - Supporto completo per credenziali database (SQL Server, MySQL, PostgreSQL, Oracle, SQLite, DB2, SAP HANA) - Gestione unificata REST API con supporto specifico per SAP B1 Service Layer e Salesforce - Test reali di connessione per database, SAP B1 e Salesforce OAuth2 - Selezione dinamica tipo servizio REST (Generico, SAP B1, Salesforce) con campi specifici - Persistenza sicura di credenziali con crittografia password e campi sensibili COMPONENTI AGGIUNTI: - CredentialManager/Models/: CredentialEntity, CredentialModels (DatabaseCredential, RestApiCredential, SapB1ServiceLayerCredential, SalesforceCredential) - CredentialManager/Services/: CredentialService, EncryptionService, DatabaseInitializer - CredentialManager/Data/: CredentialDbContext con Entity Framework - DataConnection/CredentialManagement/: Interfacce e servizi di integrazione - Data_Coupler/Pages/CredentialManagement.razor: UI completa per gestione credenziali MIGLIORAMENTI UI: - Form dinamica per REST API con campi specifici per tipo servizio - Validazione campi obbligatori per Salesforce (ClientId, ClientSecret, SecurityToken) - Test connessione in tempo reale dalla modale di inserimento/modifica - Rimozione sezioni separate per SAP B1 e Salesforce (ora unificate in REST API) - Gestione stato loading durante operazioni async PERSISTENZA AVANZATA: - Campo RestServiceType aggiunto a CredentialEntity con migrazione automatica - Serializzazione campi specifici Salesforce/SAP B1 in AdditionalParameters JSON - Mapping bidirezionale tra entità database e modelli business - Gestione nullability e conversioni tipo sicure SICUREZZA: - Crittografia AES-256 per password e token sensibili - Gestione sicura ConnectionString database - Validazione input e sanitizzazione dati TESTING E CONNETTIVITÀ: - Test autenticazione reale SAP B1 Service Layer - Test OAuth2 Salesforce con supporto Connected App - Test connettività database multi-provider - Logging dettagliato per debugging e monitoraggio CONFIGURAZIONE: - Dependency injection per tutti i servizi - Configurazione Entity Framework con SQLite - Tasks VS Code per build e run - Gestione connection string centralizzata CORREZIONI: - Risolti errori nullability in CredentialService - Aggiunto using Microsoft.JSInterop per IJSRuntime - Fix compilazione e warning Files modificati: 35+ file tra nuovi e aggiornati
168 lines
5.2 KiB
C#
168 lines
5.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using CredentialManager.Data;
|
|
|
|
namespace CredentialManager.Services;
|
|
|
|
/// <summary>
|
|
/// Interfaccia per l'inizializzazione del database
|
|
/// </summary>
|
|
public interface IDatabaseInitializer
|
|
{
|
|
Task InitializeAsync();
|
|
Task<bool> DatabaseExistsAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Servizio per l'inizializzazione del database SQLite
|
|
/// </summary>
|
|
public class DatabaseInitializer : IDatabaseInitializer
|
|
{
|
|
private readonly CredentialDbContext _context;
|
|
private readonly ILogger<DatabaseInitializer> _logger;
|
|
|
|
public DatabaseInitializer(CredentialDbContext context, ILogger<DatabaseInitializer> logger)
|
|
{
|
|
_context = context;
|
|
_logger = logger;
|
|
} public async Task InitializeAsync()
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Inizializzazione database in corso...");
|
|
|
|
// Per SQLite con EnsureCreatedAsync è più semplice e sicuro
|
|
// Crea il database e le tabelle se non esistono
|
|
var created = await _context.Database.EnsureCreatedAsync();
|
|
|
|
if (created)
|
|
{
|
|
_logger.LogInformation("Database creato con successo");
|
|
|
|
// Aggiungi dati di esempio se necessario
|
|
await SeedInitialDataAsync();
|
|
}
|
|
else
|
|
{
|
|
_logger.LogInformation("Database esistente trovato");
|
|
|
|
// Verifica che le tabelle esistano
|
|
await VerifyTablesAsync();
|
|
|
|
// Applica migrazioni se necessario
|
|
await ApplyMigrationsAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Errore durante l'inizializzazione del database");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private async Task VerifyTablesAsync()
|
|
{
|
|
try
|
|
{
|
|
// Prova a fare una query semplice per verificare che la tabella esista
|
|
await _context.Credentials.CountAsync();
|
|
_logger.LogInformation("Verifica tabelle completata con successo");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning("Tabelle mancanti, ricreazione database...");
|
|
|
|
// Se le tabelle non esistono, le ricreiamo
|
|
await _context.Database.EnsureDeletedAsync();
|
|
await _context.Database.EnsureCreatedAsync();
|
|
await SeedInitialDataAsync();
|
|
|
|
_logger.LogInformation("Database ricreato con successo");
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DatabaseExistsAsync()
|
|
{
|
|
try
|
|
{
|
|
return await _context.Database.CanConnectAsync();
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private async Task SeedInitialDataAsync()
|
|
{
|
|
try
|
|
{
|
|
// Verifica se ci sono già dati
|
|
if (await _context.Credentials.AnyAsync())
|
|
{
|
|
_logger.LogInformation("Dati esistenti trovati. Seeding saltato.");
|
|
return;
|
|
}
|
|
|
|
_logger.LogInformation("Aggiunta dati di esempio...");
|
|
|
|
// Qui puoi aggiungere dati di esempio se necessario
|
|
// Ad esempio:
|
|
/*
|
|
var sampleCredentials = new List<CredentialEntity>
|
|
{
|
|
new CredentialEntity
|
|
{
|
|
Name = "Esempio Database",
|
|
Type = "Database",
|
|
Host = "localhost",
|
|
Port = 1433,
|
|
DatabaseName = "TestDB",
|
|
Username = "testuser",
|
|
CreatedAt = DateTime.UtcNow,
|
|
CreatedBy = "System",
|
|
IsActive = true
|
|
}
|
|
};
|
|
|
|
_context.Credentials.AddRange(sampleCredentials);
|
|
await _context.SaveChangesAsync();
|
|
*/
|
|
|
|
_logger.LogInformation("Dati di esempio aggiunti con successo");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Errore durante il seeding dei dati iniziali");
|
|
throw;
|
|
}
|
|
} private async Task ApplyMigrationsAsync()
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Verifica e applicazione migrazioni...");
|
|
|
|
// Verifica se la colonna RestServiceType esiste usando una query diretta
|
|
try
|
|
{
|
|
await _context.Database.ExecuteSqlRawAsync(
|
|
"SELECT RestServiceType FROM Credentials LIMIT 1");
|
|
_logger.LogInformation("Colonna RestServiceType già presente");
|
|
}
|
|
catch (Microsoft.Data.Sqlite.SqliteException)
|
|
{
|
|
// La colonna non esiste, la aggiungiamo
|
|
_logger.LogInformation("Aggiunta colonna RestServiceType...");
|
|
await _context.Database.ExecuteSqlRawAsync(
|
|
"ALTER TABLE Credentials ADD COLUMN RestServiceType TEXT");
|
|
_logger.LogInformation("Colonna RestServiceType aggiunta con successo");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Errore nell'applicazione delle migrazioni");
|
|
throw;
|
|
}
|
|
}
|
|
}
|