diff --git a/Data/Entities/GroupLookupEntity.cs b/Data/Entities/GroupLookupEntity.cs index fc39258..71db4f1 100644 --- a/Data/Entities/GroupLookupEntity.cs +++ b/Data/Entities/GroupLookupEntity.cs @@ -2,6 +2,7 @@ namespace maintenance.Data.Entities; public sealed class GroupLookupEntity { - public string GroupClaimValue { get; set; } = string.Empty; - public string DisplayName { get; set; } = string.Empty; + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + public int EmployeeId { get; set; } } diff --git a/Data/GroupLookupDbContext.cs b/Data/GroupLookupDbContext.cs index e90fcaf..0c391cf 100644 --- a/Data/GroupLookupDbContext.cs +++ b/Data/GroupLookupDbContext.cs @@ -15,10 +15,12 @@ public sealed class GroupLookupDbContext : DbContext { modelBuilder.Entity(e => { - e.ToTable("GroupLookup"); - e.HasKey(x => x.GroupClaimValue); - e.Property(x => x.GroupClaimValue).HasMaxLength(256).IsRequired(); - e.Property(x => x.DisplayName).HasMaxLength(256).IsRequired(); + e.ToTable("EmployeeGroup"); + e.HasKey(x => x.Id); + e.Property(x => x.Id).ValueGeneratedNever(); + e.Property(x => x.Name).HasMaxLength(256).IsRequired(); + e.Property(x => x.EmployeeId).IsRequired(); + e.HasIndex(x => x.Name); }); } diff --git a/Pages/Admin/Groups/Index.cshtml b/Pages/Admin/Groups/Index.cshtml index 53cee3c..64134a7 100644 --- a/Pages/Admin/Groups/Index.cshtml +++ b/Pages/Admin/Groups/Index.cshtml @@ -7,6 +7,47 @@

Admin Groups

Manage group claim values that grant admin access.

+
+
+
+ +
+
+ +
+
+
+ +@if (Model.LookupResults.Count > 0) +{ +
+
Lookup Results
+ + + + + + + + + @foreach (var result in Model.LookupResults) + { + + + + + } + +
Group Name
@result +
+ + + +
+
+
+} +
diff --git a/Pages/Admin/Groups/Index.cshtml.cs b/Pages/Admin/Groups/Index.cshtml.cs index 41bf1bd..44b9439 100644 --- a/Pages/Admin/Groups/Index.cshtml.cs +++ b/Pages/Admin/Groups/Index.cshtml.cs @@ -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 Groups { get; private set; } = []; + public IReadOnlyList LookupResults { get; private set; } = []; public async Task OnGetAsync() { await LoadAsync(); + await SearchAsync(); } public async Task 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 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 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); + } } diff --git a/Pages/Errors/Generic.cshtml b/Pages/Errors/Generic.cshtml index a2140e9..aec117f 100644 --- a/Pages/Errors/Generic.cshtml +++ b/Pages/Errors/Generic.cshtml @@ -15,7 +15,7 @@
MAX
-

@Model.Title

+

@Model.Title

@Model.Description

diff --git a/Pages/Errors/Template.cshtml b/Pages/Errors/Template.cshtml index db41574..99bf051 100644 --- a/Pages/Errors/Template.cshtml +++ b/Pages/Errors/Template.cshtml @@ -15,7 +15,7 @@
MAX
-

@Model.ErrorName

+

@Model.ErrorName

@if (!string.IsNullOrWhiteSpace(Model.ShortDescription)) { diff --git a/Pages/Errors/Unavailable.cshtml b/Pages/Errors/Unavailable.cshtml index bf54e19..095bc5d 100644 --- a/Pages/Errors/Unavailable.cshtml +++ b/Pages/Errors/Unavailable.cshtml @@ -15,7 +15,7 @@
MAX
-

@Model.Title

+

@Model.Title

@Model.DescriptionLine1

@Model.DescriptionLine2

diff --git a/Pages/Errors/_LockedTemplate.cshtml b/Pages/Errors/_LockedTemplate.cshtml index 5b62564..be67914 100644 --- a/Pages/Errors/_LockedTemplate.cshtml +++ b/Pages/Errors/_LockedTemplate.cshtml @@ -14,7 +14,7 @@
MAX
-

@Model.Content.ErrorName

+

@Model.Content.ErrorName

@if (!string.IsNullOrWhiteSpace(Model.Content.ShortDescription)) { diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml index d5673f7..ff38a15 100644 --- a/Pages/Index.cshtml +++ b/Pages/Index.cshtml @@ -10,6 +10,7 @@ @if (Model.IsAdmin) { -

Go to edit templates

+

Manage pages

+

Manage admin groups

}
diff --git a/Pages/Shared/_Layout.cshtml b/Pages/Shared/_Layout.cshtml index bcc1117..695eb7b 100644 --- a/Pages/Shared/_Layout.cshtml +++ b/Pages/Shared/_Layout.cshtml @@ -20,6 +20,8 @@ @if (User.Identity?.IsAuthenticated == true && User.IsInRole("Admin")) { Manage pages +
+ Manage admin groups }