266 lines
9.9 KiB
Plaintext
266 lines
9.9 KiB
Plaintext
@page "/admin/users"
|
|
@using Microsoft.AspNetCore.Authorization
|
|
@using SmartDB.Components.Admin.Dtos
|
|
@using SmartDB.Components.Admin.Services
|
|
@using SmartDB.Data
|
|
@attribute [Authorize(Policy = "AdminOnly")]
|
|
|
|
@inject IUserManagementService UserService
|
|
@inject ILogger<Users> Logger
|
|
|
|
<PageTitle>Gestione Utenti - Admin</PageTitle>
|
|
|
|
<h1>Gestione Utenti</h1>
|
|
|
|
@if (!string.IsNullOrEmpty(successMessage))
|
|
{
|
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
|
@successMessage
|
|
<button type="button" class="btn-close" @onclick="@(() => successMessage = string.Empty)" aria-label="Close"></button>
|
|
</div>
|
|
}
|
|
|
|
@if (!string.IsNullOrEmpty(errorMessage))
|
|
{
|
|
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
|
@errorMessage
|
|
<button type="button" class="btn-close" @onclick="@(() => errorMessage = string.Empty)" aria-label="Close"></button>
|
|
</div>
|
|
}
|
|
|
|
<ul class="nav nav-tabs mb-3" role="tablist">
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link @(activeTab == "list" ? "active" : "")" @onclick="@(() => activeTab = "list")" type="button" role="tab" aria-selected="@(activeTab == "list")">
|
|
Elenco Utenti
|
|
</button>
|
|
</li>
|
|
<li class="nav-item" role="presentation">
|
|
<button class="nav-link @(activeTab == "create" ? "active" : "")" @onclick="@(() => activeTab = "create")" type="button" role="tab" aria-selected="@(activeTab == "create")">
|
|
Aggiungi Utente
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
|
|
<div class="tab-content">
|
|
@if (activeTab == "list")
|
|
{
|
|
<div class="tab-pane fade show active">
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<th>Email</th>
|
|
<th>Nome</th>
|
|
<th>Cognome</th>
|
|
<th>Ruolo</th>
|
|
<th>Stato</th>
|
|
<th>Data Creazione</th>
|
|
<th>Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@if (users != null && users.Any())
|
|
{
|
|
@foreach (var user in users)
|
|
{
|
|
<tr>
|
|
<td>@user.Email</td>
|
|
<td>@user.FirstName</td>
|
|
<td>@user.LastName</td>
|
|
<td>
|
|
<span class="badge bg-info">@GetUserRole(user.Id)</span>
|
|
</td>
|
|
<td>
|
|
@if (user.IsActive)
|
|
{
|
|
<span class="badge bg-success">Attivo</span>
|
|
}
|
|
else
|
|
{
|
|
<span class="badge bg-danger">Disattivo</span>
|
|
}
|
|
</td>
|
|
<td>@user.CreatedAt.ToShortDateString()</td>
|
|
<td>
|
|
<button class="btn btn-sm btn-warning" @onclick="@(() => ToggleUserStatus(user.Id))" title="Cambia stato">
|
|
@(user.IsActive ? "Disabilita" : "Abilita")
|
|
</button>
|
|
<button class="btn btn-sm btn-danger" @onclick="@(() => DeleteUser(user.Id))" title="Elimina">
|
|
Elimina
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
}
|
|
}
|
|
else
|
|
{
|
|
<tr>
|
|
<td colspan="7" class="text-center">Nessun utente trovato</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@if (activeTab == "create")
|
|
{
|
|
<div class="tab-pane fade show active">
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<h3>Aggiungi Nuovo Utente</h3>
|
|
<EditForm Model="newUser" OnValidSubmit="HandleCreateUser">
|
|
<DataAnnotationsValidator />
|
|
<ValidationSummary class="text-danger" />
|
|
|
|
<div class="mb-3">
|
|
<label for="email" class="form-label">Email</label>
|
|
<InputText id="email" class="form-control" @bind-Value="newUser.Email" placeholder="utente@example.com" />
|
|
<ValidationMessage For="@(() => newUser.Email)" />
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="firstName" class="form-label">Nome</label>
|
|
<InputText id="firstName" class="form-control" @bind-Value="newUser.FirstName" placeholder="Mario" />
|
|
<ValidationMessage For="@(() => newUser.FirstName)" />
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="lastName" class="form-label">Cognome</label>
|
|
<InputText id="lastName" class="form-control" @bind-Value="newUser.LastName" placeholder="Rossi" />
|
|
<ValidationMessage For="@(() => newUser.LastName)" />
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="password" class="form-label">Password Temporanea</label>
|
|
<InputText type="password" id="password" class="form-control" @bind-Value="newUser.Password" placeholder="Min 6 caratteri" />
|
|
<ValidationMessage For="@(() => newUser.Password)" />
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label for="role" class="form-label">Ruolo</label>
|
|
<InputSelect id="role" class="form-control" @bind-Value="newUser.Role">
|
|
<option value="User">Utente</option>
|
|
<option value="Admin">Amministratore</option>
|
|
</InputSelect>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary" disabled="@isSubmitting">
|
|
@if (isSubmitting)
|
|
{
|
|
<span class="spinner-border spinner-border-sm me-2" role="status" aria-hidden="true"></span>
|
|
<span>Creazione in corso...</span>
|
|
}
|
|
else
|
|
{
|
|
<span>Crea Utente</span>
|
|
}
|
|
</button>
|
|
</EditForm>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
private string activeTab = "list";
|
|
private List<ApplicationUser>? users;
|
|
private CreateUserDto newUser = new();
|
|
private string successMessage = string.Empty;
|
|
private string errorMessage = string.Empty;
|
|
private bool isSubmitting = false;
|
|
private Dictionary<string, string> userRoles = new();
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await LoadUsers();
|
|
}
|
|
|
|
private async Task LoadUsers()
|
|
{
|
|
try
|
|
{
|
|
users = await UserService.GetAllUsersAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError(ex, "Errore nel caricamento degli utenti");
|
|
errorMessage = "Errore nel caricamento degli utenti";
|
|
}
|
|
}
|
|
|
|
private async Task HandleCreateUser()
|
|
{
|
|
isSubmitting = true;
|
|
try
|
|
{
|
|
var (success, message) = await UserService.CreateUserAsync(newUser);
|
|
if (success)
|
|
{
|
|
successMessage = message;
|
|
newUser = new();
|
|
await LoadUsers();
|
|
activeTab = "list";
|
|
}
|
|
else
|
|
{
|
|
errorMessage = message;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError(ex, "Errore nella creazione dell'utente");
|
|
errorMessage = "Errore nella creazione dell'utente";
|
|
}
|
|
finally
|
|
{
|
|
isSubmitting = false;
|
|
}
|
|
}
|
|
|
|
private async Task DeleteUser(string userId)
|
|
{
|
|
if (await JsConfirm("Sei sicuro di voler eliminare questo utente?"))
|
|
{
|
|
var (success, message) = await UserService.DeleteUserAsync(userId);
|
|
if (success)
|
|
{
|
|
successMessage = message;
|
|
await LoadUsers();
|
|
}
|
|
else
|
|
{
|
|
errorMessage = message;
|
|
}
|
|
}
|
|
}
|
|
|
|
private async Task ToggleUserStatus(string userId)
|
|
{
|
|
var (success, message) = await UserService.ToggleUserStatusAsync(userId);
|
|
if (success)
|
|
{
|
|
successMessage = message;
|
|
await LoadUsers();
|
|
}
|
|
else
|
|
{
|
|
errorMessage = message;
|
|
}
|
|
}
|
|
|
|
private string GetUserRole(string userId)
|
|
{
|
|
// Per ora returniamo "User" - in futuro implementeremo la logica per recuperare i ruoli
|
|
return "User";
|
|
}
|
|
|
|
private async Task<bool> JsConfirm(string message)
|
|
{
|
|
// Placeholder - in un componente reale userebbero JS interop
|
|
return true;
|
|
}
|
|
}
|