Compare commits

...

2 Commits

Author SHA1 Message Date
Ondur
fcaf17eacd admin 2026-11-04 18:18:30 +01:00
Ondur
59c47f2b03 admin 2026-11-04 18:18:12 +01:00
5 changed files with 236 additions and 5 deletions

View File

@@ -0,0 +1,230 @@
@page "/admin/administratori"
@layout Web_Jarmark.Components.Layout.Admin.AdminLayout
@rendermode InteractiveServer
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Identity
@using Microsoft.EntityFrameworkCore
@using Web_Jarmark.Data
@inject UserManager<ApplicationUser> UserManager
@inject RoleManager<IdentityRole> RoleManager
@attribute [Authorize]
<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ů.</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></th>
</tr>
</thead>
<tbody>
@foreach (var u in users)
{
<tr>
<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))
{
await RoleManager.CreateAsync(new IdentityRole(AdminRole));
}
}
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)
{
busy = true;
message = null;
try
{
var user = await UserManager.FindByIdAsync(row.Id);
if (user is null)
{
message = "Uživatel nebyl nalezen.";
return;
}
if (row.IsAdmin)
{
await UserManager.RemoveFromRoleAsync(user, AdminRole);
message = $"Role Admin odebrána: {row.Email}";
}
else
{
await UserManager.AddToRoleAsync(user, AdminRole);
message = $"Role Admin přidělena: {row.Email}";
}
await LoadUsersAsync();
}
catch (Exception ex)
{
message = "Chyba: " + ex.Message;
}
finally
{
busy = false;
}
}
private async Task ToggleLockAsync(UserRow row)
{
busy = true;
message = null;
try
{
var user = await UserManager.FindByIdAsync(row.Id);
if (user is null)
{
message = "Uživatel nebyl nalezen.";
return;
}
if (row.IsLocked)
{
await UserManager.SetLockoutEndDateAsync(user, null);
message = $"Účet odemčen: {row.Email}";
}
else
{
await UserManager.SetLockoutEnabledAsync(user, true);
await UserManager.SetLockoutEndDateAsync(user, DateTimeOffset.UtcNow.AddYears(100));
message = $"Účet 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);
}

View File

@@ -28,10 +28,11 @@ builder.Services.AddDbContext<ApplicationDbContext>(options =>
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
builder.Services.AddIdentityCore<ApplicationUser>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.Stores.SchemaVersion = IdentitySchemaVersions.Version3;
})
{
options.SignIn.RequireConfirmedAccount = true;
options.Stores.SchemaVersion = IdentitySchemaVersions.Version3;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager()
.AddDefaultTokenProviders();

View File

@@ -3060,4 +3060,4 @@ textarea.wj-input {
.ap-status.is-off {
background: #f0f0f2;
color: #6e6e73;
}
}