public void RunWorkflow() { WorkflowResult finalResult = new WorkflowResult(); String LastItemRun = ""; int LastRunResultID = 0; try { RunWorkflow run = db.RunWorkflows.Where(r => r.RunID == RunID).FirstOrDefault(); run.StartTime = DateTime.Now; db.SaveChanges(); List <vwRunItem> items = db.vwRunItems.Where(i => i.RunID == RunID).OrderBy(i => i.RunOrder).ToList(); RunPayload pl = run.RunPayloads.FirstOrDefault(); foreach (vwRunItem item in items) { DateTime StartTime = DateTime.Now; LibraryItem li = db.LibraryItems.Where(l => l.ItemID == item.ItemID).FirstOrDefault(); if (!String.IsNullOrEmpty(item.HtmlAnswers)) { pl = util.SetPayload(RunID, li.ItemID, item.HtmlAnswers); } LastItemRun = li.ItemName; WorkflowItem wfi = db.WorkflowItems.Where(w => w.ItemID == item.ItemID).FirstOrDefault(); RunResult rr = new RunResult { ResultString = "", RunID = RunID, ResultID = 1, TimeStarted = DateTime.Now, WFItemID = wfi.WFItemID }; db.RunResults.Add(rr); db.SaveChanges(); LastRunResultID = rr.RunResultID; Assembly assembly = Assembly.LoadFrom(li.LibraryPath); Type type = assembly.GetType("ItemToRun.RunWorkflowItem"); object instanceOfMyType = Activator.CreateInstance(type); var result = type.InvokeMember("RunItem", BindingFlags.Default | BindingFlags.InvokeMethod, null, instanceOfMyType, new Object[] { run.EmpID, pl == null ? new RunPayload() : pl }); IEnumerable <PropertyInfo> props = result.GetType().GetRuntimeProperties(); int ResultID = Convert.ToInt32(props.ElementAt(0).GetValue(result, null)); String ResultText = props.ElementAt(1).GetValue(result, null).ToString(); DateTime TimeDone = Convert.ToDateTime(props.ElementAt(2).GetValue(result, null)); RunResult rrDone = db.RunResults.Where(r => r.RunResultID == rr.RunResultID).FirstOrDefault(); rrDone.TimeCompleted = DateTime.Now; rrDone.ResultID = ResultID; rrDone.ResultString = ResultText; db.SaveChanges(); finalResult = new WorkflowResult { Success = true, ResultString = "" }; } } catch (Exception ex) { finalResult = new WorkflowResult { Success = false, ResultString = String.Format("There was an error during the run in the {0} item.", LastItemRun), FullError = ex }; RunResult rrError = db.RunResults.Where(r => r.RunResultID == LastRunResultID).FirstOrDefault(); rrError.ResultID = 4; rrError.ResultString = String.Format("Error: {0}", ex.Message); rrError.TimeCompleted = DateTime.Now; db.SaveChanges(); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String ou = util.GetParam("DisabledUsersOU", "Active Directory Disabled Users OU path"); String defCN = ""; RunWorkflow wf = db.RunWorkflows.Where(r => r.EmpID == EmpID && r.RunPayloads.Count() > 0).OrderByDescending(r => r.StartTime).FirstOrDefault(); if (wf != null) { RunPayloadItem i = (from rpl in wf.RunPayloads select rpl.RunPayloadItems.Where(pli => pli.ElementID == "UserOUPath").FirstOrDefault()).FirstOrDefault(); if (i != null) { defCN = i.ElementValue; } } if (defCN == "") { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", "The user's original OU could not be found. Please move them manually from the disabled OU."), TimeDone = DateTime.Now }); } PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } Boolean isInOUAlready = false; DirectoryEntry userOU = new DirectoryEntry("LDAP://" + user.DistinguishedName, adminName, password); DirectoryEntry defaultCN = new DirectoryEntry(defCN, adminName, password); String OUCheckUser = userOU.Path.ToString(); if (OUCheckUser.EndsWith(defCN.Replace("LDAP://", ""))) { isInOUAlready = true; } if (isInOUAlready == true) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} is already in the default Users container.", emp.Username), TimeDone = DateTime.Now }); } userOU.MoveTo(defaultCN); userOU.Close(); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1} was moved to the default Users container in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }
public Utilities.ItemRunResult RunItem(int EmpID, RunPayload RunPayload) { DataLayer.EPSEntities db = new DataLayer.EPSEntities(); Utilities util = new Utilities(); try { Employee emp = db.Employees.Where(e => e.EmpID == EmpID).FirstOrDefault(); String domain = util.GetParam("ADDomain", "Active Directory domain"); String adminName = util.GetParam("ADUsername", "Active Directory admin user"); String password = util.GetParam("ADPassword", "Active Directory admin user password"); String ou = util.GetParam("DisabledUsersOU", "Active Directory Disabled Users OU path"); String myName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; LibraryItem li = db.LibraryItems.Where(l => l.LibraryPath.EndsWith(myName + ".dll")).FirstOrDefault(); PrincipalContext context = new PrincipalContext(ContextType.Domain, domain, adminName, password); UserPrincipal user = UserPrincipal.FindByIdentity(context, emp.Username); if (user == null) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("{0} could not be found in Active Directory.", emp.Username), TimeDone = DateTime.Now }); } Boolean isInOUAlready = false; DirectoryEntry userOU = new DirectoryEntry("LDAP://" + user.DistinguishedName, adminName, password); DirectoryEntry disabledOU = new DirectoryEntry("LDAP://" + ou, adminName, password); String OUCheckUser = userOU.Path.ToString(); if (OUCheckUser.EndsWith(disabledOU.Path.Replace("LDAP://", ""))) { isInOUAlready = true; } if (isInOUAlready == true) { return(new Utilities.ItemRunResult { ResultID = 5, ResultText = String.Format("{0} is already in the Disabled Users OU.", emp.Username), TimeDone = DateTime.Now }); } String plPath = userOU.Path.Replace(String.Format("CN={0} {1},", emp.FirstName, emp.LastName), ""); userOU.MoveTo(disabledOU); userOU.Close(); String htmlAnswers = String.Format("{1}:UserOUPath={0}", plPath, li.ItemID); RunWorkflow run = db.RunWorkflows.Where(r => r.EmpID == EmpID).OrderByDescending(r => r.StartTime).FirstOrDefault(); util.SetPayload(run.RunID, li.ItemID, htmlAnswers); return(new Utilities.ItemRunResult { ResultID = 2, ResultText = String.Format("{0} {1} was moved to the Disabled Users OU in Active Directory.", emp.FirstName, emp.LastName), TimeDone = DateTime.Now }); } catch (Exception ex) { return(new Utilities.ItemRunResult { ResultID = 4, ResultText = String.Format("Error: {0}", ex.Message), TimeDone = DateTime.Now }); } }