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) { var action = ActionName != null ? ActionName.ToLower() : ""; switch (action) { 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(",", CsvEscape(r.SerialNumber), CsvEscape(r.ComputerName), CsvEscape(r.DiscoAssignedUserId), CsvEscape(r.DiscoAssignedUserDisplayName), CsvEscape(r.ADManagedByUserId), CsvEscape(r.ADManagedByDisplayName), r.IsMatch.ToString(), CsvEscape(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 string CsvEscape(string v) { if (v == null) v = ""; return "\"" + v.Replace("\"", "\"\"") + "\""; } private string BuildDashboardHtml() { var pluginUrl = Url.Action(MVC.API.Disco.Plugin.PluginWebAction(Manifest.Id, null)); return @"
Compares the AD computer Managed By field against the Disco Assigned User.