using Disco.Plugins.ADCompare.Features; using Disco.Services.Plugins; using Newtonsoft.Json; using System; using System.Linq; using System.Text; using System.Web.Mvc; namespace Disco.Plugins.ADCompare.WebHandler { public class ADCompareWebHandler : PluginWebHandler { public override ActionResult ExecuteAction(string ActionName) { switch (ActionName?.ToLower()) { case null: case "": case "index": return Index(); case "compare": return Compare(); case "export": return ExportCsv(); default: return new HttpNotFoundResult(); } } private ActionResult Index() { return new ContentResult { Content = BuildDashboardHtml(), ContentType = "text/html", ContentEncoding = Encoding.UTF8 }; } private ActionResult Compare() { var service = new DeviceCompareService(Database); var summary = service.CompareAllDevices(); return new ContentResult { Content = JsonConvert.SerializeObject(summary, Formatting.Indented), ContentType = "application/json", ContentEncoding = Encoding.UTF8 }; } private ActionResult ExportCsv() { var service = new DeviceCompareService(Database); var summary = service.CompareAllDevices(); var sb = new StringBuilder(); sb.AppendLine("SerialNumber,ComputerName,DiscoAssignedUser,DiscoAssignedUserName,ADManagedByUser,ADManagedByName,Match,Reason"); foreach (var r in summary.Results.Where(r => !r.IsMatch)) { 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))); } 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 Csv(string v) => $"\"{(v ?? "").Replace("\"", "\"\"")}\""; private string BuildDashboardHtml() { var pluginUrl = Url.Action(MVC.API.Disco.Plugin.PluginWebAction(Manifest.Id, null)); return $@"

AD Compare — Device Managed By

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

"; } } }