using Disco.Plugins.ADCompare.Features; using Disco.Services.Plugins; using System; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; namespace Disco.Plugins.ADCompare.WebHandler { public class ADCompareWebHandler : PluginWebHandler { public override ActionResult ExecuteAction(string ActionName) { var action = ActionName != null ? ActionName.ToLower() : ""; switch (action) { case "": case "index": return Index(); case "run": return Run(); case "export": return ExportCsv(); default: return new HttpNotFoundResult(); } } private ActionResult Index() { return HtmlResult(BuildPage(null)); } private ActionResult Run() { var service = new DeviceCompareService(Database); var summary = service.CompareAllDevices(); return HtmlResult(BuildPage(summary)); } private ActionResult ExportCsv() { var service = new DeviceCompareService(Database); var summary = service.CompareAllDevices(); var sb = new StringBuilder(); sb.AppendLine("SerialNumber,ComputerName,DiscoAssignedUser,DiscoAssignedUserName,ADManagedBy,Match,Reason"); foreach (var r in summary.Results.Where(r => !r.IsMatch)) { sb.AppendLine(string.Join(",", CsvEsc(r.SerialNumber), CsvEsc(r.ComputerName), CsvEsc(r.DiscoAssignedUserId), CsvEsc(r.DiscoAssignedUserDisplayName), CsvEsc(r.ADManagedByDisplayName), r.IsMatch.ToString(), CsvEsc(r.MismatchReason))); } var fileName = "AD_ManagedBy_Compare_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv"; HostController.Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + fileName + "\""); return new ContentResult { Content = sb.ToString(), ContentType = "text/csv", ContentEncoding = Encoding.UTF8 }; } private ActionResult HtmlResult(string html) { return new ContentResult { Content = html, ContentType = "text/html", ContentEncoding = Encoding.UTF8 }; } private string CsvEsc(string v) { if (v == null) v = ""; return "\"" + v.Replace("\"", "\"\"") + "\""; } private string H(string v) { if (string.IsNullOrEmpty(v)) return ""; return HttpUtility.HtmlEncode(v); } private string BuildPage(Models.DeviceComparisonSummary summary) { var pluginUrl = "/Plugin/Disco.Plugins.ADCompare"; var sb = new StringBuilder(); sb.Append("
"); sb.Append("Compares the AD computer Managed By field against the Disco Assigned User for domain-joined devices found in AD.
"); sb.Append("| Serial Number | Computer Name | "); sb.Append("Disco Assigned User | AD Managed By | Reason | "); sb.Append("
|---|---|---|---|---|
| " + H(r.SerialNumber) + " | "); sb.Append("" + H(r.ComputerName) + " | "); sb.Append("" + discoUser + " | "); sb.Append("" + adUser + " | "); sb.Append("" + H(r.MismatchReason) + " | "); sb.Append("