Esempio n. 1
0
        /// <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 Update(OrganizationalUnit org)
        {
            try
            {
                if (string.IsNullOrEmpty(org.DistinguishedName))
                    throw new MissingFieldException("OrganizationalUnit", "DistinguishedName");

                if (string.IsNullOrEmpty(org.DisplayName))
                    throw new MissingFieldException("OrganizationalUnit", "DisplayName");

                log.DebugFormat("Updating organizational unit {0}", org.DistinguishedName);

                de = GetDirectoryEntry(org.DistinguishedName);

                // Update available properties
                if (!string.IsNullOrEmpty(org.DisplayName))
                    de.Properties["displayName"].Value = org.DisplayName;

                if (!string.IsNullOrEmpty(org.Description))
                    de.Properties["Description"].Value = org.Description;
                else
                    de.Properties["Description"].Clear();

                if (!string.IsNullOrEmpty(org.Street))
                    de.Properties["street"].Value = org.Street;
                else
                    de.Properties["street"].Clear();

                if (!string.IsNullOrEmpty(org.City))
                    de.Properties["l"].Value = org.City;
                else
                    de.Properties["l"].Clear();

                if (!string.IsNullOrEmpty(org.State))
                    de.Properties["st"].Value = org.State;
                else
                    de.Properties["st"].Clear();

                if (!string.IsNullOrEmpty(org.Country))
                    de.Properties["co"].Value = org.Country;
                else
                    de.Properties["co"].Clear();

                if (!string.IsNullOrEmpty(org.CountryCode))
                    de.Properties["c"].Value = org.CountryCode;
                else
                    de.Properties["c"].Clear();

                de.CommitChanges();
                log.DebugFormat("Successfully updated organizational unit {0}.", org.DistinguishedName);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error updating organizational unit {0}. Exception: {2}", org.DistinguishedName, ex.ToString());
                throw;
            }
        }
Esempio n. 3
0
        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 OrganizationalUnit Create(string parent, OrganizationalUnit newOU)
        {
            DirectoryEntry child = null;

            try
            {
                if (string.IsNullOrEmpty(newOU.Name))
                    throw new MissingFieldException("OrganizationalUnit", "Name");

                log.DebugFormat("Creating new organizational unit {0} in {1}", newOU.Name, parent);

                de = GetDirectoryEntry(parent);
                child = de.Children.Add("OU=" + newOU.Name, "OrganizationalUnit");

                // Add available properties
                log.Debug("Iterating through all the properties to add to the new organizational unit.");
                if (!string.IsNullOrEmpty(newOU.Description))
                    child.Properties["Description"].Value = newOU.Description;

                if (!string.IsNullOrEmpty(newOU.DisplayName))
                    child.Properties["displayName"].Value = newOU.DisplayName;

                if (!string.IsNullOrEmpty(newOU.Street))
                    child.Properties["street"].Value = newOU.Street;

                if (!string.IsNullOrEmpty(newOU.City))
                    child.Properties["l"].Value = newOU.City;

                if (!string.IsNullOrEmpty(newOU.State))
                    child.Properties["st"].Value = newOU.State;

                if (!string.IsNullOrEmpty(newOU.Country))
                    child.Properties["co"].Value = newOU.Country;

                if (!string.IsNullOrEmpty(newOU.CountryCode))
                    child.Properties["c"].Value = newOU.CountryCode;

                if (newOU.UPNSuffixes != null && newOU.UPNSuffixes.Length > 0)
                    foreach (var u in newOU.UPNSuffixes)
                        child.Properties["uPNSuffixes"].Add(u);

                log.Debug("Done going through all the properties. Now saving the organizational unit...");
                child.CommitChanges();
                log.DebugFormat("Successfully saved new organizational unit {0} in {1}... Retrieving new values...", newOU.Name, parent);

                // Set the values to send back
                newOU.DistinguishedName = child.Properties["distinguishedName"].Value.ToString();

                return newOU;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error creating new organizational unit {0} in {1}. Exception: {2}", newOU.Name, parent, ex.ToString());
                throw;
            }
            finally
            {
                if (child != null)
                    child.Dispose();
            }
        }