Creata pagina login con Syncfusion

This commit is contained in:
2025-11-27 22:23:17 +01:00
parent e528927d96
commit 9369b70e2d
16 changed files with 1389 additions and 142 deletions

View File

@@ -0,0 +1,155 @@
using Microsoft.AspNetCore.Identity;
using SmartDB.Components.Admin.Dtos;
using SmartDB.Data;
namespace SmartDB.Components.Admin.Services
{
/// <summary>
/// Servizio per la gestione degli utenti dell'applicazione
/// </summary>
public interface IUserManagementService
{
/// <summary>
/// Crea un nuovo utente con il ruolo specificato
/// </summary>
Task<(bool Success, string Message)> CreateUserAsync(CreateUserDto dto);
/// <summary>
/// Recupera tutti gli utenti
/// </summary>
Task<List<ApplicationUser>> GetAllUsersAsync();
/// <summary>
/// Elimina un utente
/// </summary>
Task<(bool Success, string Message)> DeleteUserAsync(string userId);
/// <summary>
/// Disabilita/abilita un utente
/// </summary>
Task<(bool Success, string Message)> ToggleUserStatusAsync(string userId);
}
/// <summary>
/// Implementazione del servizio di gestione degli utenti
/// </summary>
public class UserManagementService : IUserManagementService
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public UserManagementService(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public async Task<(bool Success, string Message)> CreateUserAsync(CreateUserDto dto)
{
try
{
// Verifica se l'email <20> gi<67> in uso
var existingUser = await _userManager.FindByEmailAsync(dto.Email);
if (existingUser != null)
{
return (false, "Un utente con questa email esiste gi<67>");
}
// Crea il nuovo utente
var user = new ApplicationUser
{
UserName = dto.Email,
Email = dto.Email,
FirstName = dto.FirstName,
LastName = dto.LastName,
EmailConfirmed = true,
IsActive = true
};
var result = await _userManager.CreateAsync(user, dto.Password);
if (!result.Succeeded)
{
var errors = string.Join(", ", result.Errors.Select(e => e.Description));
return (false, $"Errore nella creazione dell'utente: {errors}");
}
// Assegna il ruolo
var roleExist = await _roleManager.RoleExistsAsync(dto.Role);
if (!roleExist)
{
dto.Role = "User"; // Fallback al ruolo User
}
var roleResult = await _userManager.AddToRoleAsync(user, dto.Role);
if (!roleResult.Succeeded)
{
var errors = string.Join(", ", roleResult.Errors.Select(e => e.Description));
return (false, $"Errore nell'assegnazione del ruolo: {errors}");
}
return (true, "Utente creato con successo");
}
catch (Exception ex)
{
return (false, $"Errore: {ex.Message}");
}
}
public async Task<List<ApplicationUser>> GetAllUsersAsync()
{
return _userManager.Users.ToList();
}
public async Task<(bool Success, string Message)> DeleteUserAsync(string userId)
{
try
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return (false, "Utente non trovato");
}
var result = await _userManager.DeleteAsync(user);
if (!result.Succeeded)
{
var errors = string.Join(", ", result.Errors.Select(e => e.Description));
return (false, $"Errore nell'eliminazione: {errors}");
}
return (true, "Utente eliminato con successo");
}
catch (Exception ex)
{
return (false, $"Errore: {ex.Message}");
}
}
public async Task<(bool Success, string Message)> ToggleUserStatusAsync(string userId)
{
try
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return (false, "Utente non trovato");
}
user.IsActive = !user.IsActive;
var result = await _userManager.UpdateAsync(user);
if (!result.Succeeded)
{
var errors = string.Join(", ", result.Errors.Select(e => e.Description));
return (false, $"Errore nell'aggiornamento: {errors}");
}
return (true, $"Utente {(user.IsActive ? "abilitato" : "disabilitato")} con successo");
}
catch (Exception ex)
{
return (false, $"Errore: {ex.Message}");
}
}
}
}