revamp2
This commit is contained in:
@@ -7,6 +7,47 @@
|
||||
<h1>Admin Groups</h1>
|
||||
<p>Manage group claim values that grant admin access.</p>
|
||||
|
||||
<form method="get" class="mb-4">
|
||||
<div class="row g-2">
|
||||
<div class="col-md-8">
|
||||
<input asp-for="GroupSearch" class="form-control" placeholder="Search available groups by name" />
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<button type="submit" class="btn btn-outline-primary">Search</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@if (Model.LookupResults.Count > 0)
|
||||
{
|
||||
<div class="mb-4">
|
||||
<h5>Lookup Results</h5>
|
||||
<table class="table table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Group Name</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var result in Model.LookupResults)
|
||||
{
|
||||
<tr>
|
||||
<td>@result</td>
|
||||
<td>
|
||||
<form method="post" asp-page-handler="AddFromLookup" style="display:inline">
|
||||
<input type="hidden" name="groupName" value="@result" />
|
||||
<input type="hidden" asp-for="GroupSearch" />
|
||||
<button type="submit" class="btn btn-sm btn-success">Add as admin</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
|
||||
<form method="post" asp-page-handler="Add" class="mb-4">
|
||||
<div class="row g-2">
|
||||
<div class="col-md-8">
|
||||
|
||||
@@ -11,20 +11,27 @@ namespace maintenance.Pages.Admin.Groups;
|
||||
public sealed class IndexModel : PageModel
|
||||
{
|
||||
private readonly AppDbContext _dbContext;
|
||||
private readonly GroupLookupDbContext _groupLookupDbContext;
|
||||
|
||||
public IndexModel(AppDbContext dbContext)
|
||||
public IndexModel(AppDbContext dbContext, GroupLookupDbContext groupLookupDbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_groupLookupDbContext = groupLookupDbContext;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
public string NewGroupClaimValue { get; set; } = string.Empty;
|
||||
|
||||
[BindProperty(SupportsGet = true)]
|
||||
public string GroupSearch { get; set; } = string.Empty;
|
||||
|
||||
public IReadOnlyList<AuthorizationGroupEntity> Groups { get; private set; } = [];
|
||||
public IReadOnlyList<string> LookupResults { get; private set; } = [];
|
||||
|
||||
public async Task OnGetAsync()
|
||||
{
|
||||
await LoadAsync();
|
||||
await SearchAsync();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAddAsync()
|
||||
@@ -33,6 +40,7 @@ public sealed class IndexModel : PageModel
|
||||
if (string.IsNullOrWhiteSpace(claim))
|
||||
{
|
||||
await LoadAsync();
|
||||
await SearchAsync();
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -53,6 +61,31 @@ public sealed class IndexModel : PageModel
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAddFromLookupAsync(string groupName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(groupName))
|
||||
{
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
}
|
||||
|
||||
var claim = groupName.Trim();
|
||||
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(new { GroupSearch });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostToggleAsync(Guid id)
|
||||
{
|
||||
var entity = await _dbContext.AuthorizationGroups.FirstOrDefaultAsync(x => x.Id == id, HttpContext.RequestAborted);
|
||||
@@ -83,4 +116,23 @@ public sealed class IndexModel : PageModel
|
||||
.ThenBy(x => x.GroupClaimValue)
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
private async Task SearchAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(GroupSearch))
|
||||
{
|
||||
LookupResults = [];
|
||||
return;
|
||||
}
|
||||
|
||||
var term = GroupSearch.Trim();
|
||||
LookupResults = await _groupLookupDbContext.Groups
|
||||
.AsNoTracking()
|
||||
.Where(g => g.Name.Contains(term))
|
||||
.OrderBy(g => g.Name)
|
||||
.Select(g => g.Name)
|
||||
.Distinct()
|
||||
.Take(25)
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user