using maintenance.Data; using maintenance.Data.Entities; using maintenance.Authentication.Configuration; 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 const string AdminRole = "admin"; private readonly AppDbContext _dbContext; private readonly IConfiguration _configuration; private readonly IServiceProvider _serviceProvider; public IndexModel(AppDbContext dbContext, IConfiguration configuration, IServiceProvider serviceProvider) { _dbContext = dbContext; _configuration = configuration; _serviceProvider = serviceProvider; } [BindProperty] public string NewGroupClaimValue { get; set; } = string.Empty; [BindProperty(SupportsGet = true)] public string GroupSearch { get; set; } = string.Empty; public bool IsAdSource { get; private set; } public IReadOnlyList Groups { get; private set; } = []; public IReadOnlyList AdGroups { get; private set; } = []; public IReadOnlyList LookupResults { get; private set; } = []; public async Task OnGetAsync() { IsAdSource = ResolveIsAdSource(); await LoadAsync(); if (IsAdSource) { await SearchAdLookupAsync(); } } public async Task OnPostAddAsync() { IsAdSource = ResolveIsAdSource(); var claim = NewGroupClaimValue.Trim(); if (string.IsNullOrWhiteSpace(claim)) { await LoadAsync(); if (IsAdSource) { await SearchAdLookupAsync(); } return Page(); } if (IsAdSource) { 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, NormalizedGroupClaimValue = normalizedClaim }); await _dbContext.SaveChangesAsync(HttpContext.RequestAborted); } return RedirectToPage(new { GroupSearch }); } public async Task OnPostAddFromLookupAsync(string groupName) { if (!ResolveIsAdSource()) { return RedirectToPage(new { GroupSearch }); } 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 = AdminRole, Active = true }); await _dbContext.SaveChangesAsync(HttpContext.RequestAborted); } return RedirectToPage(new { GroupSearch }); } public async Task 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(new { GroupSearch }); } public async Task OnPostDeleteAsync(Guid id) { 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.AdminGroups.Remove(entity); await _dbContext.SaveChangesAsync(HttpContext.RequestAborted); } return RedirectToPage(new { GroupSearch }); } private async Task LoadAsync() { 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() .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 SearchAdLookupAsync() { if (string.IsNullOrWhiteSpace(GroupSearch)) { LookupResults = []; return; } var groupLookupDbContext = _serviceProvider.GetService(); if (groupLookupDbContext is null) { 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); } 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(); } }