revamp
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
@page
|
||||
@model maintenance.Pages.Admin.Groups.IndexModel
|
||||
@{
|
||||
ViewData["Title"] = "Admin Groups";
|
||||
}
|
||||
|
||||
<h1>Admin Groups</h1>
|
||||
<p>Manage group claim values that grant admin access.</p>
|
||||
|
||||
<form method="post" asp-page-handler="Add" class="mb-4">
|
||||
<div class="row g-2">
|
||||
<div class="col-md-8">
|
||||
<input asp-for="NewGroupClaimValue" class="form-control" placeholder="Group claim value" />
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<button type="submit" class="btn btn-primary">Add Group</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Group Claim Value</th>
|
||||
<th>Role</th>
|
||||
<th>Active</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var g in Model.Groups)
|
||||
{
|
||||
<tr>
|
||||
<td>@g.GroupClaimValue</td>
|
||||
<td>@g.Role</td>
|
||||
<td>@g.Active</td>
|
||||
<td>
|
||||
<form method="post" asp-page-handler="Toggle" style="display:inline">
|
||||
<input type="hidden" name="id" value="@g.Id" />
|
||||
<button type="submit" class="btn btn-sm btn-outline-secondary">Toggle</button>
|
||||
</form>
|
||||
<form method="post" asp-page-handler="Delete" style="display:inline">
|
||||
<input type="hidden" name="id" value="@g.Id" />
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger">Remove</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -0,0 +1,86 @@
|
||||
using maintenance.Data;
|
||||
using maintenance.Data.Entities;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace maintenance.Pages.Admin.Groups;
|
||||
|
||||
[Authorize(Policy = "AdminOnly")]
|
||||
public sealed class IndexModel : PageModel
|
||||
{
|
||||
private readonly AppDbContext _dbContext;
|
||||
|
||||
public IndexModel(AppDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public string NewGroupClaimValue { get; set; } = string.Empty;
|
||||
|
||||
public IReadOnlyList<AuthorizationGroupEntity> Groups { get; private set; } = [];
|
||||
|
||||
public async Task OnGetAsync()
|
||||
{
|
||||
await LoadAsync();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAddAsync()
|
||||
{
|
||||
var claim = NewGroupClaimValue.Trim();
|
||||
if (string.IsNullOrWhiteSpace(claim))
|
||||
{
|
||||
await LoadAsync();
|
||||
return Page();
|
||||
}
|
||||
|
||||
var exists = await _dbContext.AuthorizationGroups
|
||||
.AnyAsync(x => x.GroupClaimValue == claim, HttpContext.RequestAborted);
|
||||
if (!exists)
|
||||
{
|
||||
_dbContext.AuthorizationGroups.Add(new AuthorizationGroupEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
GroupClaimValue = claim,
|
||||
Role = "admin",
|
||||
Active = true
|
||||
});
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostToggleAsync(Guid id)
|
||||
{
|
||||
var entity = await _dbContext.AuthorizationGroups.FirstOrDefaultAsync(x => x.Id == id, HttpContext.RequestAborted);
|
||||
if (entity is not null)
|
||||
{
|
||||
entity.Active = !entity.Active;
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteAsync(Guid id)
|
||||
{
|
||||
var entity = await _dbContext.AuthorizationGroups.FirstOrDefaultAsync(x => x.Id == id, HttpContext.RequestAborted);
|
||||
if (entity is not null)
|
||||
{
|
||||
_dbContext.AuthorizationGroups.Remove(entity);
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
private async Task LoadAsync()
|
||||
{
|
||||
Groups = await _dbContext.AuthorizationGroups
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(x => x.Active)
|
||||
.ThenBy(x => x.GroupClaimValue)
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user