Compare commits
2 Commits
1a8a3ee563
...
fcaf17eacd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fcaf17eacd | ||
|
|
59c47f2b03 |
230
Web-Jarmark/Web-Jarmark/Components/Pages/Admin/AdminUsers.razor
Normal file
230
Web-Jarmark/Web-Jarmark/Components/Pages/Admin/AdminUsers.razor
Normal 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);
|
||||||
|
}
|
||||||
@@ -28,10 +28,11 @@ builder.Services.AddDbContext<ApplicationDbContext>(options =>
|
|||||||
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
|
||||||
|
|
||||||
builder.Services.AddIdentityCore<ApplicationUser>(options =>
|
builder.Services.AddIdentityCore<ApplicationUser>(options =>
|
||||||
{
|
{
|
||||||
options.SignIn.RequireConfirmedAccount = true;
|
options.SignIn.RequireConfirmedAccount = true;
|
||||||
options.Stores.SchemaVersion = IdentitySchemaVersions.Version3;
|
options.Stores.SchemaVersion = IdentitySchemaVersions.Version3;
|
||||||
})
|
})
|
||||||
|
.AddRoles<IdentityRole>()
|
||||||
.AddEntityFrameworkStores<ApplicationDbContext>()
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
||||||
.AddSignInManager()
|
.AddSignInManager()
|
||||||
.AddDefaultTokenProviders();
|
.AddDefaultTokenProviders();
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user