admin
This commit is contained in:
@@ -1,14 +1,19 @@
|
|||||||
@page "/admin/administratori"
|
@page "/admin/administratori"
|
||||||
@layout Web_Jarmark.Components.Layout.Admin.AdminLayout
|
@layout Web_Jarmark.Components.Layout.Admin.AdminLayout
|
||||||
@rendermode InteractiveServer
|
@rendermode InteractiveServer
|
||||||
|
|
||||||
@using Microsoft.AspNetCore.Authorization
|
@using Microsoft.AspNetCore.Authorization
|
||||||
@using Microsoft.AspNetCore.Identity
|
@using Microsoft.AspNetCore.Identity
|
||||||
|
@using Microsoft.AspNetCore.Components.Authorization
|
||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using Web_Jarmark.Data
|
@using Web_Jarmark.Data
|
||||||
|
|
||||||
@inject UserManager<ApplicationUser> UserManager
|
@inject UserManager<ApplicationUser> UserManager
|
||||||
@inject RoleManager<IdentityRole> RoleManager
|
@inject RoleManager<IdentityRole> RoleManager
|
||||||
|
@inject AuthenticationStateProvider AuthenticationStateProvider
|
||||||
|
|
||||||
@attribute [Authorize(Roles = "Admin")]
|
@attribute [Authorize(Roles = "Admin")]
|
||||||
|
|
||||||
<PageTitle>Registrovaní uživatelé</PageTitle>
|
<PageTitle>Registrovaní uživatelé</PageTitle>
|
||||||
|
|
||||||
<div class="ap">
|
<div class="ap">
|
||||||
@@ -17,7 +22,7 @@
|
|||||||
<div class="ap-hero-row">
|
<div class="ap-hero-row">
|
||||||
<div>
|
<div>
|
||||||
<h1>Uživatelé</h1>
|
<h1>Uživatelé</h1>
|
||||||
<p class="ap-sub">Správa registrovaných účtů.</p>
|
<p class="ap-sub">Správa registrovaných účtů a administrátorských oprávnění.</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
@@ -53,13 +58,13 @@
|
|||||||
<th>Potvrzený</th>
|
<th>Potvrzený</th>
|
||||||
<th>Admin</th>
|
<th>Admin</th>
|
||||||
<th>Stav</th>
|
<th>Stav</th>
|
||||||
<th></th>
|
<th>Akce</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@foreach (var u in users)
|
@foreach (var u in users)
|
||||||
{
|
{
|
||||||
<tr>
|
<tr @key="u.Id">
|
||||||
<td class="ap-name">@u.Email</td>
|
<td class="ap-name">@u.Email</td>
|
||||||
<td>
|
<td>
|
||||||
<span class="ap-status @(u.EmailConfirmed ? "is-on" : "is-off")">
|
<span class="ap-status @(u.EmailConfirmed ? "is-on" : "is-off")">
|
||||||
@@ -83,6 +88,7 @@
|
|||||||
@onclick="() => ToggleAdminAsync(u)">
|
@onclick="() => ToggleAdminAsync(u)">
|
||||||
@(u.IsAdmin ? "Odebrat admin" : "Udělat admin")
|
@(u.IsAdmin ? "Odebrat admin" : "Udělat admin")
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<button type="button"
|
<button type="button"
|
||||||
class="ap-btn ap-btn-ghost"
|
class="ap-btn ap-btn-ghost"
|
||||||
disabled="@busy"
|
disabled="@busy"
|
||||||
@@ -118,7 +124,14 @@
|
|||||||
{
|
{
|
||||||
if (!await RoleManager.RoleExistsAsync(AdminRole))
|
if (!await RoleManager.RoleExistsAsync(AdminRole))
|
||||||
{
|
{
|
||||||
await RoleManager.CreateAsync(new IdentityRole(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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +147,9 @@
|
|||||||
foreach (var user in list)
|
foreach (var user in list)
|
||||||
{
|
{
|
||||||
var roles = await UserManager.GetRolesAsync(user);
|
var roles = await UserManager.GetRolesAsync(user);
|
||||||
var isLocked = user.LockoutEnd.HasValue && user.LockoutEnd > DateTimeOffset.UtcNow;
|
|
||||||
|
var isLocked = user.LockoutEnd.HasValue
|
||||||
|
&& user.LockoutEnd > DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
users.Add(new UserRow(
|
users.Add(new UserRow(
|
||||||
user.Id,
|
user.Id,
|
||||||
@@ -147,34 +162,82 @@
|
|||||||
|
|
||||||
private async Task ToggleAdminAsync(UserRow row)
|
private async Task ToggleAdminAsync(UserRow row)
|
||||||
{
|
{
|
||||||
|
if (busy)
|
||||||
|
return;
|
||||||
|
|
||||||
busy = true;
|
busy = true;
|
||||||
message = null;
|
message = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var user = await UserManager.FindByIdAsync(row.Id);
|
var user = await UserManager.FindByIdAsync(row.Id);
|
||||||
|
|
||||||
if (user is null)
|
if (user is null)
|
||||||
{
|
{
|
||||||
message = "Uživatel nebyl nalezen.";
|
message = "Uživatel nebyl nalezen.";
|
||||||
return;
|
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)
|
if (row.IsAdmin)
|
||||||
{
|
{
|
||||||
await UserManager.RemoveFromRoleAsync(user, AdminRole);
|
var admins = await UserManager.GetUsersInRoleAsync(AdminRole);
|
||||||
message = $"Role Admin odebrána: {row.Email}";
|
|
||||||
|
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
|
else
|
||||||
{
|
{
|
||||||
await UserManager.AddToRoleAsync(user, AdminRole);
|
result = await UserManager.AddToRoleAsync(user, AdminRole);
|
||||||
message = $"Role Admin přidělena: {row.Email}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
await LoadUsersAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
message = "Chyba: " + ex.Message;
|
message = "Chyba při změně role: " + ex.Message;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -184,29 +247,58 @@
|
|||||||
|
|
||||||
private async Task ToggleLockAsync(UserRow row)
|
private async Task ToggleLockAsync(UserRow row)
|
||||||
{
|
{
|
||||||
|
if (busy)
|
||||||
|
return;
|
||||||
|
|
||||||
busy = true;
|
busy = true;
|
||||||
message = null;
|
message = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var user = await UserManager.FindByIdAsync(row.Id);
|
var user = await UserManager.FindByIdAsync(row.Id);
|
||||||
|
|
||||||
if (user is null)
|
if (user is null)
|
||||||
{
|
{
|
||||||
message = "Uživatel nebyl nalezen.";
|
message = "Uživatel nebyl nalezen.";
|
||||||
return;
|
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)
|
if (row.IsLocked)
|
||||||
{
|
{
|
||||||
await UserManager.SetLockoutEndDateAsync(user, null);
|
result = await UserManager.SetLockoutEndDateAsync(user, null);
|
||||||
message = $"Účet odemčen: {row.Email}";
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await UserManager.SetLockoutEnabledAsync(user, true);
|
result = await UserManager.SetLockoutEnabledAsync(user, true);
|
||||||
await UserManager.SetLockoutEndDateAsync(user, DateTimeOffset.UtcNow.AddYears(100));
|
|
||||||
message = $"Účet zamčen: {row.Email}";
|
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();
|
await LoadUsersAsync();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,69 +46,22 @@ builder.Services.AddSingleton<IEmailSender<ApplicationUser>, IdentityNoOpEmailSe
|
|||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Vytvoření role Admin, pokud ještě neexistuje
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
{
|
{
|
||||||
var userManager = scope.ServiceProvider
|
|
||||||
.GetRequiredService<UserManager<ApplicationUser>>();
|
|
||||||
|
|
||||||
var roleManager = scope.ServiceProvider
|
var roleManager = scope.ServiceProvider
|
||||||
.GetRequiredService<RoleManager<IdentityRole>>();
|
.GetRequiredService<RoleManager<IdentityRole>>();
|
||||||
|
|
||||||
const string adminEmail = "markus@bumbus.cz";
|
|
||||||
|
|
||||||
// Vytvoření role Admin, pokud ještě neexistuje
|
|
||||||
if (!await roleManager.RoleExistsAsync("Admin"))
|
if (!await roleManager.RoleExistsAsync("Admin"))
|
||||||
{
|
{
|
||||||
await roleManager.CreateAsync(new IdentityRole("Admin"));
|
var result = await roleManager.CreateAsync(new IdentityRole("Admin"));
|
||||||
}
|
|
||||||
|
|
||||||
// Vyhledání administrátorského účtu
|
|
||||||
var adminUser = await userManager.FindByEmailAsync(adminEmail);
|
|
||||||
|
|
||||||
if (adminUser != null)
|
|
||||||
{
|
|
||||||
if (!await userManager.IsInRoleAsync(adminUser, "Admin"))
|
|
||||||
{
|
|
||||||
await userManager.AddToRoleAsync(adminUser, "Admin");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception(
|
|
||||||
$"Administrátorský účet {adminEmail} nebyl nalezen.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Vytvoření rolí a nastavení administrátora
|
|
||||||
using (var scope = app.Services.CreateScope())
|
|
||||||
{
|
|
||||||
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
|
||||||
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
|
||||||
|
|
||||||
if (!await roleManager.RoleExistsAsync("Admin"))
|
|
||||||
{
|
|
||||||
await roleManager.CreateAsync(new IdentityRole("Admin"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!await roleManager.RoleExistsAsync("User"))
|
|
||||||
{
|
|
||||||
await roleManager.CreateAsync(new IdentityRole("User"));
|
|
||||||
}
|
|
||||||
|
|
||||||
var adminEmail = app.Configuration["InitialAdmin:Email"];
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(adminEmail))
|
|
||||||
{
|
|
||||||
var admin = await userManager.FindByEmailAsync(adminEmail);
|
|
||||||
|
|
||||||
if (admin != null && !await userManager.IsInRoleAsync(admin, "Admin"))
|
|
||||||
{
|
|
||||||
var result = await userManager.AddToRoleAsync(admin, "Admin");
|
|
||||||
|
|
||||||
if (!result.Succeeded)
|
if (!result.Succeeded)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException(
|
throw new InvalidOperationException(
|
||||||
string.Join(", ", result.Errors.Select(e => e.Description)));
|
"Nepodařilo se vytvořit roli Admin: "
|
||||||
}
|
+ string.Join(", ", result.Errors.Select(e => e.Description)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
Web-Jarmark/Web-Jarmark/jarmark.db-shm
Normal file
BIN
Web-Jarmark/Web-Jarmark/jarmark.db-shm
Normal file
Binary file not shown.
BIN
Web-Jarmark/Web-Jarmark/jarmark.db-wal
Normal file
BIN
Web-Jarmark/Web-Jarmark/jarmark.db-wal
Normal file
Binary file not shown.
Reference in New Issue
Block a user