Files
Web-Jarmark/Web-Jarmark/Web-Jarmark/Components/Pages/Admin/AdminUsers.razor
2026-11-11 16:53:32 +01:00

322 lines
9.8 KiB
Plaintext

@page "/admin/administratori"
@layout Web_Jarmark.Components.Layout.Admin.AdminLayout
@rendermode InteractiveServer
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.EntityFrameworkCore
@using Web_Jarmark.Data
@inject UserManager<ApplicationUser> UserManager
@inject RoleManager<IdentityRole> RoleManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@attribute [Authorize(Roles = "Admin")]
<PageTitle>Registrovaní uživatelé</PageTitle>
<div class="ap">
<header class="ap-hero ap-hero-left">
<p class="ap-eyebrow">Administrace</p>
<div class="ap-hero-row">
<div>
<h1>Uživatelé</h1>
<p class="ap-sub">Správa registrovaných účtů a administrátorských oprávnění.</p>
</div>
</div>
</header>
@if (!string.IsNullOrEmpty(message))
{
<div class="ap-alert">@message</div>
}
@if (loading)
{
<div class="ap-loading">Načítám uživatele…</div>
}
else if (users.Count == 0)
{
<section class="ap-panel">
<p class="ap-empty">Zatím žádní registrovaní uživatelé.</p>
</section>
}
else
{
<section class="ap-panel ap-panel-table">
<div class="ap-panel-head">
<h2>Registrované účty</h2>
<span class="ap-muted">@users.Count účtů</span>
</div>
<div class="ap-table-wrap">
<table class="ap-table">
<thead>
<tr>
<th>E-mail</th>
<th>Potvrzený</th>
<th>Admin</th>
<th>Stav</th>
<th>Akce</th>
</tr>
</thead>
<tbody>
@foreach (var u in users)
{
<tr @key="u.Id">
<td class="ap-name">@u.Email</td>
<td>
<span class="ap-status @(u.EmailConfirmed ? "is-on" : "is-off")">
@(u.EmailConfirmed ? "Ano" : "Ne")
</span>
</td>
<td>
<span class="ap-status @(u.IsAdmin ? "is-on" : "is-off")">
@(u.IsAdmin ? "Admin" : "Uživatel")
</span>
</td>
<td>
<span class="ap-status @(u.IsLocked ? "is-off" : "is-on")">
@(u.IsLocked ? "Zamčený" : "Aktivní")
</span>
</td>
<td class="ap-actions-cell">
<button type="button"
class="ap-btn ap-btn-ghost"
disabled="@busy"
@onclick="() => ToggleAdminAsync(u)">
@(u.IsAdmin ? "Odebrat admin" : "Udělat admin")
</button>
<button type="button"
class="ap-btn ap-btn-ghost"
disabled="@busy"
@onclick="() => ToggleLockAsync(u)">
@(u.IsLocked ? "Odemknout" : "Zamknout")
</button>
</td>
</tr>
}
</tbody>
</table>
</div>
</section>
}
</div>
@code {
private List<UserRow> users = new();
private bool loading = true;
private bool busy;
private string? message;
private const string AdminRole = "Admin";
protected override async Task OnInitializedAsync()
{
await EnsureAdminRoleAsync();
await LoadUsersAsync();
loading = false;
}
private async Task EnsureAdminRoleAsync()
{
if (!await RoleManager.RoleExistsAsync(AdminRole))
{
var result = await RoleManager.CreateAsync(
new IdentityRole(AdminRole));
if (!result.Succeeded)
{
message = "Nepodařilo se vytvořit roli Admin: "
+ string.Join(", ", result.Errors.Select(e => e.Description));
}
}
}
private async Task LoadUsersAsync()
{
var list = await UserManager.Users
.AsNoTracking()
.OrderBy(u => u.Email)
.ToListAsync();
users = new List<UserRow>();
foreach (var user in list)
{
var roles = await UserManager.GetRolesAsync(user);
var isLocked = user.LockoutEnd.HasValue
&& user.LockoutEnd > DateTimeOffset.UtcNow;
users.Add(new UserRow(
user.Id,
user.Email ?? "(bez e-mailu)",
user.EmailConfirmed,
roles.Contains(AdminRole),
isLocked));
}
}
private async Task ToggleAdminAsync(UserRow row)
{
if (busy)
return;
busy = true;
message = null;
try
{
var user = await UserManager.FindByIdAsync(row.Id);
if (user is null)
{
message = "Uživatel nebyl nalezen.";
return;
}
if (!await RoleManager.RoleExistsAsync(AdminRole))
{
var createRoleResult = await RoleManager.CreateAsync(
new IdentityRole(AdminRole));
if (!createRoleResult.Succeeded)
{
message = "Nepodařilo se vytvořit roli Admin: "
+ string.Join(", ", createRoleResult.Errors.Select(e => e.Description));
return;
}
}
if (row.IsAdmin)
{
var admins = await UserManager.GetUsersInRoleAsync(AdminRole);
if (admins.Count <= 1)
{
message = "Nelze odebrat roli poslednímu administrátorovi.";
return;
}
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var currentUserId = UserManager.GetUserId(authState.User);
if (currentUserId == user.Id)
{
message = "Nemůžeš odebrat administrátorskou roli sám sobě.";
return;
}
}
IdentityResult result;
if (row.IsAdmin)
{
result = await UserManager.RemoveFromRoleAsync(user, AdminRole);
}
else
{
result = await UserManager.AddToRoleAsync(user, AdminRole);
}
if (!result.Succeeded)
{
message = "Změna role se nepodařila: "
+ string.Join(", ", result.Errors.Select(e => e.Description));
return;
}
message = row.IsAdmin
? $"Administrátorská role byla odebrána: {row.Email}"
: $"Administrátorská role byla přidána: {row.Email}";
await LoadUsersAsync();
}
catch (Exception ex)
{
message = "Chyba při změně role: " + ex.Message;
}
finally
{
busy = false;
}
}
private async Task ToggleLockAsync(UserRow row)
{
if (busy)
return;
busy = true;
message = null;
try
{
var user = await UserManager.FindByIdAsync(row.Id);
if (user is null)
{
message = "Uživatel nebyl nalezen.";
return;
}
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var currentUserId = UserManager.GetUserId(authState.User);
if (currentUserId == user.Id)
{
message = "Nemůžeš zamknout vlastní účet.";
return;
}
IdentityResult result;
if (row.IsLocked)
{
result = await UserManager.SetLockoutEndDateAsync(user, null);
}
else
{
result = await UserManager.SetLockoutEnabledAsync(user, true);
if (result.Succeeded)
{
result = await UserManager.SetLockoutEndDateAsync(
user, DateTimeOffset.UtcNow.AddYears(100));
}
}
if (!result.Succeeded)
{
message = "Změna stavu účtu se nepodařila: "
+ string.Join(", ", result.Errors.Select(e => e.Description));
return;
}
message = row.IsLocked
? $"Účet byl odemčen: {row.Email}"
: $"Účet byl zamčen: {row.Email}";
await LoadUsersAsync();
}
catch (Exception ex)
{
message = "Chyba: " + ex.Message;
}
finally
{
busy = false;
}
}
private sealed record UserRow(
string Id,
string Email,
bool EmailConfirmed,
bool IsAdmin,
bool IsLocked);
}