Changes
This commit is contained in:
@@ -5,12 +5,28 @@
|
||||
}
|
||||
|
||||
<h1>Admin Groups</h1>
|
||||
<p>Manage group claim values that grant admin access.</p>
|
||||
<p>
|
||||
@if (Model.IsAdSource)
|
||||
{
|
||||
<text>Source: AD lookup + AuthorizationGroup records.</text>
|
||||
}
|
||||
else
|
||||
{
|
||||
<text>Source: MAX app database records.</text>
|
||||
}
|
||||
</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" />
|
||||
@if (Model.IsAdSource)
|
||||
{
|
||||
<input asp-for="GroupSearch" class="form-control" placeholder="Search AD lookup groups by name" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<input asp-for="GroupSearch" class="form-control" placeholder="Search added groups (case-insensitive)" />
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<button type="submit" class="btn btn-outline-primary">Search</button>
|
||||
@@ -18,7 +34,7 @@
|
||||
</div>
|
||||
</form>
|
||||
|
||||
@if (Model.LookupResults.Count > 0)
|
||||
@if (Model.IsAdSource && Model.LookupResults.Count > 0)
|
||||
{
|
||||
<div class="mb-4">
|
||||
<h5>Lookup Results</h5>
|
||||
@@ -63,29 +79,53 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Group Claim Value</th>
|
||||
<th>Role</th>
|
||||
<th>Active</th>
|
||||
@if (Model.IsAdSource)
|
||||
{
|
||||
<th>Role</th>
|
||||
<th>Active</th>
|
||||
}
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var g in Model.Groups)
|
||||
@if (Model.IsAdSource)
|
||||
{
|
||||
<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>
|
||||
@foreach (var g in Model.AdGroups)
|
||||
{
|
||||
<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" />
|
||||
<input type="hidden" asp-for="GroupSearch" />
|
||||
<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" />
|
||||
<input type="hidden" asp-for="GroupSearch" />
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger">Remove</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@foreach (var g in Model.Groups)
|
||||
{
|
||||
<tr>
|
||||
<td>@g.GroupClaimValue</td>
|
||||
<td>
|
||||
<form method="post" asp-page-handler="Delete" style="display:inline">
|
||||
<input type="hidden" name="id" value="@g.Id" />
|
||||
<input type="hidden" asp-for="GroupSearch" />
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger">Remove</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using maintenance.Data;
|
||||
using maintenance.Data.Entities;
|
||||
using maintenance.Authentication.Configuration;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
@@ -10,13 +11,16 @@ namespace maintenance.Pages.Admin.Groups;
|
||||
[Authorize(Policy = "AdminOnly")]
|
||||
public sealed class IndexModel : PageModel
|
||||
{
|
||||
private const string AdminRole = "admin";
|
||||
private readonly AppDbContext _dbContext;
|
||||
private readonly GroupLookupDbContext _groupLookupDbContext;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public IndexModel(AppDbContext dbContext, GroupLookupDbContext groupLookupDbContext)
|
||||
public IndexModel(AppDbContext dbContext, IConfiguration configuration, IServiceProvider serviceProvider)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_groupLookupDbContext = groupLookupDbContext;
|
||||
_configuration = configuration;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
[BindProperty]
|
||||
@@ -25,44 +29,78 @@ public sealed class IndexModel : PageModel
|
||||
[BindProperty(SupportsGet = true)]
|
||||
public string GroupSearch { get; set; } = string.Empty;
|
||||
|
||||
public IReadOnlyList<AuthorizationGroupEntity> Groups { get; private set; } = [];
|
||||
public bool IsAdSource { get; private set; }
|
||||
public IReadOnlyList<AdminGroupEntity> Groups { get; private set; } = [];
|
||||
public IReadOnlyList<AuthorizationGroupEntity> AdGroups { get; private set; } = [];
|
||||
public IReadOnlyList<string> LookupResults { get; private set; } = [];
|
||||
|
||||
public async Task OnGetAsync()
|
||||
{
|
||||
IsAdSource = ResolveIsAdSource();
|
||||
await LoadAsync();
|
||||
await SearchAsync();
|
||||
if (IsAdSource)
|
||||
{
|
||||
await SearchAdLookupAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAddAsync()
|
||||
{
|
||||
IsAdSource = ResolveIsAdSource();
|
||||
var claim = NewGroupClaimValue.Trim();
|
||||
if (string.IsNullOrWhiteSpace(claim))
|
||||
{
|
||||
await LoadAsync();
|
||||
await SearchAsync();
|
||||
if (IsAdSource)
|
||||
{
|
||||
await SearchAdLookupAsync();
|
||||
}
|
||||
return Page();
|
||||
}
|
||||
|
||||
var exists = await _dbContext.AuthorizationGroups
|
||||
.AnyAsync(x => x.GroupClaimValue == claim, HttpContext.RequestAborted);
|
||||
if (!exists)
|
||||
if (IsAdSource)
|
||||
{
|
||||
_dbContext.AuthorizationGroups.Add(new AuthorizationGroupEntity
|
||||
var existsAd = await _dbContext.AuthorizationGroups
|
||||
.AnyAsync(x => x.GroupClaimValue == claim, HttpContext.RequestAborted);
|
||||
if (!existsAd)
|
||||
{
|
||||
_dbContext.AuthorizationGroups.Add(new AuthorizationGroupEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
GroupClaimValue = claim,
|
||||
Role = AdminRole,
|
||||
Active = true
|
||||
});
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
}
|
||||
|
||||
var normalizedClaim = NormalizeGroup(claim);
|
||||
var existsMax = await _dbContext.AdminGroups
|
||||
.AnyAsync(x => x.NormalizedGroupClaimValue == normalizedClaim, HttpContext.RequestAborted);
|
||||
if (!existsMax)
|
||||
{
|
||||
_dbContext.AdminGroups.Add(new AdminGroupEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
GroupClaimValue = claim,
|
||||
Role = "admin",
|
||||
Active = true
|
||||
NormalizedGroupClaimValue = normalizedClaim
|
||||
});
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
return RedirectToPage();
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostAddFromLookupAsync(string groupName)
|
||||
{
|
||||
if (!ResolveIsAdSource())
|
||||
{
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(groupName))
|
||||
{
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
@@ -77,7 +115,7 @@ public sealed class IndexModel : PageModel
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
GroupClaimValue = claim,
|
||||
Role = "admin",
|
||||
Role = AdminRole,
|
||||
Active = true
|
||||
});
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
@@ -88,36 +126,74 @@ public sealed class IndexModel : PageModel
|
||||
|
||||
public async Task<IActionResult> OnPostToggleAsync(Guid id)
|
||||
{
|
||||
if (!ResolveIsAdSource())
|
||||
{
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostDeleteAsync(Guid id)
|
||||
{
|
||||
var entity = await _dbContext.AuthorizationGroups.FirstOrDefaultAsync(x => x.Id == id, HttpContext.RequestAborted);
|
||||
if (ResolveIsAdSource())
|
||||
{
|
||||
var adEntity = await _dbContext.AuthorizationGroups.FirstOrDefaultAsync(x => x.Id == id, HttpContext.RequestAborted);
|
||||
if (adEntity is not null)
|
||||
{
|
||||
_dbContext.AuthorizationGroups.Remove(adEntity);
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
}
|
||||
|
||||
var entity = await _dbContext.AdminGroups.FirstOrDefaultAsync(x => x.Id == id, HttpContext.RequestAborted);
|
||||
if (entity is not null)
|
||||
{
|
||||
_dbContext.AuthorizationGroups.Remove(entity);
|
||||
_dbContext.AdminGroups.Remove(entity);
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
return RedirectToPage();
|
||||
return RedirectToPage(new { GroupSearch });
|
||||
}
|
||||
|
||||
private async Task LoadAsync()
|
||||
{
|
||||
Groups = await _dbContext.AuthorizationGroups
|
||||
if (IsAdSource)
|
||||
{
|
||||
AdGroups = await _dbContext.AuthorizationGroups
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(x => x.Active)
|
||||
.ThenBy(x => x.GroupClaimValue)
|
||||
.Take(200)
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
return;
|
||||
}
|
||||
|
||||
var query = _dbContext.AdminGroups
|
||||
.AsNoTracking()
|
||||
.OrderByDescending(x => x.Active)
|
||||
.ThenBy(x => x.GroupClaimValue)
|
||||
.AsQueryable();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(GroupSearch))
|
||||
{
|
||||
var term = NormalizeGroup(GroupSearch);
|
||||
query = query.Where(g => g.NormalizedGroupClaimValue.Contains(term));
|
||||
}
|
||||
|
||||
Groups = await query
|
||||
.OrderBy(g => g.GroupClaimValue)
|
||||
.Take(200)
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
private async Task SearchAsync()
|
||||
private async Task SearchAdLookupAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(GroupSearch))
|
||||
{
|
||||
@@ -125,8 +201,15 @@ public sealed class IndexModel : PageModel
|
||||
return;
|
||||
}
|
||||
|
||||
var groupLookupDbContext = _serviceProvider.GetService<GroupLookupDbContext>();
|
||||
if (groupLookupDbContext is null)
|
||||
{
|
||||
LookupResults = [];
|
||||
return;
|
||||
}
|
||||
|
||||
var term = GroupSearch.Trim();
|
||||
LookupResults = await _groupLookupDbContext.Groups
|
||||
LookupResults = await groupLookupDbContext.Groups
|
||||
.AsNoTracking()
|
||||
.Where(g => g.Name.Contains(term))
|
||||
.OrderBy(g => g.Name)
|
||||
@@ -135,4 +218,15 @@ public sealed class IndexModel : PageModel
|
||||
.Take(25)
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
private bool ResolveIsAdSource()
|
||||
{
|
||||
var source = _configuration[AuthEnvironment.AdminGroupSource] ?? AdminGroupSources.Max;
|
||||
return source.Equals(AdminGroupSources.Ad, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private static string NormalizeGroup(string value)
|
||||
{
|
||||
return value.Trim().ToUpperInvariant();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user