From cf2c006e3f07d2b41d295ade2ada2d9bddca9e46 Mon Sep 17 00:00:00 2001 From: jessikitty Date: Tue, 21 Apr 2026 21:33:29 +1000 Subject: [PATCH] Rewrite web handler for device managedBy comparison dashboard --- WebHandler/ADCompareWebHandler.cs | 291 +++++++----------------------- 1 file changed, 66 insertions(+), 225 deletions(-) diff --git a/WebHandler/ADCompareWebHandler.cs b/WebHandler/ADCompareWebHandler.cs index efedd1c..090f3f5 100644 --- a/WebHandler/ADCompareWebHandler.cs +++ b/WebHandler/ADCompareWebHandler.cs @@ -1,8 +1,8 @@ using Disco.Plugins.ADCompare.Features; -using Disco.Plugins.ADCompare.Models; using Disco.Services.Plugins; using Newtonsoft.Json; using System; +using System.Linq; using System.Text; using System.Web.Mvc; @@ -20,8 +20,6 @@ namespace Disco.Plugins.ADCompare.WebHandler return Index(); case "compare": return Compare(); - case "compareuser": - return CompareUser(); case "export": return ExportCsv(); default: @@ -29,28 +27,20 @@ namespace Disco.Plugins.ADCompare.WebHandler } } - /// - /// Landing page with a button to run comparison - /// private ActionResult Index() { - var html = BuildIndexPage(); return new ContentResult { - Content = html, + Content = BuildDashboardHtml(), ContentType = "text/html", ContentEncoding = Encoding.UTF8 }; } - /// - /// Run full comparison and return results as JSON - /// private ActionResult Compare() { - var service = new ADCompareService(Database); - var summary = service.CompareAllUsers(); - + var service = new DeviceCompareService(Database); + var summary = service.CompareAllDevices(); return new ContentResult { Content = JsonConvert.SerializeObject(summary, Formatting.Indented), @@ -59,237 +49,88 @@ namespace Disco.Plugins.ADCompare.WebHandler }; } - /// - /// Compare a single user - expects ?userId=DOMAIN\username - /// - private ActionResult CompareUser() - { - var userId = HostController.Request.QueryString["userId"]; - if (string.IsNullOrWhiteSpace(userId)) - { - return new HttpStatusCodeResult(400, "userId parameter required"); - } - - var user = Database.Users.Find(userId); - if (user == null) - { - return new HttpStatusCodeResult(404, "User not found in Disco"); - } - - var service = new ADCompareService(Database); - var result = service.CompareUser(user); - - return new ContentResult - { - Content = JsonConvert.SerializeObject(result, Formatting.Indented), - ContentType = "application/json", - ContentEncoding = Encoding.UTF8 - }; - } - - /// - /// Export comparison results as CSV - /// private ActionResult ExportCsv() { - var service = new ADCompareService(Database); - var summary = service.CompareAllUsers(); - + var service = new DeviceCompareService(Database); + var summary = service.CompareAllDevices(); var sb = new StringBuilder(); - sb.AppendLine("UserId,DisplayName,FoundInAD,ADDisabled,MismatchedFields,Details"); - - foreach (var result in summary.Results) + sb.AppendLine("SerialNumber,ComputerName,DiscoAssignedUser,DiscoAssignedUserName,ADManagedByUser,ADManagedByName,Match,Reason"); + foreach (var r in summary.Results.Where(r => !r.IsMatch)) { - var mismatchFields = result.HasMismatches - ? string.Join("; ", result.Mismatches.ConvertAll(m => m.FieldName)) - : "None"; - - var details = result.HasMismatches - ? string.Join("; ", result.Mismatches.ConvertAll(m => - $"{m.FieldName}: Disco='{m.DiscoValue}' AD='{m.ADValue}'")) - : ""; - - sb.AppendLine($"\"{CsvEscape(result.UserId)}\",\"{CsvEscape(result.DisplayName)}\",{result.UserFoundInAD},{result.ADAccountDisabled},\"{CsvEscape(mismatchFields)}\",\"{CsvEscape(details)}\""); + sb.AppendLine(string.Join(",", + Csv(r.SerialNumber), Csv(r.ComputerName), + Csv(r.DiscoAssignedUserId), Csv(r.DiscoAssignedUserDisplayName), + Csv(r.ADManagedByUserId), Csv(r.ADManagedByDisplayName), + r.IsMatch.ToString(), Csv(r.MismatchReason))); } - - var fileName = $"AD_Compare_{DateTime.Now:yyyyMMdd_HHmmss}.csv"; - - HostController.Response.Headers.Add("Content-Disposition", $"attachment; filename=\"{fileName}\""); - - return new ContentResult - { - Content = sb.ToString(), - ContentType = "text/csv", - ContentEncoding = Encoding.UTF8 - }; + HostController.Response.Headers.Add("Content-Disposition", + $"attachment; filename=\"AD_ManagedBy_Compare_{DateTime.Now:yyyyMMdd_HHmmss}.csv\""); + return new ContentResult { Content = sb.ToString(), ContentType = "text/csv", ContentEncoding = Encoding.UTF8 }; } - private string CsvEscape(string value) - { - if (string.IsNullOrEmpty(value)) return ""; - return value.Replace("\"", "\"\""); - } + private string Csv(string v) => $"\"{(v ?? "").Replace("\"", "\"\"")}\""; - #region HTML Page Builder - - private string BuildIndexPage() + private string BuildDashboardHtml() { var pluginUrl = Url.Action(MVC.API.Disco.Plugin.PluginWebAction(Manifest.Id, null)); - return $@" -
-

AD Compare - User Detail Comparison

-

Compare Active Directory user details against Disco ICT records to identify mismatches.

- -
- - - +
+

AD Compare — Device Managed By

+

Compares the AD computer Managed By field against the Disco Assigned User.

+
+ + +
- -