Example #1
0
        public static void Create(ResellerObject reseller)
        {
            ADOrganizationalUnit org = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.Password, StaticSettings.PrimaryDC);

            try
            {
            }
            catch (Exception ex)
            {

            }
        }
Example #2
0
        /// <summary>
        /// Creates a new company
        /// </summary>
        /// <param name="company"></param>
        /// <param name="resellerCode"></param>
        public void NewCompany(CompanyObject company, string resellerCode)
        {
            CPDatabase database = null;

            // Rollback class in case something goes wrong we can undo changes
            CloudPanelTransaction events = new CloudPanelTransaction();

            try
            {
                database = new CPDatabase();

                // Generate the company code
                string companyCode = CompanyObject.GenerateCompanyCode(company.CompanyName, company.UseCompanyNameInsteadofCompanyCode);

                // Loop and make sure one does exist or find one that does
                int count = 0;
                for (int i = 0; i < 1000; i++)
                {
                    if (Validation.DoesCompanyCodeExist(companyCode))
                    {
                        this.logger.Info("Tried to create a new company with company code " + companyCode + " but it already existed... trying another code");

                        companyCode = companyCode + count.ToString();
                        count++;
                    }
                    else
                    {
                        company.CompanyCode = companyCode; // Assign company code to object
                        break;
                    }
                }

                // Get the resellers distinguished name
                var resellerDistinguishedName = (from r in database.Companies
                                                 where r.IsReseller
                                                 where r.CompanyCode == resellerCode
                                                 select r.DistinguishedName).First();

                #region Create Organizational Units

                //
                // Create organizational units
                //
                ADOrganizationalUnit adOrg = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);

                // Check if resellers are enabled and create the organizational unit in the correct place
                string newCompanyDistinguishedName = string.Empty;
                if (!StaticSettings.ResellersEnabled)
                    newCompanyDistinguishedName = adOrg.CreateCompany(StaticSettings.HostingOU, company);
                else
                    newCompanyDistinguishedName = adOrg.CreateCompany(resellerDistinguishedName, company);
                events.NewOrganizationalUnitEvent(newCompanyDistinguishedName);
                adOrg.RemoveAuthUsersRights(newCompanyDistinguishedName); // Removes authenticated users from the OU;

                // Create the Exchange OU
                string exchangeOU = adOrg.CreateOU(newCompanyDistinguishedName, "Exchange", company.Domains[0]);
                events.NewOrganizationalUnitEvent(exchangeOU);
                adOrg.RemoveAuthUsersRights(exchangeOU); // Removes authenticated users from the OU;

                // Create the Applications OU
                string applicationsOU = adOrg.CreateOU(newCompanyDistinguishedName, "Applications", company.Domains[0]);
                events.NewOrganizationalUnitEvent(applicationsOU);
                adOrg.RemoveAuthUsersRights(applicationsOU); // Removes authenticated users from the OU;

                // Create the custom users OU if there is one
                string customUsersOU = string.Empty;
                if (!string.IsNullOrEmpty(StaticSettings.UsersOU))
                {
                    customUsersOU = adOrg.CreateOU(newCompanyDistinguishedName, StaticSettings.UsersOU, company.Domains[0]);
                    events.NewOrganizationalUnitEvent(customUsersOU);
                    adOrg.RemoveAuthUsersRights(customUsersOU); // Removes authenticated users from the OU;
                }
                #endregion

                #region Create Security Groups
                //
                // Create Security Groups
                //
                ADGroup adGroup = new ADGroup(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);

                // Create the Admins security group
                adGroup.Create(newCompanyDistinguishedName, "[email protected]" + companyCode, "", true, false);
                events.NewSecurityGroup("[email protected]" + companyCode);

                // Create the All Users security group
                adGroup.Create(newCompanyDistinguishedName, "[email protected]" + companyCode, "", true, false);
                events.NewSecurityGroup("[email protected]" + companyCode);

                // Create the AllTSUsers security groups
                adGroup.Create(newCompanyDistinguishedName, "[email protected]" + companyCode, "", true, false);
                events.NewSecurityGroup("[email protected]" + companyCode);

                // Add AllTSUsers to the [email protected] group
                adGroup.AddMember("[email protected]", "[email protected]" + companyCode, "name");

                // Check the GPOAccess and see if we are using resellers or not. Then add the group to the GPOAccess security group
                if (StaticSettings.ResellersEnabled)
                {
                    adGroup.AddMember("[email protected]"+ resellerCode, "[email protected]" + companyCode, "name");
                }
                else
                {
                    adGroup.AddMember("[email protected]", "[email protected]" + companyCode, "name");
                }
                #endregion

                #region Add read rights to organizational units
                //
                // Now add read rights
                //
                adOrg.AddAccessRights(newCompanyDistinguishedName, "[email protected] " + companyCode);
                adOrg.AddAccessRights(exchangeOU, "[email protected]" + companyCode);
                adOrg.AddAccessRights(applicationsOU, "[email protected]" + companyCode);

                if (!string.IsNullOrEmpty(StaticSettings.UsersOU))
                    adOrg.AddAccessRights(customUsersOU, "[email protected]" + companyCode);

                #endregion

                // Insert into database
                Company newCompanyDb = new Company();
                newCompanyDb.IsReseller = false;
                newCompanyDb.CompanyName = company.CompanyName;
                newCompanyDb.CompanyCode = company.CompanyCode;
                newCompanyDb.ResellerCode = resellerCode;
                newCompanyDb.Street = company.Street;
                newCompanyDb.City = company.City;
                newCompanyDb.State = company.State;
                newCompanyDb.ZipCode = company.ZipCode;
                newCompanyDb.Country = company.Country;
                newCompanyDb.PhoneNumber = company.Telephone;
                newCompanyDb.AdminName = company.AdminName;
                newCompanyDb.AdminEmail = company.AdminEmail;
                newCompanyDb.DistinguishedName = newCompanyDistinguishedName;
                newCompanyDb.Created = DateTime.Now;

                database.Companies.Add(newCompanyDb);
                database.SaveChanges();
                events.InsertCompanyToDatabase(newCompanyDb.CompanyCode);

                // Insert domain into database
                Domain newDomain = new Domain();
                newDomain.CompanyCode = newCompanyDb.CompanyCode;
                newDomain.Domain1 = company.Domains[0];
                newDomain.IsAcceptedDomain = false;
                newDomain.IsLyncDomain = false;
                newDomain.IsSubDomain = false;

                database.Domains.Add(newDomain);
                database.SaveChanges();

                // Notify success
                ThrowEvent(AlertID.SUCCESS, "Successfully created new company " + newCompanyDb.CompanyName);
            }
            catch (Exception ex)
            {
                ThrowEvent(AlertID.FAILED, ex.Message);

                // Rollback on error
                events.RollBack();
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
        private void Delete_OrganizationalUnit(string distinguishedName)
        {
            ADOrganizationalUnit org = null;

            try
            {
                this.logger.Warn("Rolling back action... Deleting organizational unit " + distinguishedName);

                org = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                org.DeleteOU(distinguishedName);

                this.logger.Warn("Successfully deleted " + distinguishedName);
            }
            catch(Exception ex)
            {
                this.logger.Error("Failed to roll back action... Deleting organizational unit " + distinguishedName, ex);
            }
        }
Example #4
0
        /// <summary>
        /// Creates a new reseller
        /// </summary>
        /// <param name="reseller"></param>
        /// <param name="baseOrganizationalUnit"></param>
        public void NewReseller(ResellerObject reseller, string baseOrganizationalUnit)
        {
            CPDatabase database = null;

            // Rollback class in case something goes wrong we can undo changes
            CloudPanelTransaction events = new CloudPanelTransaction();

            try
            {
                database = new CPDatabase();

                // Generate the company code
                string companyCode = ResellerObject.GenerateCompanyCode(reseller.CompanyName);

                // Loop and make sure one does exist or find one that does
                int count = 0;
                for (int i = 0; i < 1000; i++)
                {
                    if (Validation.DoesCompanyCodeExist(companyCode))
                    {
                        this.logger.Info("Tried to create a new reseller with company code " + companyCode + " but it already existed... trying another code");

                        companyCode = companyCode + count.ToString();
                        count++;
                    }
                    else
                    {
                        reseller.CompanyCode = companyCode; // Assign company code to object
                        break;
                    }
                }

                // Create the reseller in Active Directory
                ADOrganizationalUnit adOrg = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                string distinguishedName = adOrg.CreateReseller(StaticSettings.HostingOU, reseller);
                events.NewOrganizationalUnitEvent(distinguishedName);

                // Create the security group
                ADGroup adGroup = new ADGroup(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                adGroup.Create(distinguishedName, "[email protected]" + companyCode, "", true, false);
                events.NewSecurityGroup("[email protected]" + companyCode);

                // Add the new group to the [email protected] group for group policy permissions
                adGroup.AddMember("[email protected]", "[email protected]" + companyCode, "name");

                // Add access rights
                adOrg.AddAccessRights(distinguishedName, "[email protected]" + companyCode);

                // Insert into database
                Company company = new Company();
                company.IsReseller = true;
                company.CompanyName = reseller.CompanyName;
                company.CompanyCode = companyCode;
                company.Street = reseller.Street;
                company.City = reseller.City;
                company.State = reseller.State;
                company.ZipCode = reseller.ZipCode;
                company.Country = reseller.Country;
                company.PhoneNumber = reseller.Telephone;
                company.AdminName = reseller.AdminName;
                company.AdminEmail = reseller.AdminEmail;
                company.Created = DateTime.Now;
                company.DistinguishedName = distinguishedName;

                database.Companies.Add(company);
                database.SaveChanges();

                // Notify success
                ThrowEvent(AlertID.SUCCESS, "Successfully created new reseller " + reseller.CompanyName);
            }
            catch (Exception ex)
            {
                ThrowEvent(AlertID.FAILED, ex.Message);

                // Rollback on error
                events.RollBack();
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
        private void Delete_Domain(string distinguishedName, string domainName)
        {
            ADOrganizationalUnit org = null;

            try
            {
                this.logger.Warn("Rolling back action... Deleting domain from " + distinguishedName);

                org = new ADOrganizationalUnit(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                org.RemoveDomain(distinguishedName, domainName);

                this.logger.Warn("Successfully removed domain " + domainName + " from " + distinguishedName + " and all child OU's");
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to roll back action... Deleting domain from " + distinguishedName, ex);
            }
        }