revamp
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using maintenance.Authentication.Configuration;
|
||||
using maintenance.Data;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace maintenance.Authentication.Core;
|
||||
@@ -15,30 +17,43 @@ public sealed class AdminRoleClaimsTransformation : IClaimsTransformation
|
||||
};
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly AppDbContext _dbContext;
|
||||
|
||||
public AdminRoleClaimsTransformation(IConfiguration configuration)
|
||||
public AdminRoleClaimsTransformation(IConfiguration configuration, AppDbContext dbContext)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
|
||||
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
|
||||
{
|
||||
if (principal.Identity is not ClaimsIdentity identity || !identity.IsAuthenticated)
|
||||
{
|
||||
return Task.FromResult(principal);
|
||||
return principal;
|
||||
}
|
||||
|
||||
if (identity.Claims.Any(c => c.Type == ClaimTypes.Role && c.Value == AuthRoles.Admin))
|
||||
{
|
||||
return Task.FromResult(principal);
|
||||
return principal;
|
||||
}
|
||||
|
||||
var configuredGroups = (_configuration[AuthEnvironment.AdminGroups] ?? string.Empty)
|
||||
.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
|
||||
if (configuredGroups.Length == 0)
|
||||
var dbGroups = await _dbContext.AuthorizationGroups
|
||||
.AsNoTracking()
|
||||
.Where(g => g.Active && g.Role == "admin")
|
||||
.Select(g => g.GroupClaimValue)
|
||||
.ToListAsync();
|
||||
|
||||
var effectiveGroups = configuredGroups
|
||||
.Concat(dbGroups)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
|
||||
if (effectiveGroups.Length == 0)
|
||||
{
|
||||
return Task.FromResult(principal);
|
||||
return principal;
|
||||
}
|
||||
|
||||
var principalGroups = identity.Claims
|
||||
@@ -46,14 +61,14 @@ public sealed class AdminRoleClaimsTransformation : IClaimsTransformation
|
||||
.Select(c => c.Value);
|
||||
|
||||
var isAdmin = principalGroups.Any(group =>
|
||||
configuredGroups.Contains(group, StringComparer.OrdinalIgnoreCase));
|
||||
effectiveGroups.Contains(group, StringComparer.OrdinalIgnoreCase));
|
||||
|
||||
if (isAdmin)
|
||||
{
|
||||
identity.AddClaim(new Claim(ClaimTypes.Role, AuthRoles.Admin));
|
||||
}
|
||||
|
||||
return Task.FromResult(principal);
|
||||
return principal;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ public sealed class AppDbContext : DbContext
|
||||
public DbSet<AuthorizationGroupEntity> AuthorizationGroups => Set<AuthorizationGroupEntity>();
|
||||
public DbSet<PermissionRuleEntity> PermissionRules => Set<PermissionRuleEntity>();
|
||||
public DbSet<AuditRecordEntity> AuditRecords => Set<AuditRecordEntity>();
|
||||
public DbSet<ManagedPageEntity> ManagedPages => Set<ManagedPageEntity>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
@@ -87,5 +88,19 @@ public sealed class AppDbContext : DbContext
|
||||
e.Property(x => x.Target).HasMaxLength(200).IsRequired();
|
||||
e.HasIndex(x => x.TimestampUtc);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<ManagedPageEntity>(e =>
|
||||
{
|
||||
e.ToTable("ManagedPage");
|
||||
e.HasKey(x => x.Id);
|
||||
e.Property(x => x.Key).HasMaxLength(100).IsRequired();
|
||||
e.Property(x => x.DisplayName).HasMaxLength(200).IsRequired();
|
||||
e.Property(x => x.PageKind).HasMaxLength(50).IsRequired();
|
||||
e.Property(x => x.RecurrenceRule).HasMaxLength(200);
|
||||
e.Property(x => x.LastUpdatedBy).HasMaxLength(200).IsRequired();
|
||||
e.Property(x => x.ContentJson).IsRequired();
|
||||
e.HasIndex(x => x.Key).IsUnique();
|
||||
e.HasIndex(x => new { x.PageKind, x.IsEnabled, x.Priority });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace maintenance.Data.Entities;
|
||||
|
||||
public sealed class ManagedPageEntity
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Key { get; set; } = string.Empty;
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
public string PageKind { get; set; } = "maintenance"; // maintenance | error
|
||||
public bool IsDefaultFallback { get; set; }
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
public bool OverrideWindow { get; set; }
|
||||
public int Priority { get; set; }
|
||||
|
||||
public DateTime? StartUtc { get; set; }
|
||||
public DateTime? EndUtc { get; set; }
|
||||
public string? RecurrenceRule { get; set; } // cron-like expression (future evaluator)
|
||||
|
||||
// Structured content payload rendered into a locked template.
|
||||
public string ContentJson { get; set; } = "{}";
|
||||
|
||||
public string LastUpdatedBy { get; set; } = string.Empty;
|
||||
public DateTime LastUpdatedAtUtc { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System.Text.Json;
|
||||
using maintenance.Data.Entities;
|
||||
using maintenance.Services.Pages;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace maintenance.Data.Seed;
|
||||
|
||||
public static class ManagedPageSeeder
|
||||
{
|
||||
public static async Task SeedAsync(AppDbContext dbContext, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var defaults = new[]
|
||||
{
|
||||
new ManagedPageEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Key = "generic-maintenance",
|
||||
DisplayName = "Generic Maintenance",
|
||||
PageKind = "maintenance",
|
||||
IsDefaultFallback = true,
|
||||
IsEnabled = true,
|
||||
OverrideWindow = false,
|
||||
Priority = 0,
|
||||
ContentJson = JsonSerializer.Serialize(new PageContentModel
|
||||
{
|
||||
ErrorName = "Application Unavailable",
|
||||
Description = "MAX applications may be down for scheduled maintenance 2AM-8AM Sunday morning. If this appears at another time, contact support.",
|
||||
ErrorCode = "eau",
|
||||
ShowInstructions = false
|
||||
}),
|
||||
LastUpdatedBy = "system-seed",
|
||||
LastUpdatedAtUtc = DateTime.UtcNow
|
||||
},
|
||||
new ManagedPageEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Key = "generic-error",
|
||||
DisplayName = "Generic Error",
|
||||
PageKind = "error",
|
||||
IsDefaultFallback = true,
|
||||
IsEnabled = true,
|
||||
OverrideWindow = false,
|
||||
Priority = 0,
|
||||
ContentJson = JsonSerializer.Serialize(new PageContentModel
|
||||
{
|
||||
ErrorName = "System Error",
|
||||
Description = "A system error has occurred. Please contact MAX Support.",
|
||||
ErrorCode = "eag",
|
||||
ShowInstructions = false
|
||||
}),
|
||||
LastUpdatedBy = "system-seed",
|
||||
LastUpdatedAtUtc = DateTime.UtcNow
|
||||
}
|
||||
};
|
||||
|
||||
var existingKeys = await dbContext.ManagedPages
|
||||
.AsNoTracking()
|
||||
.Select(x => x.Key)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var missing = defaults
|
||||
.Where(x => !existingKeys.Contains(x.Key, StringComparer.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
|
||||
if (missing.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await dbContext.ManagedPages.AddRangeAsync(missing, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
<div class="text-center">
|
||||
<h1 class="display-5">Admin Console</h1>
|
||||
<p>This page is protected by the <code>AdminOnly</code> policy.</p>
|
||||
<p><a asp-page="/Admin/Templates/Index">Manage Templates</a></p>
|
||||
<p><a asp-page="/Admin/Templates/Index">Manage Pages</a></p>
|
||||
<p><a asp-page="/Admin/Groups/Index">Manage Admin Groups</a></p>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
}
|
||||
|
||||
<h1>Create Page From Template</h1>
|
||||
<p>Creates a new managed page by copying content and schedule fields from an existing page.</p>
|
||||
|
||||
<form method="post">
|
||||
<div class="mb-3">
|
||||
@@ -14,15 +15,15 @@
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.NewName" class="form-label"></label>
|
||||
<input asp-for="Input.NewName" class="form-control" />
|
||||
<span asp-validation-for="Input.NewName" class="text-danger"></span>
|
||||
<label asp-for="Input.NewKey" class="form-label"></label>
|
||||
<input asp-for="Input.NewKey" class="form-control" />
|
||||
<span asp-validation-for="Input.NewKey" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.NewTitle" class="form-label"></label>
|
||||
<input asp-for="Input.NewTitle" class="form-control" />
|
||||
<span asp-validation-for="Input.NewTitle" class="text-danger"></span>
|
||||
<label asp-for="Input.DisplayName" class="form-label"></label>
|
||||
<input asp-for="Input.DisplayName" class="form-control" />
|
||||
<span asp-validation-for="Input.DisplayName" class="text-danger"></span>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Create</button>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json;
|
||||
using maintenance.Data;
|
||||
using maintenance.Data.Entities;
|
||||
using maintenance.Services.Pages;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
@@ -30,15 +32,15 @@ public sealed class CreateModel : PageModel
|
||||
public Guid SourceTemplateId { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(50, MinimumLength = 2)]
|
||||
[StringLength(100, MinimumLength = 3)]
|
||||
[RegularExpression("^[a-z0-9-]+$")]
|
||||
[Display(Name = "New Template Name")]
|
||||
public string NewName { get; set; } = string.Empty;
|
||||
[Display(Name = "Page Key")]
|
||||
public string NewKey { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[StringLength(200)]
|
||||
[Display(Name = "New Template Title")]
|
||||
public string NewTitle { get; set; } = string.Empty;
|
||||
[Display(Name = "Display Name")]
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public async Task OnGetAsync()
|
||||
@@ -55,38 +57,45 @@ public sealed class CreateModel : PageModel
|
||||
return Page();
|
||||
}
|
||||
|
||||
var source = await _dbContext.Templates
|
||||
var source = await _dbContext.ManagedPages
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(t => t.Id == Input.SourceTemplateId, HttpContext.RequestAborted);
|
||||
|
||||
if (source is null)
|
||||
{
|
||||
ModelState.AddModelError(nameof(Input.SourceTemplateId), "Source template was not found.");
|
||||
ModelState.AddModelError(nameof(Input.SourceTemplateId), "Source page was not found.");
|
||||
return Page();
|
||||
}
|
||||
|
||||
var normalizedName = Input.NewName.Trim().ToLowerInvariant();
|
||||
var exists = await _dbContext.Templates
|
||||
.AnyAsync(t => t.Name == normalizedName, HttpContext.RequestAborted);
|
||||
var normalizedKey = Input.NewKey.Trim().ToLowerInvariant();
|
||||
var exists = await _dbContext.ManagedPages
|
||||
.AnyAsync(t => t.Key == normalizedKey, HttpContext.RequestAborted);
|
||||
|
||||
if (exists)
|
||||
{
|
||||
ModelState.AddModelError(nameof(Input.NewName), "A template with this name already exists.");
|
||||
ModelState.AddModelError(nameof(Input.NewKey), "A page with this key already exists.");
|
||||
return Page();
|
||||
}
|
||||
|
||||
var clone = new TemplateEntity
|
||||
var clone = new ManagedPageEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = normalizedName,
|
||||
Title = Input.NewTitle.Trim(),
|
||||
Body = source.Body,
|
||||
SupportContactBlock = source.SupportContactBlock,
|
||||
Key = normalizedKey,
|
||||
DisplayName = Input.DisplayName.Trim(),
|
||||
PageKind = source.PageKind,
|
||||
IsDefaultFallback = false,
|
||||
IsEnabled = false,
|
||||
OverrideWindow = false,
|
||||
Priority = source.Priority,
|
||||
StartUtc = source.StartUtc,
|
||||
EndUtc = source.EndUtc,
|
||||
RecurrenceRule = source.RecurrenceRule,
|
||||
ContentJson = CloneJson(source.ContentJson),
|
||||
LastUpdatedBy = User.Identity?.Name ?? "unknown",
|
||||
LastUpdatedAtUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
_dbContext.Templates.Add(clone);
|
||||
_dbContext.ManagedPages.Add(clone);
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
|
||||
return RedirectToPage("./Edit", new { id = clone.Id });
|
||||
@@ -94,14 +103,28 @@ public sealed class CreateModel : PageModel
|
||||
|
||||
private async Task LoadSourcesAsync()
|
||||
{
|
||||
SourceTemplates = await _dbContext.Templates
|
||||
SourceTemplates = await _dbContext.ManagedPages
|
||||
.AsNoTracking()
|
||||
.OrderBy(t => t.Name)
|
||||
.OrderBy(t => t.PageKind)
|
||||
.ThenBy(t => t.Key)
|
||||
.Select(t => new SelectListItem
|
||||
{
|
||||
Value = t.Id.ToString(),
|
||||
Text = $"{t.Name} ({t.Title})"
|
||||
Text = $"{t.PageKind} - {t.Key}"
|
||||
})
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
private static string CloneJson(string json)
|
||||
{
|
||||
try
|
||||
{
|
||||
var model = JsonSerializer.Deserialize<PageContentModel>(json) ?? new PageContentModel();
|
||||
return JsonSerializer.Serialize(model);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return JsonSerializer.Serialize(new PageContentModel());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,36 +1,120 @@
|
||||
@page "{id:guid}"
|
||||
@model maintenance.Pages.Admin.Templates.EditModel
|
||||
@{
|
||||
ViewData["Title"] = "Edit Template";
|
||||
ViewData["Title"] = "Edit Page";
|
||||
}
|
||||
|
||||
<h1>Edit Template</h1>
|
||||
<h1>Edit Page</h1>
|
||||
<p><a href="@Model.PreviewUrl" target="_blank" rel="noopener">Preview this page</a></p>
|
||||
|
||||
<form method="post">
|
||||
<input asp-for="Input.Id" type="hidden" />
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Name" class="form-label"></label>
|
||||
<input asp-for="Input.Name" class="form-control" readonly />
|
||||
<div class="row">
|
||||
<div class="col-md-6 mb-3">
|
||||
<label asp-for="Input.Key" class="form-label"></label>
|
||||
<input asp-for="Input.Key" class="form-control" readonly />
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label asp-for="Input.DisplayName" class="form-label"></label>
|
||||
<input asp-for="Input.DisplayName" class="form-control" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Title" class="form-label"></label>
|
||||
<input asp-for="Input.Title" class="form-control" />
|
||||
<span asp-validation-for="Input.Title" class="text-danger"></span>
|
||||
<div class="row">
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.PageKind" class="form-label"></label>
|
||||
<select asp-for="Input.PageKind" class="form-select">
|
||||
<option value="maintenance">maintenance</option>
|
||||
<option value="error">error</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.Priority" class="form-label"></label>
|
||||
<input asp-for="Input.Priority" class="form-control" type="number" />
|
||||
</div>
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.RecurrenceRule" class="form-label"></label>
|
||||
<input asp-for="Input.RecurrenceRule" class="form-control" placeholder="cron-like (future)" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Body" class="form-label"></label>
|
||||
<textarea asp-for="Input.Body" class="form-control" rows="5"></textarea>
|
||||
<span asp-validation-for="Input.Body" class="text-danger"></span>
|
||||
<div class="row">
|
||||
<div class="col-md-3 form-check mb-3">
|
||||
<input asp-for="Input.IsEnabled" class="form-check-input" />
|
||||
<label asp-for="Input.IsEnabled" class="form-check-label"></label>
|
||||
</div>
|
||||
<div class="col-md-3 form-check mb-3">
|
||||
<input asp-for="Input.IsDefaultFallback" class="form-check-input" />
|
||||
<label asp-for="Input.IsDefaultFallback" class="form-check-label"></label>
|
||||
</div>
|
||||
<div class="col-md-3 form-check mb-3">
|
||||
<input asp-for="Input.OverrideWindow" class="form-check-input" />
|
||||
<label asp-for="Input.OverrideWindow" class="form-check-label"></label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-6 mb-3">
|
||||
<label asp-for="Input.StartUtc" class="form-label"></label>
|
||||
<input asp-for="Input.StartUtc" class="form-control" type="datetime-local" />
|
||||
</div>
|
||||
<div class="col-md-6 mb-3">
|
||||
<label asp-for="Input.EndUtc" class="form-label"></label>
|
||||
<input asp-for="Input.EndUtc" class="form-control" type="datetime-local" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<hr />
|
||||
<h4>Page Content (Template Slots)</h4>
|
||||
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.SupportContactBlock" class="form-label"></label>
|
||||
<textarea asp-for="Input.SupportContactBlock" class="form-control" rows="6"></textarea>
|
||||
<span asp-validation-for="Input.SupportContactBlock" class="text-danger"></span>
|
||||
<label asp-for="Input.ErrorName" class="form-label"></label>
|
||||
<input asp-for="Input.ErrorName" class="form-control" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.ShortDescription" class="form-label"></label>
|
||||
<input asp-for="Input.ShortDescription" class="form-control" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.Description" class="form-label"></label>
|
||||
<textarea asp-for="Input.Description" class="form-control" rows="3"></textarea>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.InstructionsHeader" class="form-label"></label>
|
||||
<input asp-for="Input.InstructionsHeader" class="form-control" />
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.InstructionSteps" class="form-label"></label>
|
||||
<input asp-for="Input.InstructionSteps" class="form-control" placeholder="step 1|step 2|step 3" />
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-3 form-check mb-3">
|
||||
<input asp-for="Input.ShowInstructions" class="form-check-input" />
|
||||
<label asp-for="Input.ShowInstructions" class="form-check-label"></label>
|
||||
</div>
|
||||
<div class="col-md-3 form-check mb-3">
|
||||
<input asp-for="Input.ShowSupport" class="form-check-input" />
|
||||
<label asp-for="Input.ShowSupport" class="form-check-label"></label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.SupportEmail" class="form-label"></label>
|
||||
<input asp-for="Input.SupportEmail" class="form-control" />
|
||||
</div>
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.SupportPhone" class="form-label"></label>
|
||||
<input asp-for="Input.SupportPhone" class="form-control" />
|
||||
</div>
|
||||
<div class="col-md-4 mb-3">
|
||||
<label asp-for="Input.ErrorCode" class="form-label"></label>
|
||||
<input asp-for="Input.ErrorCode" class="form-control" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label asp-for="Input.SupportHours" class="form-label"></label>
|
||||
<input asp-for="Input.SupportHours" class="form-control" />
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json;
|
||||
using maintenance.Data;
|
||||
using maintenance.Services.Pages;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
@@ -20,46 +22,90 @@ public sealed class EditModel : PageModel
|
||||
[BindProperty]
|
||||
public InputModel Input { get; set; } = new();
|
||||
|
||||
public string PreviewUrl { get; private set; } = "/errors/template";
|
||||
public string PreviewUrl { get; private set; } = "/errors/maintenance";
|
||||
|
||||
public sealed class InputModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string Key { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[StringLength(200)]
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string DisplayName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Body { get; set; } = string.Empty;
|
||||
[RegularExpression("^(maintenance|error)$")]
|
||||
public string PageKind { get; set; } = "maintenance";
|
||||
|
||||
public string? SupportContactBlock { get; set; }
|
||||
public bool IsDefaultFallback { get; set; }
|
||||
public bool IsEnabled { get; set; }
|
||||
public bool OverrideWindow { get; set; }
|
||||
public int Priority { get; set; }
|
||||
public DateTime? StartUtc { get; set; }
|
||||
public DateTime? EndUtc { get; set; }
|
||||
public string? RecurrenceRule { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(200)]
|
||||
public string ErrorName { get; set; } = string.Empty;
|
||||
public string? ShortDescription { get; set; }
|
||||
[Required]
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string? InstructionsHeader { get; set; }
|
||||
public string? InstructionSteps { get; set; }
|
||||
public bool ShowInstructions { get; set; } = true;
|
||||
public bool ShowSupport { get; set; } = true;
|
||||
[Required]
|
||||
public string SupportEmail { get; set; } = "maxsupport@max.gov";
|
||||
[Required]
|
||||
public string SupportPhone { get; set; } = "202-395-6860";
|
||||
[Required]
|
||||
public string SupportHours { get; set; } = "8:00AM - 8:00PM Weekdays, 9:00AM - 6:00PM Weekends (ET)";
|
||||
[Required]
|
||||
public string ErrorCode { get; set; } = "eag";
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnGetAsync(Guid id)
|
||||
{
|
||||
var template = await _dbContext.Templates
|
||||
var page = await _dbContext.ManagedPages
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(t => t.Id == id, HttpContext.RequestAborted);
|
||||
|
||||
if (template is null)
|
||||
if (page is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var content = Deserialize(page.ContentJson);
|
||||
|
||||
Input = new InputModel
|
||||
{
|
||||
Id = template.Id,
|
||||
Name = template.Name,
|
||||
Title = template.Title,
|
||||
Body = template.Body,
|
||||
SupportContactBlock = template.SupportContactBlock
|
||||
Id = page.Id,
|
||||
Key = page.Key,
|
||||
DisplayName = page.DisplayName,
|
||||
PageKind = page.PageKind,
|
||||
IsDefaultFallback = page.IsDefaultFallback,
|
||||
IsEnabled = page.IsEnabled,
|
||||
OverrideWindow = page.OverrideWindow,
|
||||
Priority = page.Priority,
|
||||
StartUtc = page.StartUtc,
|
||||
EndUtc = page.EndUtc,
|
||||
RecurrenceRule = page.RecurrenceRule,
|
||||
ErrorName = content.ErrorName,
|
||||
ShortDescription = content.ShortDescription,
|
||||
Description = content.Description,
|
||||
InstructionsHeader = content.InstructionsHeader,
|
||||
InstructionSteps = content.InstructionSteps.Count == 0 ? null : string.Join('|', content.InstructionSteps),
|
||||
ShowInstructions = content.ShowInstructions,
|
||||
ShowSupport = content.ShowSupport,
|
||||
SupportEmail = content.SupportEmail,
|
||||
SupportPhone = content.SupportPhone,
|
||||
SupportHours = content.SupportHours,
|
||||
ErrorCode = content.ErrorCode
|
||||
};
|
||||
|
||||
PreviewUrl = BuildPreviewUrl(template.Name);
|
||||
|
||||
PreviewUrl = BuildPreviewUrl(page.PageKind, page.Key);
|
||||
return Page();
|
||||
}
|
||||
|
||||
@@ -67,42 +113,75 @@ public sealed class EditModel : PageModel
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
PreviewUrl = BuildPreviewUrl(Input.PageKind, Input.Key);
|
||||
return Page();
|
||||
}
|
||||
|
||||
var template = await _dbContext.Templates
|
||||
var page = await _dbContext.ManagedPages
|
||||
.FirstOrDefaultAsync(t => t.Id == Input.Id, HttpContext.RequestAborted);
|
||||
|
||||
if (template is null)
|
||||
if (page is null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
template.Title = Input.Title.Trim();
|
||||
template.Body = Input.Body.Trim();
|
||||
template.SupportContactBlock = string.IsNullOrWhiteSpace(Input.SupportContactBlock)
|
||||
? null
|
||||
: Input.SupportContactBlock.Trim();
|
||||
template.LastUpdatedAtUtc = DateTime.UtcNow;
|
||||
template.LastUpdatedBy = User.Identity?.Name ?? "unknown";
|
||||
page.DisplayName = Input.DisplayName.Trim();
|
||||
page.PageKind = Input.PageKind.Trim().ToLowerInvariant();
|
||||
page.IsDefaultFallback = Input.IsDefaultFallback;
|
||||
page.IsEnabled = Input.IsEnabled;
|
||||
page.OverrideWindow = Input.OverrideWindow;
|
||||
page.Priority = Input.Priority;
|
||||
page.StartUtc = Input.StartUtc;
|
||||
page.EndUtc = Input.EndUtc;
|
||||
page.RecurrenceRule = string.IsNullOrWhiteSpace(Input.RecurrenceRule) ? null : Input.RecurrenceRule.Trim();
|
||||
page.ContentJson = JsonSerializer.Serialize(new PageContentModel
|
||||
{
|
||||
ErrorName = Input.ErrorName.Trim(),
|
||||
ShortDescription = string.IsNullOrWhiteSpace(Input.ShortDescription) ? null : Input.ShortDescription.Trim(),
|
||||
Description = Input.Description.Trim(),
|
||||
InstructionsHeader = string.IsNullOrWhiteSpace(Input.InstructionsHeader) ? null : Input.InstructionsHeader.Trim(),
|
||||
InstructionSteps = ParseSteps(Input.InstructionSteps),
|
||||
ShowInstructions = Input.ShowInstructions,
|
||||
ShowSupport = Input.ShowSupport,
|
||||
SupportEmail = Input.SupportEmail.Trim(),
|
||||
SupportPhone = Input.SupportPhone.Trim(),
|
||||
SupportHours = Input.SupportHours.Trim(),
|
||||
ErrorCode = Input.ErrorCode.Trim()
|
||||
});
|
||||
page.LastUpdatedAtUtc = DateTime.UtcNow;
|
||||
page.LastUpdatedBy = User.Identity?.Name ?? "unknown";
|
||||
|
||||
await _dbContext.SaveChangesAsync(HttpContext.RequestAborted);
|
||||
|
||||
return RedirectToPage("./Index");
|
||||
}
|
||||
|
||||
private static string BuildPreviewUrl(string templateName)
|
||||
private static PageContentModel Deserialize(string json)
|
||||
{
|
||||
if (templateName.Equals("generic", StringComparison.OrdinalIgnoreCase))
|
||||
try
|
||||
{
|
||||
return "/errors/generic";
|
||||
return JsonSerializer.Deserialize<PageContentModel>(json) ?? new PageContentModel();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new PageContentModel();
|
||||
}
|
||||
}
|
||||
|
||||
private static List<string> ParseSteps(string? steps)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(steps))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
if (templateName.Equals("unavailable", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "/errors/unavailable";
|
||||
}
|
||||
return steps.Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
return $"/errors/template?template={Uri.EscapeDataString(templateName)}";
|
||||
private static string BuildPreviewUrl(string kind, string key)
|
||||
{
|
||||
return $"/errors/{kind}?page={Uri.EscapeDataString(key)}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,15 +4,18 @@
|
||||
ViewData["Title"] = "Templates";
|
||||
}
|
||||
|
||||
<h1>Templates</h1>
|
||||
<p>Manage error template content used by user-facing pages.</p>
|
||||
<h1>Manage Pages</h1>
|
||||
<p>Create and schedule maintenance/error pages while keeping the visual template fixed.</p>
|
||||
<p><a asp-page="./Create" class="btn btn-primary">Create page from template</a></p>
|
||||
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Title</th>
|
||||
<th>Key</th>
|
||||
<th>Kind</th>
|
||||
<th>Default</th>
|
||||
<th>Enabled</th>
|
||||
<th>Window</th>
|
||||
<th>Updated</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
@@ -21,12 +24,15 @@
|
||||
@foreach (var item in Model.Templates)
|
||||
{
|
||||
<tr>
|
||||
<td>@item.Name</td>
|
||||
<td>@item.Title</td>
|
||||
<td>@item.Key</td>
|
||||
<td>@item.PageKind</td>
|
||||
<td>@item.IsDefaultFallback</td>
|
||||
<td>@item.IsEnabled</td>
|
||||
<td>@(item.StartUtc?.ToString("u") ?? "-") to @(item.EndUtc?.ToString("u") ?? "-")</td>
|
||||
<td>@item.LastUpdatedAtUtc.ToString("u")</td>
|
||||
<td>
|
||||
<a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
|
||||
<a href="@Model.GetPreviewUrl(item.Name)" target="_blank" rel="noopener">Preview</a>
|
||||
<a href="@Model.GetPreviewUrl(item)" target="_blank" rel="noopener">Preview</a>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
|
||||
@@ -16,28 +16,20 @@ public sealed class IndexModel : PageModel
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public IReadOnlyList<TemplateEntity> Templates { get; private set; } = [];
|
||||
public IReadOnlyList<ManagedPageEntity> Templates { get; private set; } = [];
|
||||
|
||||
public async Task OnGetAsync()
|
||||
{
|
||||
Templates = await _dbContext.Templates
|
||||
Templates = await _dbContext.ManagedPages
|
||||
.AsNoTracking()
|
||||
.OrderBy(t => t.Name)
|
||||
.OrderBy(t => t.PageKind)
|
||||
.ThenByDescending(t => t.IsDefaultFallback)
|
||||
.ThenBy(t => t.Key)
|
||||
.ToListAsync(HttpContext.RequestAborted);
|
||||
}
|
||||
|
||||
public string GetPreviewUrl(string templateName)
|
||||
public string GetPreviewUrl(ManagedPageEntity page)
|
||||
{
|
||||
if (templateName.Equals("generic", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "/errors/generic";
|
||||
}
|
||||
|
||||
if (templateName.Equals("unavailable", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "/errors/unavailable";
|
||||
}
|
||||
|
||||
return $"/errors/template?template={Uri.EscapeDataString(templateName)}";
|
||||
return $"/errors/{page.PageKind}?page={Uri.EscapeDataString(page.Key)}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,16 +21,18 @@ public sealed class IndexModel : PageModel
|
||||
? (_configuration["ErrorPages:DefaultTemplate"] ?? "unavailable")
|
||||
: TemplateName;
|
||||
|
||||
if (effectiveTemplate.Equals("generic", StringComparison.OrdinalIgnoreCase))
|
||||
if (effectiveTemplate.Equals("generic", StringComparison.OrdinalIgnoreCase) ||
|
||||
effectiveTemplate.Equals("error", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return Redirect("/errors/generic");
|
||||
return Redirect("/errors/error");
|
||||
}
|
||||
|
||||
if (effectiveTemplate.Equals("unavailable", StringComparison.OrdinalIgnoreCase))
|
||||
if (effectiveTemplate.Equals("unavailable", StringComparison.OrdinalIgnoreCase) ||
|
||||
effectiveTemplate.Equals("maintenance", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return Redirect("/errors/unavailable");
|
||||
return Redirect("/errors/maintenance");
|
||||
}
|
||||
|
||||
return Redirect($"/errors/template?template={Uri.EscapeDataString(effectiveTemplate)}");
|
||||
return Redirect($"/errors/maintenance?page={Uri.EscapeDataString(effectiveTemplate)}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
@page "/errors/{kind}"
|
||||
@model maintenance.Pages.Errors.RenderModel
|
||||
@await Html.PartialAsync("_LockedTemplate", Model.Resolved)
|
||||
@@ -0,0 +1,29 @@
|
||||
using maintenance.Services.Pages;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
|
||||
namespace maintenance.Pages.Errors;
|
||||
|
||||
public sealed class RenderModel : PageModel
|
||||
{
|
||||
private readonly IManagedPageService _managedPageService;
|
||||
|
||||
public RenderModel(IManagedPageService managedPageService)
|
||||
{
|
||||
_managedPageService = managedPageService;
|
||||
}
|
||||
|
||||
public ResolvedPage Resolved { get; private set; } = new();
|
||||
|
||||
public async Task<IActionResult> OnGetAsync(string kind, [FromQuery] string? page = null)
|
||||
{
|
||||
if (!kind.Equals("maintenance", StringComparison.OrdinalIgnoreCase) &&
|
||||
!kind.Equals("error", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
Resolved = await _managedPageService.ResolveAsync(kind, page, DateTime.UtcNow, HttpContext.RequestAborted);
|
||||
return Page();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
@model maintenance.Services.Pages.ResolvedPage
|
||||
@{
|
||||
Layout = null;
|
||||
}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>MAX - @Model.Content.ErrorName</title>
|
||||
<link rel="stylesheet" href="~/css/max_error.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">MAX</div>
|
||||
<div class="center-box-vertically" style="margin-bottom: -300px;"></div>
|
||||
<div class="box">
|
||||
<div class="title"><img src="~/favicon.ico" alt="" /><h1>@Model.Content.ErrorName</h1></div>
|
||||
<div class="description">
|
||||
@if (!string.IsNullOrWhiteSpace(Model.Content.ShortDescription))
|
||||
{
|
||||
<h2>@Model.Content.ShortDescription</h2>
|
||||
}
|
||||
<h2>@Model.Content.Description</h2>
|
||||
</div>
|
||||
@if (Model.Content.ShowInstructions)
|
||||
{
|
||||
<div class="instructions">
|
||||
@if (!string.IsNullOrWhiteSpace(Model.Content.InstructionsHeader))
|
||||
{
|
||||
<h2>@Model.Content.InstructionsHeader</h2>
|
||||
<br />
|
||||
}
|
||||
@if (Model.Content.InstructionSteps.Count > 0)
|
||||
{
|
||||
<h2>To correct this problem:</h2>
|
||||
<h4>
|
||||
<ol>
|
||||
@foreach (var step in Model.Content.InstructionSteps)
|
||||
{
|
||||
<li>@step</li>
|
||||
}
|
||||
</ol>
|
||||
</h4>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
@if (Model.Content.ShowSupport)
|
||||
{
|
||||
<div class="support">
|
||||
<h2>MAX Support:</h2>
|
||||
<br />
|
||||
<h3>Email: <a href="mailto:@Model.Content.SupportEmail">@Model.Content.SupportEmail</a></h3>
|
||||
<h4>@Model.Content.SupportHours</h4>
|
||||
<br />
|
||||
<h3>Phone: @Model.Content.SupportPhone</h3>
|
||||
<h4>All hours in Eastern Time (ET)</h4>
|
||||
</div>
|
||||
}
|
||||
<div class="code"><h4>(Error Code: @Model.Content.ErrorCode)</h4></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
+4
-1
@@ -5,6 +5,7 @@ using maintenance.Authentication.Providers.AdfsWsFed;
|
||||
using maintenance.Authentication.Providers.Oidc;
|
||||
using maintenance.Data;
|
||||
using maintenance.Data.Seed;
|
||||
using maintenance.Services.Pages;
|
||||
using maintenance.Services.Templates;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
@@ -43,13 +44,14 @@ builder.Services.AddDbContext<GroupLookupDbContext>(options =>
|
||||
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
});
|
||||
builder.Services.AddScoped<ITemplateService, TemplateService>();
|
||||
builder.Services.AddScoped<IManagedPageService, ManagedPageService>();
|
||||
|
||||
builder.Services.AddRazorPages(options =>
|
||||
{
|
||||
options.Conventions.AuthorizeFolder("/Admin", "AdminOnly");
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<IClaimsTransformation, AdminRoleClaimsTransformation>();
|
||||
builder.Services.AddScoped<IClaimsTransformation, AdminRoleClaimsTransformation>();
|
||||
builder.Services.AddSingleton<IIdentityContextFactory, IdentityContextFactory>();
|
||||
builder.Services.AddSingleton<IAuthProvider, AdfsWsFedAuthProvider>();
|
||||
builder.Services.AddSingleton<IAuthProvider, OidcAuthProvider>();
|
||||
@@ -105,6 +107,7 @@ if (app.Environment.IsDevelopment())
|
||||
try
|
||||
{
|
||||
await TemplateSeeder.SeedAsync(db);
|
||||
await ManagedPageSeeder.SeedAsync(db);
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace maintenance.Services.Pages;
|
||||
|
||||
public interface IManagedPageService
|
||||
{
|
||||
Task<ResolvedPage> ResolveAsync(
|
||||
string pageKind,
|
||||
string? requestedPageKey = null,
|
||||
DateTime? nowUtc = null,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
using System.Text.Json;
|
||||
using maintenance.Data;
|
||||
using maintenance.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace maintenance.Services.Pages;
|
||||
|
||||
public sealed class ManagedPageService : IManagedPageService
|
||||
{
|
||||
private readonly AppDbContext _dbContext;
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true
|
||||
};
|
||||
|
||||
public ManagedPageService(AppDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<ResolvedPage> ResolveAsync(
|
||||
string pageKind,
|
||||
string? requestedPageKey = null,
|
||||
DateTime? nowUtc = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var normalizedKind = NormalizeKind(pageKind);
|
||||
var utcNow = nowUtc ?? DateTime.UtcNow;
|
||||
|
||||
var pages = await _dbContext.ManagedPages
|
||||
.AsNoTracking()
|
||||
.Where(p => p.PageKind == normalizedKind && p.IsEnabled)
|
||||
.OrderByDescending(p => p.OverrideWindow)
|
||||
.ThenByDescending(p => p.Priority)
|
||||
.ThenBy(p => p.Key)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
ManagedPageEntity? selected = null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(requestedPageKey))
|
||||
{
|
||||
selected = pages.FirstOrDefault(p =>
|
||||
p.Key.Equals(requestedPageKey, StringComparison.OrdinalIgnoreCase) &&
|
||||
IsWindowActive(p, utcNow));
|
||||
}
|
||||
|
||||
selected ??= pages.FirstOrDefault(p => p.OverrideWindow && IsWindowActive(p, utcNow));
|
||||
selected ??= pages.FirstOrDefault(p => !p.IsDefaultFallback && IsWindowActive(p, utcNow));
|
||||
selected ??= pages.FirstOrDefault(p => p.IsDefaultFallback);
|
||||
|
||||
if (selected is null)
|
||||
{
|
||||
return new ResolvedPage
|
||||
{
|
||||
PageKind = normalizedKind,
|
||||
PageKey = normalizedKind == "maintenance" ? "generic-maintenance" : "generic-error",
|
||||
IsDefaultFallback = true,
|
||||
Content = DefaultContent(normalizedKind)
|
||||
};
|
||||
}
|
||||
|
||||
return new ResolvedPage
|
||||
{
|
||||
PageKind = normalizedKind,
|
||||
PageKey = selected.Key,
|
||||
IsDefaultFallback = selected.IsDefaultFallback,
|
||||
Content = DeserializeContent(selected.ContentJson, normalizedKind)
|
||||
};
|
||||
}
|
||||
|
||||
private static string NormalizeKind(string pageKind)
|
||||
{
|
||||
return pageKind.Equals("error", StringComparison.OrdinalIgnoreCase) ? "error" : "maintenance";
|
||||
}
|
||||
|
||||
private static bool IsWindowActive(ManagedPageEntity page, DateTime utcNow)
|
||||
{
|
||||
// Initial behavior:
|
||||
// - If no window set => active.
|
||||
// - If one-time window set => active only inside start/end.
|
||||
// - RecurrenceRule parsing is future enhancement; currently treated as active when no one-time bounds fail.
|
||||
if (page.StartUtc is null && page.EndUtc is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (page.StartUtc.HasValue && utcNow < page.StartUtc.Value)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (page.EndUtc.HasValue && utcNow > page.EndUtc.Value)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static PageContentModel DeserializeContent(string contentJson, string kind)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(contentJson))
|
||||
{
|
||||
return DefaultContent(kind);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<PageContentModel>(contentJson, JsonOptions)
|
||||
?? DefaultContent(kind);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return DefaultContent(kind);
|
||||
}
|
||||
}
|
||||
|
||||
private static PageContentModel DefaultContent(string kind)
|
||||
{
|
||||
if (kind == "maintenance")
|
||||
{
|
||||
return new PageContentModel
|
||||
{
|
||||
ErrorName = "Application Unavailable",
|
||||
Description =
|
||||
"MAX applications may be down for scheduled maintenance 2AM-8AM Sunday morning. If this appears at another time, contact support.",
|
||||
ErrorCode = "eau",
|
||||
ShowInstructions = false
|
||||
};
|
||||
}
|
||||
|
||||
return new PageContentModel
|
||||
{
|
||||
ErrorName = "System Error",
|
||||
Description = "A system error has occurred. Please contact MAX Support.",
|
||||
ErrorCode = "eag",
|
||||
ShowInstructions = false
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
namespace maintenance.Services.Pages;
|
||||
|
||||
public sealed class PageContentModel
|
||||
{
|
||||
public string ErrorName { get; set; } = "System Error";
|
||||
public string? ShortDescription { get; set; }
|
||||
public string Description { get; set; } = "A system error has occurred. Please contact support.";
|
||||
public string? InstructionsHeader { get; set; }
|
||||
public List<string> InstructionSteps { get; set; } = [];
|
||||
public bool ShowInstructions { get; set; } = true;
|
||||
public bool ShowSupport { get; set; } = true;
|
||||
public string SupportEmail { get; set; } = "maxsupport@max.gov";
|
||||
public string SupportPhone { get; set; } = "202-395-6860";
|
||||
public string SupportHours { get; set; } =
|
||||
"8:00AM - 8:00PM Weekdays, 9:00AM - 6:00PM Weekends (ET)";
|
||||
public string ErrorCode { get; set; } = "eag";
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace maintenance.Services.Pages;
|
||||
|
||||
public sealed class ResolvedPage
|
||||
{
|
||||
public string PageKey { get; init; } = string.Empty;
|
||||
public string PageKind { get; init; } = string.Empty;
|
||||
public bool IsDefaultFallback { get; init; }
|
||||
public PageContentModel Content { get; init; } = new();
|
||||
}
|
||||
Reference in New Issue
Block a user