1cb7a8ca4a
Phase 3 — eliminated ApplicationDbContext from all non-exempt controllers, routing all data access through IUnitOfWork. Added IPlainRepository<T> for the four platform entities (Announcement, BannedIp, DashboardTip, ReleaseNote) that intentionally don't extend BaseEntity and therefore can't use the constrained IRepository<T>. Added permanent-exception comments to the 18 controllers that legitimately retain direct DbContext access (Identity infra, cross-tenant platform ops, bulk streaming exports). Phase 4 — added EnforceDataAccessArchitecture() to Program.cs, a startup gate that reflects over every Controller subclass and throws at boot if any non-exempt controller injects ApplicationDbContext. The app cannot start with a violation. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
126 lines
4.5 KiB
C#
126 lines
4.5 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using PowderCoating.Core.Entities;
|
|
using PowderCoating.Core.Interfaces;
|
|
using PowderCoating.Shared.Constants;
|
|
|
|
namespace PowderCoating.Web.Controllers;
|
|
|
|
/// <summary>
|
|
/// SuperAdmin management of banned IP addresses.
|
|
/// Entries here block login before Identity even checks credentials.
|
|
/// </summary>
|
|
[Authorize(Policy = AppConstants.Policies.SuperAdminOnly)]
|
|
public class BannedIpsController : Controller
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
private readonly ILogger<BannedIpsController> _logger;
|
|
|
|
public BannedIpsController(
|
|
IUnitOfWork unitOfWork,
|
|
UserManager<ApplicationUser> userManager,
|
|
ILogger<BannedIpsController> logger)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_userManager = userManager;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>Lists all banned IPs, showing active and expired separately.</summary>
|
|
public async Task<IActionResult> Index()
|
|
{
|
|
var bans = (await _unitOfWork.BannedIps.GetAllAsync())
|
|
.OrderByDescending(b => b.BannedAt)
|
|
.ToList();
|
|
return View(bans);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a new IP ban. Rejects obviously invalid formats but doesn't require
|
|
/// a perfect regex — admins are trusted to enter valid IPs.
|
|
/// </summary>
|
|
[HttpPost, ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Add(string ipAddress, string? reason, DateTime? expiresAt)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(ipAddress))
|
|
{
|
|
TempData["Error"] = "IP address is required.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
ipAddress = ipAddress.Trim();
|
|
|
|
if (!System.Net.IPAddress.TryParse(ipAddress, out _))
|
|
{
|
|
TempData["Error"] = $"'{ipAddress}' is not a valid IP address.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
var existing = await _unitOfWork.BannedIps.FirstOrDefaultAsync(b => b.IpAddress == ipAddress && b.IsActive);
|
|
if (existing != null)
|
|
{
|
|
TempData["Error"] = $"{ipAddress} already has an active ban (added {existing.BannedAt:MMM dd, yyyy}).";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
var currentUser = await _userManager.GetUserAsync(User);
|
|
|
|
await _unitOfWork.BannedIps.AddAsync(new BannedIp
|
|
{
|
|
IpAddress = ipAddress,
|
|
Reason = reason?.Trim(),
|
|
BannedByUserId = currentUser?.Id,
|
|
BannedAt = DateTime.UtcNow,
|
|
ExpiresAt = expiresAt,
|
|
IsActive = true
|
|
});
|
|
await _unitOfWork.CompleteAsync();
|
|
|
|
_logger.LogWarning("IP {IP} banned by {Admin}. Reason: {Reason}", ipAddress, User.Identity?.Name, reason);
|
|
TempData["Success"] = $"{ipAddress} has been banned.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
/// <summary>Lifts a ban immediately by marking IsActive = false.</summary>
|
|
[HttpPost, ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Lift(int id)
|
|
{
|
|
var ban = await _unitOfWork.BannedIps.GetByIdAsync(id);
|
|
if (ban == null)
|
|
{
|
|
TempData["Error"] = "Ban not found.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
ban.IsActive = false;
|
|
await _unitOfWork.CompleteAsync();
|
|
|
|
_logger.LogInformation("IP ban lifted for {IP} by {Admin}", ban.IpAddress, User.Identity?.Name);
|
|
TempData["Success"] = $"Ban on {ban.IpAddress} has been lifted.";
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
/// <summary>Permanently deletes a ban record.</summary>
|
|
[HttpPost, ValidateAntiForgeryToken]
|
|
public async Task<IActionResult> Delete(int id)
|
|
{
|
|
var ban = await _unitOfWork.BannedIps.GetByIdAsync(id);
|
|
if (ban != null)
|
|
{
|
|
await _unitOfWork.BannedIps.DeleteAsync(ban);
|
|
await _unitOfWork.CompleteAsync();
|
|
_logger.LogInformation("IP ban record deleted for {IP} by {Admin}", ban.IpAddress, User.Identity?.Name);
|
|
TempData["Success"] = $"Ban record for {ban.IpAddress} deleted.";
|
|
}
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
|
|
/// <summary>Returns the requesting client's IP so the admin can pre-fill it quickly.</summary>
|
|
public IActionResult MyIp()
|
|
{
|
|
return Json(new { ip = HttpContext.Connection.RemoteIpAddress?.ToString() ?? "unknown" });
|
|
}
|
|
}
|