public void Delete(string resellerCode) { OrganizationalUnits organizationalUnits = null; try { if (string.IsNullOrEmpty(resellerCode)) throw new MissingFieldException("", "ResellerCode"); organizationalUnits = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); // Find the resellers from SQL var reseller = (from c in db.Companies where c.IsReseller where c.CompanyCode == resellerCode select c).First(); if (reseller == null) throw new ArgumentNullException(resellerCode); else { // See if any companies belong to the reseller var companyCount = (from c in db.Companies where c.ResellerCode == resellerCode select c).Count(); if (companyCount > 0) throw new Exception("Unable to delete reseller because it contains companies."); organizationalUnits.Delete(reseller.DistinguishedName, true); db.Companies.Remove(reseller); db.SaveChanges(); log.InfoFormat("Successfully deleted reseller {0}.", reseller.CompanyName); } } catch (Exception ex) { log.ErrorFormat("Error deleting reseller: {0}", ex.ToString()); throw; } finally { if (organizationalUnits != null) organizationalUnits.Dispose(); } }
/// <summary> /// Deletes a company. Does not check for existing users. Is destructive /// </summary> /// <param name="companyCode"></param> public void Delete(string companyCode) { OrganizationalUnits organizationalUnits = null; try { if (string.IsNullOrEmpty(companyCode)) throw new MissingFieldException("", "CompanyCode"); organizationalUnits = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); // Find the resellers from SQL var company = (from c in db.Companies where c.IsReseller where c.CompanyCode == companyCode select c).First(); if (company == null) throw new ArgumentNullException(companyCode); else { // SAFE DELETE OFF! DESTRUCTIVE! WILL DELETE ALL USERS AND OBJECTS IN COMPANY!! organizationalUnits.Delete(company.DistinguishedName, false); // Remove all companiesby calling stored procedure db.spDeleteCompany(company.CompanyCode); log.InfoFormat("Successfully deleted company {0}.", company.CompanyName); } } catch (Exception ex) { log.ErrorFormat("Error deleting company: {0}", ex.ToString()); throw; } finally { if (organizationalUnits != null) organizationalUnits.Dispose(); } }
private void RollbackADAction(Actions action, object[] attribute) { OrganizationalUnits org = null; Groups grp = null; Users usr = null; try { log.DebugFormat("Rolling back action {0}...", action.ToString()); switch (action) { case Actions.CreateOrganizationalUnit: org = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); org.Delete(attribute[0].ToString(), true); log.DebugFormat("Successfully rolled back action {0} at path {1}", action.ToString(), attribute.ToString()); break; case Actions.CreateSecurityGroup: grp = new Groups(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); grp.Delete(attribute[0].ToString()); log.DebugFormat("Successfully rolled back action {0} for group {1}", action.ToString(), attribute.ToString()); break; case Actions.AddDomains: org = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); org.RemoveDomains(attribute[0].ToString(), new string[] { attribute[1].ToString() }); log.DebugFormat("Successfully rolled back action {0} for org {1}", action.ToString(), attribute[0]); break; default: log.DebugFormat("Unknown action {0}... Skipping...", action.ToString()); break; } } catch (Exception ex) { log.ErrorFormat("Failed to rollback action {0}. Exception: {1}", action.ToString(), ex.ToString()); } finally { if (usr != null) usr.Dispose(); if (grp != null) grp.Dispose(); if (org != null) org.Dispose(); } }