/// <summary> /// Updates an existing company in AD and the database /// </summary> /// <param name="existingCompany"></param> public void Update(Company existingCompany) { OrganizationalUnits organizationalUnits = null; try { if (string.IsNullOrEmpty(existingCompany.CompanyCode)) throw new MissingFieldException("Company", "CompanyCode"); organizationalUnits = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); // Find the company from SQL var company = (from c in db.Companies where !c.IsReseller where c.CompanyCode == existingCompany.CompanyCode select c).First(); if (company == null) throw new ArgumentNullException(existingCompany.CompanyCode); else { // Set the new values company.CompanyName = existingCompany.CompanyName; company.AdminName = existingCompany.AdminName; company.AdminEmail = existingCompany.AdminEmail; company.PhoneNumber = existingCompany.PhoneNumber; company.Street = existingCompany.Street; company.City = existingCompany.City; company.State = existingCompany.State; company.ZipCode = existingCompany.ZipCode; company.Country = existingCompany.Country; // Update the OU log.DebugFormat("Updating organizational unit for company {0}", existingCompany.CompanyName); organizationalUnits.Update(new OrganizationalUnit() { DistinguishedName = company.DistinguishedName, DisplayName = company.CompanyName, Description = company.Description, Street = company.Street, City = company.City, State = company.State, PostalCode = company.ZipCode, Country = company.Country }); // Save SQL changes db.SaveChanges(); log.InfoFormat("Successfully updated existing company {0}. New name if changed: {1}", existingCompany.CompanyName, company.CompanyName); } } catch (Exception ex) { log.ErrorFormat("Error updating company: {0}", ex.ToString()); throw; } finally { if (organizationalUnits != null) organizationalUnits.Dispose(); } }
/// <summary> /// Adds a new domain /// </summary> /// <param name="companyCode"></param> /// <param name="domainName"></param> public void AddDomain(string companyCode, string domainName) { if (string.IsNullOrEmpty(companyCode)) throw new MissingFieldException("Companies", "companyCode"); if (string.IsNullOrEmpty(domainName)) throw new MissingFieldException("Companies", "domainName"); // Get company var company = (from c in db.Companies where !c.IsReseller where c.CompanyCode == companyCode select c).First(); // Check if domain exists var domain = (from d in db.Domains where d.Domain1 == domainName select d).Count(); if (domain > 0) throw new Exception("Domain already exists in the system."); // Check for company limits // TODO OrganizationalUnits organizationalUnits = null; ReverseActions reverse = new ReverseActions(); try { organizationalUnits = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); organizationalUnits.AddDomains(company.DistinguishedName, new[] { domainName }); reverse.AddAction(Actions.AddDomains, new object[] { company.DistinguishedName, domainName }); // Add domain to the system Domain newDomain = new Domain(); newDomain.Domain1 = domainName; newDomain.CompanyCode = companyCode; newDomain.IsAcceptedDomain = false; newDomain.IsLyncDomain = false; newDomain.IsDefault = false; db.Domains.Add(newDomain); db.SaveChanges(); } catch (Exception ex) { log.ErrorFormat("Error adding domain {0} to {1}. Error {2}", domainName, companyCode, 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(); } }
/// <summary> /// Creates a new company in AD and the database /// </summary> /// <param name="newCompany"></param> /// <param name="domainName"></param> /// <param name="resellerCode"></param> public void Create(Company newCompany, string domainName, string resellerCode) { OrganizationalUnits organizationalUnits = null; Groups groups = null; ReverseActions reverse = new ReverseActions(); try { if (string.IsNullOrEmpty(domainName)) throw new MissingFieldException("", "DomainName"); if (string.IsNullOrEmpty(resellerCode)) throw new MissingFieldException("", "ResellerCode"); organizationalUnits = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); groups = new Groups(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); // Check if the user provided a company code or not // If they didn't then we will automatically generate one if (string.IsNullOrEmpty(newCompany.CompanyCode)) newCompany.CompanyCode = OtherStatics.FindAvailableCompanyCode(newCompany.CompanyName, this.db); OrganizationalUnit newOrg = new OrganizationalUnit(); newOrg.Name = newCompany.CompanyCode; newOrg.DisplayName = newCompany.CompanyName; newOrg.City = newCompany.City; newOrg.State = newCompany.State; newOrg.PostalCode = newCompany.ZipCode; newOrg.Country = newCompany.Country; newOrg.UPNSuffixes = new[] { domainName }; newOrg.Description = newCompany.Description; var createdCompany = organizationalUnits.Create(Settings.HostingOU, newOrg); reverse.AddAction(Actions.CreateOrganizationalUnit, createdCompany.DistinguishedName); // // Create security groups // string strippedCompanyCode = newCompany.CompanyCode.Replace(" ", string.Empty); // Create Admins@ group SecurityGroup newGroup = new SecurityGroup(); newGroup.Name = string.Format("Admins@", strippedCompanyCode); newGroup.SamAccountName = newGroup.Name.Length > 19 ? newGroup.Name.Substring(0, 18) : newGroup.Name; groups.Create(createdCompany.DistinguishedName, newGroup); reverse.AddAction(Actions.CreateSecurityGroup, newGroup.Name); // Create AllUsers@ group newGroup.Name = string.Format("AllUsers@", strippedCompanyCode); newGroup.SamAccountName = newGroup.Name.Length > 19 ? newGroup.Name.Substring(0, 18) : newGroup.Name; groups.Create(createdCompany.DistinguishedName, newGroup); reverse.AddAction(Actions.CreateSecurityGroup, newGroup.Name); // Create AllTSUsers@ group newGroup.Name = string.Format("AllTSUsers@", strippedCompanyCode); newGroup.SamAccountName = newGroup.Name.Length > 19 ? newGroup.Name.Substring(0, 18) : newGroup.Name; groups.Create(createdCompany.DistinguishedName, newGroup); reverse.AddAction(Actions.CreateSecurityGroup, newGroup.Name); groups.AddGroup("GPOAccess@" + resellerCode, newGroup.Name); // Add group to the GPOAccess group in resellers OU // // Create Exchange and Applications OU // newOrg = new OrganizationalUnit(); newOrg.Name = Settings.ExchangeOU; newOrg.DisplayName = Settings.ExchangeOU; newOrg.UPNSuffixes = new[] { domainName }; var createdOrg = organizationalUnits.Create(createdCompany.DistinguishedName, newOrg); reverse.AddAction(Actions.CreateOrganizationalUnit, createdOrg.DistinguishedName); newOrg = new OrganizationalUnit(); newOrg.Name = Settings.ApplicationsOU; newOrg.DisplayName = Settings.ApplicationsOU; newOrg.UPNSuffixes = new[] { domainName }; createdOrg = organizationalUnits.Create(createdCompany.DistinguishedName, newOrg); reverse.AddAction(Actions.CreateOrganizationalUnit, createdOrg.DistinguishedName); // Add to SQL log.DebugFormat("Saving new company {0} to the database.", newCompany.CompanyName); newCompany.Created = DateTime.Now; newCompany.DistinguishedName = createdCompany.DistinguishedName; newCompany.IsReseller = false; db.Companies.Add(newCompany); db.SaveChanges(); log.InfoFormat("Successfully created new company {0}", newCompany.CompanyName); } catch (Exception ex) { log.ErrorFormat("Error creating new company: {0}", ex.ToString()); reverse.RollbackNow(); throw; } finally { if (groups != null) groups.Dispose(); if (organizationalUnits != null) organizationalUnits.Dispose(); } }
public void Create(Company newReseller) { OrganizationalUnits organizationalUnits = null; Groups groups = null; ReverseActions reverse = new ReverseActions(); try { organizationalUnits = new OrganizationalUnits(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); groups = new Groups(Settings.Username, Settings.DecryptedPassword, Settings.PrimaryDC); // Check if the user provided a company code or not // If they didn't then we will automatically generate one if (string.IsNullOrEmpty(newReseller.CompanyCode)) newReseller.CompanyCode = OtherStatics.FindAvailableCompanyCode(newReseller.CompanyName, this.db); OrganizationalUnit newOrg = new OrganizationalUnit(); newOrg.Name = newReseller.CompanyCode; newOrg.DisplayName = newReseller.CompanyName; newOrg.City = newReseller.City; newOrg.State = newReseller.State; newOrg.PostalCode = newReseller.ZipCode; newOrg.Country = newReseller.Country; newOrg.UPNSuffixes = null; // Do not allow UPNSuffixes on the reseller OU newOrg.Description = newReseller.Description; var createdReseller = organizationalUnits.Create(Settings.HostingOU, newOrg); reverse.AddAction(Actions.CreateOrganizationalUnit, createdReseller.DistinguishedName); // // Create GPOAccess Group // SecurityGroup newGroup = new SecurityGroup(); newGroup.Name = string.Format("GPOAccess@{0}", newReseller.CompanyCode.Replace(" ", string.Empty)); newGroup.SamAccountName = newGroup.Name.Length > 19 ? newGroup.Name.Substring(0, 18) : newGroup.Name; groups.Create(createdReseller.DistinguishedName, newGroup); reverse.AddAction(Actions.CreateSecurityGroup, newGroup.Name); // // Add group to hoster GPOAccess group // groups.AddGroup("GPOAccess@Hosting", newGroup.Name); // Add to SQL log.DebugFormat("Saving new reseller {0} to the database.", newReseller.CompanyName); newReseller.Created = DateTime.Now; newReseller.DistinguishedName = createdReseller.DistinguishedName; newReseller.IsReseller = true; db.Companies.Add(newReseller); db.SaveChanges(); log.InfoFormat("Successfully created new reseller {0}", newReseller.CompanyName); } catch (Exception ex) { log.ErrorFormat("Error creating new reseller: {0}", ex.ToString()); reverse.RollbackNow(); throw; } finally { if (groups != null) groups.Dispose(); if (organizationalUnits != null) organizationalUnits.Dispose(); } }
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(); } }
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(); } }