Exemplo n.º 1
0
        public void Create(CompanyPlanObject plan)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                Plans_Organization newPlan = new Plans_Organization();
                newPlan.OrgPlanName = plan.CompanyPlanName;
                newPlan.MaxUsers = plan.MaxUser;
                newPlan.MaxDomains = plan.MaxDomains;
                newPlan.MaxExchangeMailboxes = plan.MaxExchangeMailboxes;
                newPlan.MaxExchangeContacts = plan.MaxExchangeContacts;
                newPlan.MaxExchangeDistLists = plan.MaxExchangeDistributionGroups;
                newPlan.MaxExchangeResourceMailboxes = plan.MaxExchangeResourceMailboxes;
                newPlan.MaxExchangeMailPublicFolders = plan.MaxExchangeMailPublicFolders;

                database.Plans_Organization.Add(newPlan);
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                this.logger.Error("Error saving new company plan " + plan.CompanyPlanName + " to the database.", ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if a company code exists in the database (reseller or company)
        /// </summary>
        /// <param name="companyCode"></param>
        /// <returns></returns>
        public static bool DoesCompanyCodeExist(string companyCode)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var exists = (from c in database.Companies
                              where c.CompanyCode == companyCode
                              select c.CompanyCode).FirstOrDefault();

                if (!string.IsNullOrEmpty(exists))
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
        public void DeleteContact(string distinguishedName, string companyCode)
        {
            ExchangePowershell powershell = null;
            CPDatabase database = null;

            try
            {
                // Get company distinguished name
                database = new CPDatabase();
                var contact = (from c in database.Contacts
                               where c.DistinguishedName == distinguishedName
                               select c).First();

                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                powershell.DeleteContact(distinguishedName);

                database.Contacts.Remove(contact);
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                    database.Dispose();

                if (powershell != null)
                    powershell.Dispose();
            }
        }
        public MailContactObject GetContact(string distinguishedName)
        {
            CPDatabase database = null;

            try
            {
                // Get all contacts for company
                database = new CPDatabase();

                var contact = (from c in database.Contacts
                               where c.DistinguishedName == distinguishedName
                               orderby c.DisplayName
                               select new MailContactObject()
                               {
                                   DisplayName = c.DisplayName,
                                   CompanyCode = c.CompanyCode,
                                   DistinguishedName = c.DistinguishedName,
                                   Email = c.Email,
                                   Hidden = c.Hidden
                               }).First();

                return contact;
            }
            catch (Exception ex)
            {
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 5
0
        public static void AddAudit(string companyCode, string username, ActionID actionID, string variable1, string variable2 = null)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                Audit newAudit = new Audit();
                newAudit.CompanyCode = companyCode;
                newAudit.Username = username;
                newAudit.Date = DateTime.Now;
                newAudit.ActionID = (int)actionID;
                newAudit.Variable1 = variable1;
                newAudit.Variable2 = variable2;

                database.Audits.Add(newAudit);
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                logger.Info("Failed to add audit to database: " + actionID.ToString(), ex);
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
        public void DisableExchange(string companyCode)
        {
            ExchangePowershell powershell = null;
            CPDatabase database = null;

            try
            {
                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);

                // Disable all exchange objects
                powershell.DeleteAllMailboxes(companyCode);
                powershell.DeleteAllContacts(companyCode);
                powershell.DeleteAllGroups(companyCode);
                powershell.DeleteAddressBookPolicy(companyCode + " ABP");
                powershell.DeleteOfflineAddressBook(companyCode + " OAL");
                powershell.DeleteAddressList(companyCode + " - All Rooms");
                powershell.DeleteAddressList(companyCode + " - All Contacts");
                powershell.DeleteAddressList(companyCode + " - All Groups");
                powershell.DeleteAddressList(companyCode + " - All Users");
                powershell.DeleteGlobalAddressList(companyCode + " - GAL");

                // Get all accepted domains
                this.logger.Debug("Retrieving list of accepted domains for " + companyCode);

                database = new CPDatabase();
                var domains = from d in database.Domains
                              where d.IsAcceptedDomain
                              where d.CompanyCode == companyCode
                              select d;

                if (domains != null)
                {
                    foreach (Domain d in domains)
                        powershell.DeleteDomain(d.Domain1);
                }

                // Now update the database
                int r = database.DisableExchange(companyCode);
                this.logger.Debug("Total count returned when calling DisableExchange stored procedure: " + r.ToString());

            }
            catch (Exception ex)
            {
                this.logger.Error("Error disabling Exchange for company " + companyCode, ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                    database.Dispose();

                if (powershell != null)
                    powershell.Dispose();
            }
        }
Exemplo n.º 7
0
        public bool CreatePlan(MailboxPlanObject obj)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                Plans_ExchangeMailbox plan = new Plans_ExchangeMailbox();
                plan.MailboxPlanName = obj.MailboxPlanName;
                plan.CompanyCode = obj.CompanyCode;
                plan.MailboxPlanDesc = obj.MailboxPlanDescription;
                plan.MaxRecipients = obj.MaxRecipients;
                plan.MaxKeepDeletedItems = obj.MaxKeepDeletedItemsInDays;
                plan.MailboxSizeMB = obj.MailboxSizeInMB;
                plan.MaxMailboxSizeMB = obj.MaxMailboxSizeInMB;
                plan.MaxSendKB = obj.MaxSendInKB;
                plan.MaxReceiveKB = obj.MaxReceiveInKB;
                plan.EnablePOP3 = obj.EnablePOP3;
                plan.EnableIMAP = obj.EnableIMAP;
                plan.EnableOWA = obj.EnableOWA;
                plan.EnableMAPI = obj.EnableMAPI;
                plan.EnableAS = obj.EnableAS;
                plan.EnableECP = obj.EnableECP;
                plan.Cost = obj.Cost;
                plan.Price = obj.Price;
                plan.AdditionalGBPrice = obj.AdditionalGBPrice;

                database.Plans_ExchangeMailbox.Add(plan);
                database.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                this.logger.Error("Error creating new mailbox plan", ex);
                ThrowEvent(AlertID.FAILED, ex.Message);

                return false;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 8
0
        public UsersObject[] Search(string searchResult)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                // Compile a list of companies
                var companies = from c in database.Companies
                                select c;

                // Compile a list of users
                var users = from u in database.Users
                            from c in database.Companies.Where(cc => cc.CompanyCode == u.CompanyCode).DefaultIfEmpty()
                            where u.UserPrincipalName.Contains(searchResult) || u.Firstname.Contains(searchResult) || u.Lastname.Contains(searchResult)
                            select new UsersObject
                            {
                                UserPrincipalName = u.UserPrincipalName,
                                Firstname = u.Firstname,
                                Lastname = u.Lastname,
                                CompanyCode = u.CompanyCode,
                                CompanyName = c.CompanyName,
                                ResellerCode = c.ResellerCode
                            };

                if (users != null)
                    return users.ToArray();
                else
                    return null;
            }
            catch (Exception ex)
            {
                ThrowEvent(AlertID.FAILED, ex.Message);
                this.logger.Error("Error searching for " + searchResult, ex);

                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Gets a specific reseller
        /// </summary>
        /// <param name="companyCode"></param>
        /// <returns></returns>
        public ResellerObject GetReseller(string companyCode)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var reseller = (from r in database.Companies
                                where r.IsReseller
                                where r.CompanyCode == companyCode
                                orderby r.CompanyName
                                select r).FirstOrDefault();

                ResellerObject tmp = new ResellerObject();
                tmp.CompanyID = reseller.CompanyId;
                tmp.CompanyName = reseller.CompanyName;
                tmp.CompanyCode = reseller.CompanyCode;
                tmp.Street = reseller.Street;
                tmp.City = reseller.City;
                tmp.State = reseller.State;
                tmp.ZipCode = reseller.ZipCode;
                tmp.Country = reseller.Country;
                tmp.Telephone = reseller.PhoneNumber;
                tmp.Description = reseller.Description;
                tmp.AdminName = reseller.AdminName;
                tmp.AdminEmail = reseller.AdminEmail;
                tmp.DistinguishedName = reseller.DistinguishedName;
                tmp.Created = reseller.Created;

                return tmp;
            }
            catch (Exception ex)
            {
                ThrowEvent(AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 10
0
        public List<ApplicationsObject> GetCitrixApplications(string companyCode)
        {
            CPDatabase database = null;

               try
               {
               database = new CPDatabase();

               var foundApplications = (from a in database.Plans_Citrix
                                        where a.CompanyCode == null || a.CompanyCode == companyCode
                                        orderby a.Name
                                        select new ApplicationsObject()
                                        {
                                            CitrixPlanID = a.CitrixPlanID,
                                            DisplayName = a.Name,
                                            GroupName = a.GroupName,
                                            Description = a.Description,
                                            IsServer = a.IsServer,
                                            CompanyCode = a.CompanyCode,
                                            Price = a.Price,
                                            Cost = a.Cost,
                                            PictureURL = a.PictureURL
                                        });

               if (foundApplications != null)
                   return foundApplications.ToList();
               else
                   return null;
               }
               catch (Exception ex)
               {
               this.logger.Error("Error retrieving Citrix applications for company " + companyCode, ex);
               ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
               return null;
               }
               finally
               {
               if (database != null)
                   database.Dispose();
               }
        }
Exemplo n.º 11
0
        public List<CompanyPlanObject> GetAllPlans()
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var foundPlans = (from a in database.Plans_Organization
                                  orderby a.OrgPlanName
                                  select new CompanyPlanObject()
                                  {
                                       CompanyPlanID = a.OrgPlanID,
                                       CompanyPlanName = a.OrgPlanName,
                                       MaxUser = a.MaxUsers,
                                       MaxDomains = a.MaxDomains,
                                       MaxExchangeMailboxes = a.MaxExchangeMailboxes,
                                       MaxExchangeContacts = a.MaxExchangeContacts,
                                       MaxExchangeDistributionGroups = a.MaxExchangeDistLists,
                                       MaxExchangeMailPublicFolders = a.MaxExchangeMailPublicFolders,
                                       MaxExchangeResourceMailboxes = a.MaxExchangeResourceMailboxes == null ? 0 : (int)a.MaxExchangeResourceMailboxes
                                  });

                if (foundPlans != null)
                    return foundPlans.ToList();
                else
                    return null;
            }
            catch (Exception ex)
            {
                this.logger.Error("Error retrieving organization plans from the database", ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 12
0
        public void Update(CompanyPlanObject plan)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var findPlan = (from p in database.Plans_Organization
                                where p.OrgPlanID == plan.CompanyPlanID
                                select p).FirstOrDefault();

                findPlan.OrgPlanName = plan.CompanyPlanName;
                findPlan.MaxUsers = plan.MaxUser;
                findPlan.MaxDomains = plan.MaxDomains;
                findPlan.MaxExchangeMailboxes = plan.MaxExchangeMailboxes;
                findPlan.MaxExchangeContacts = plan.MaxExchangeContacts;
                findPlan.MaxExchangeDistLists = plan.MaxExchangeDistributionGroups;
                findPlan.MaxExchangeMailboxes = plan.MaxExchangeMailboxes;
                findPlan.MaxExchangeMailPublicFolders = plan.MaxExchangeMailPublicFolders;

                database.SaveChanges();
            }
            catch (Exception ex)
            {
                this.logger.Error("Error updating company plan " + plan.CompanyPlanName + " with id " + plan.CompanyPlanID, ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 13
0
        public void UpdateUser(UsersObject updateUser, bool isSuperOrResellerAdmin)
        {
            CPDatabase database = null;
            ADGroup ldapGroup = null;
            ADUser ldapUser = null;

            try
            {
                database = new CPDatabase();

                // Get the user from the database
                var foundUser = (from u in database.Users
                                 where u.UserPrincipalName == updateUser.UserPrincipalName
                                 select u).FirstOrDefault();

                if (foundUser == null)
                    ThrowEvent(AlertID.FAILED, "Unknown user " + updateUser.UserPrincipalName);
                else
                {
                    this.logger.Debug("Found user " + foundUser.UserPrincipalName + " in the database. Continuing...");

                    // Update the user values
                    foundUser.Firstname = updateUser.Firstname;
                    foundUser.Middlename = updateUser.Middlename;
                    foundUser.Lastname = updateUser.Lastname;
                    foundUser.DisplayName = updateUser.DisplayName;
                    foundUser.Department = updateUser.Department;

                    // Update user in Active Directory
                    ldapUser = new ADUser(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                    ldapUser.UpdateUser(updateUser, StaticSettings.AllowCustomNameAttribute);

                    // Only update these values if super admin or reseller admin is modifying the user
                    if (isSuperOrResellerAdmin)
                    {
                        this.logger.Debug("Super admin or reseller is updating user so we can check comapny admin permissions and reseller permissions");

                        foundUser.IsCompanyAdmin = updateUser.IsCompanyAdmin;
                        foundUser.IsResellerAdmin = updateUser.IsResellerAdmin;

                        // Get permissions from database
                        var userPermissions = (from p in database.UserPermissions
                                               where p.UserID == foundUser.ID
                                               select p).FirstOrDefault();

                        // If the user is no longer a company admin then remove permissions from the database
                        if (userPermissions != null && !updateUser.IsCompanyAdmin)
                        {
                            this.logger.Debug("User " + updateUser.UserPrincipalName + " is no longer a comapny admin. Need to remove rights from database and security group");

                            database.UserPermissions.Remove(userPermissions);

                            // Remove from Admins@ security group
                            ldapGroup = new ADGroup(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                            ldapGroup.RemoveMember("Admins@" + updateUser.CompanyCode, updateUser.UserPrincipalName, "upn");
                        }
                        else if (userPermissions != null && updateUser.IsCompanyAdmin)
                        {
                            this.logger.Debug("User " + updateUser.UserPrincipalName + " is a company admin. Need to update company admin rights in database.");

                            // If user permissions was found and the user is company admin then update the values
                            userPermissions.EnableExchange = updateUser.EnableExchangePerm;
                            userPermissions.DisableExchange = updateUser.DisableExchangePerm;
                            userPermissions.AddDomain = updateUser.AddDomainPerm;
                            userPermissions.DeleteDomain = updateUser.DeleteDomainPerm;
                            userPermissions.EnableAcceptedDomain = updateUser.EnableAcceptedDomainPerm;
                            userPermissions.DisableAcceptedDomain = updateUser.DisableAcceptedDomainPerm;
                        }
                        else if (userPermissions == null && updateUser.IsCompanyAdmin)
                        {
                            this.logger.Debug("User " + updateUser.UserPrincipalName + " does not have any existing company admin rights. We need to add them to the database.");

                            // No existing permissions were found and we need to add to database
                            userPermissions = new UserPermission();
                            userPermissions.UserID = foundUser.ID;
                            userPermissions.EnableExchange = updateUser.EnableExchangePerm;
                            userPermissions.DisableExchange = updateUser.DisableExchangePerm;
                            userPermissions.AddDomain = updateUser.AddDomainPerm;
                            userPermissions.DeleteDomain = updateUser.DeleteDomainPerm;
                            userPermissions.EnableAcceptedDomain = updateUser.EnableAcceptedDomainPerm;
                            userPermissions.DisableAcceptedDomain = updateUser.DisableAcceptedDomainPerm;
                            database.UserPermissions.Add(userPermissions);

                            // Add to Admins@ security group
                            ldapGroup = new ADGroup(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                            ldapGroup.AddMember("Admins@" + updateUser.CompanyCode, updateUser.UserPrincipalName, "upn");
                        }
                    }
                    else
                        this.logger.Debug("User making changes to " + updateUser.UserPrincipalName + " is not a super admin or reseller admin. We cannot update company admin or reseller admin permissions unless the user making changes is a super or reseller admin.");

                    // Update database
                    database.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                this.logger.Debug("Error updating user " + updateUser.UserPrincipalName, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (ldapUser != null)
                    ldapUser.Dispose();

                if (ldapGroup != null)
                    ldapGroup.Dispose();

                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 14
0
        public UsersObject GetUser(string userPrincipalName)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                // Get from the database
                var findUser = (from u in database.Users
                                from p in database.UserPermissions.Where(m => m.UserID == u.ID).DefaultIfEmpty()
                                where u.UserPrincipalName == userPrincipalName
                                orderby u.DisplayName
                                select new UsersObject()
                                {
                                    UserGuid = u.UserGuid,
                                    CompanyCode = u.CompanyCode,
                                    sAMAccountName = u.sAMAccountName,
                                    UserPrincipalName = u.UserPrincipalName,
                                    Firstname = u.Firstname,
                                    Middlename = u.Middlename,
                                    Lastname = u.Lastname,
                                    DisplayName = u.DisplayName,
                                    Department = u.Department,
                                    DistinguishedName = u.DistinguishedName,
                                    Created = u.Created,
                                    IsEnabled = u.IsEnabled == null ? true : (bool)u.IsEnabled,
                                    IsCompanyAdmin = u.IsCompanyAdmin == null ? false : (bool)u.IsCompanyAdmin,
                                    IsResellerAdmin = u.IsResellerAdmin == null ? false : (bool)u.IsResellerAdmin,
                                    MailboxPlan = u.MailboxPlan == null ? 0 : (int)u.MailboxPlan,
                                    AdditionalMB = u.AdditionalMB == null ? 0 : (int)u.AdditionalMB,
                                    ActiveSyncPlan = u.ActiveSyncPlan == null ? 0 : (int)u.ActiveSyncPlan,
                                    EnableExchangePerm = p.EnableExchange == null ? false : p.EnableExchange,
                                    DisableExchangePerm = p.DisableExchange == null ? false : p.DisableExchange,
                                    AddDomainPerm = p.AddDomain == null ? false : p.AddDomain,
                                    DeleteDomainPerm = p.DeleteDomain == null ? false : p.DeleteDomain,
                                    EnableAcceptedDomainPerm = p.EnableAcceptedDomain == null ? false : p.EnableAcceptedDomain,
                                    DisableAcceptedDomainPerm = p.DisableAcceptedDomain == null ? false : p.DisableAcceptedDomain
                                }).FirstOrDefault();

                return findUser;
            }
            catch (Exception ex)
            {
                this.logger.Error("Failed to retrieve user " + userPrincipalName, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 15
0
        public MailboxPlanObject GetMailboxPlan(int planID)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var foundPlan = (from p in database.Plans_ExchangeMailbox
                                  where p.MailboxPlanID == planID
                                  orderby p.MailboxPlanName
                                  select new MailboxPlanObject()
                                  {
                                      MailboxPlanID = p.MailboxPlanID,
                                      MailboxPlanName = p.MailboxPlanName,
                                      MailboxPlanDescription = p.MailboxPlanDesc,
                                      CompanyCode = p.CompanyCode,
                                      Cost = string.IsNullOrEmpty(p.Cost) ? "0.00" : p.Cost,
                                      Price = string.IsNullOrEmpty(p.Price) ? "0.00" : p.Price,
                                      AdditionalGBPrice = string.IsNullOrEmpty(p.AdditionalGBPrice) ? "0.00" : p.AdditionalGBPrice,
                                      MailboxSizeInMB = p.MailboxSizeMB,
                                      MaxMailboxSizeInMB = p.MaxMailboxSizeMB == null ? p.MailboxSizeMB : (int)p.MaxMailboxSizeMB,
                                      MaxSendInKB = p.MaxSendKB,
                                      MaxReceiveInKB = p.MaxReceiveKB,
                                      MaxRecipients = p.MaxRecipients,
                                      EnablePOP3 = p.EnablePOP3,
                                      EnableIMAP = p.EnableIMAP,
                                      EnableOWA = p.EnableOWA,
                                      EnableAS = p.EnableAS,
                                      EnableECP = p.EnableECP,
                                      MaxKeepDeletedItemsInDays = p.MaxKeepDeletedItems
                                  }).First();

                return foundPlan;
            }
            catch (Exception ex)
            {
                this.logger.Error("Error trying to retrieve mailbox plan " + planID.ToString(), ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 16
0
        public List<DomainsObject> GetDomains(string companyCode)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var foundDomains = from d in database.Domains
                                   where d.CompanyCode == companyCode
                                   orderby d.Domain1
                                   select new DomainsObject()
                                   {
                                       DomainID = d.DomainID,
                                       CompanyCode = d.CompanyCode,
                                       DomainName = d.Domain1,
                                       IsSubDomain = d.IsSubDomain == null ? false : (bool)d.IsSubDomain,
                                       IsDefault = d.IsDefault,
                                       IsAcceptedDomain = d.IsAcceptedDomain,
                                       TypeOfDomain= d.DomainType == null ? DomainType.Unknown : (DomainType)d.DomainType
                                   };

                if (foundDomains != null)
                    return foundDomains.ToList();
                else
                    return null;
            }
            catch (Exception ex)
            {
                this.logger.Error("Error trying to retrieve domains for company " + companyCode, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 17
0
        public void CreateMailbox(UsersObject user)
        {
            CPDatabase database = null;
            ExchangePowershell powershell = null;

            CloudPanelTransaction transaction = new CloudPanelTransaction();
            try
            {
                database = new CPDatabase();

                // Get the user from the database
                var foundUser = (from u in database.Users
                                 where u.UserPrincipalName == user.UserPrincipalName
                                 select u).FirstOrDefault();

                powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);

                // Get the selected mailbox plan
                MailboxPlanObject mailboxPlan = GetMailboxPlan(user.MailboxPlan);

                // Create new mailbox and register transaction
                powershell.NewMailbox(user);
                transaction.NewMailbox(user.UserPrincipalName);

                // Update the mailbox values
                powershell.UpdateMailbox(user, mailboxPlan);
                powershell.UpdateCASMailbox(user, mailboxPlan);

                // Set litigation hold settings if enabled for litigation hold
                if (user.LitigationHoldEnabled)
                    powershell.NewLitigationHold(user.UserPrincipalName, user.LitigationHoldComment, user.LitigationHoldUrl, user.LitigationHoldDuration);

                // Set archive settings if enabled for archiving
                if (user.ArchivingEnabled && user.ArchivePlan > 0)
                {
                    powershell.NewArchiveMailbox(user);
                    // Set quota on archive
                }

                foundUser.Email = user.PrimarySmtpAddress;
                foundUser.MailboxPlan = user.MailboxPlan;
                foundUser.AdditionalMB = user.SetMailboxSizeInMB - mailboxPlan.MailboxSizeInMB;
                foundUser.ExchArchivePlan = user.ArchivePlan;
                database.SaveChanges();
            }
            catch (Exception ex)
            {
                this.logger.Error("Error creating mailbox for " + user.UserPrincipalName, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);

                transaction.RollBack();
            }
            finally
            {
                if (powershell != null)
                    powershell.Dispose();

                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Commits the current static settings to the database
        /// </summary>
        /// <param name="securityKey"></param>
        public static void CommitSettings(string securityKey)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var newSettings = (from s in database.Settings
                                select s).FirstOrDefault();

                if (newSettings == null)
                {
                    // No settings found.. insert new
                    newSettings = new Setting();

                    #region General

                    newSettings.CompanysName = HostersName;
                    newSettings.ResellersEnabled = ResellersEnabled;

                    #endregion

                    #region Active Directory

                    newSettings.BaseOU = HostingOU;
                    newSettings.UsersOU = UsersOU;
                    newSettings.PrimaryDC = PrimaryDC;
                    newSettings.Username = Username;
                    newSettings.Password = EncryptedPassword;

                    #endregion

                    #region Security Groups

                    newSettings.SuperAdmins = SuperAdmins;
                    newSettings.BillingAdmins = BillingAdmins;

                    #endregion

                    #region Billing

                    #endregion

                    #region Exchange

                    newSettings.ExchangeConnectionType = ExchangeConnectionType;
                    newSettings.ExchangeVersion = ExchangeVersion;
                    newSettings.ExchangeFqdn = ExchangeServer;
                    newSettings.ExchangePFServer = ExchangePublicFolderServer;
                    newSettings.ExchDatabases = ExchangeDatabases;
                    newSettings.PublicFolderEnabled = PublicFoldersEnabled;
                    newSettings.ExchangeSSLEnabled = ExchangeSSLEnabled;

                    #endregion

                    #region Modules

                    newSettings.CitrixEnabled = CitrixEnabled;
                    newSettings.LyncEnabled = LyncEnabled;

                    #endregion

                    #region Other

                    newSettings.LockdownEnabled = LockdownEnabled;
                    newSettings.AllowCustomNameAttrib = AllowCustomNameAttribute;
                    newSettings.IPBlockingEnabled = IPBlockingEnabled;
                    newSettings.IPBlockingFailedCount = IPBlockingFailedCount;
                    newSettings.IPBlockingLockedMinutes = IPBlockingLockedInMinutes;
                    newSettings.CurrencySymbol = CurrencySymbol;

                    #endregion

                    // Insert
                    database.Settings.Add(newSettings);
                    database.SaveChanges();
                }
                else
                {
                    #region General

                    newSettings.CompanysName = HostersName;
                    newSettings.ResellersEnabled = ResellersEnabled;

                    #endregion

                    #region Active Directory

                    newSettings.BaseOU = HostingOU;
                    newSettings.UsersOU = UsersOU;
                    newSettings.PrimaryDC = PrimaryDC;
                    newSettings.Username = Username;
                    newSettings.Password = EncryptedPassword;

                    #endregion

                    #region Security Groups

                    newSettings.SuperAdmins = SuperAdmins;
                    newSettings.BillingAdmins = BillingAdmins;

                    #endregion

                    #region Billing

                    #endregion

                    #region Exchange

                    newSettings.ExchangeConnectionType = ExchangeConnectionType;
                    newSettings.ExchangeVersion = ExchangeVersion;
                    newSettings.ExchangeFqdn = ExchangeServer;
                    newSettings.ExchangePFServer = ExchangePublicFolderServer;
                    newSettings.ExchDatabases = ExchangeDatabases;
                    newSettings.PublicFolderEnabled = PublicFoldersEnabled;
                    newSettings.ExchangeSSLEnabled = ExchangeSSLEnabled;

                    #endregion

                    #region Modules

                    newSettings.CitrixEnabled = CitrixEnabled;
                    newSettings.LyncEnabled = LyncEnabled;

                    #endregion

                    #region Other

                    newSettings.LockdownEnabled = LockdownEnabled;
                    newSettings.AllowCustomNameAttrib = AllowCustomNameAttribute;
                    newSettings.IPBlockingEnabled = IPBlockingEnabled;
                    newSettings.IPBlockingFailedCount = IPBlockingFailedCount;
                    newSettings.IPBlockingLockedMinutes = IPBlockingLockedInMinutes;
                    newSettings.CurrencySymbol = CurrencySymbol;

                    #endregion

                    // Save
                    database.SaveChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 19
0
        public List<MailboxPlanObject> GetMailboxPlans(string companyCode)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var foundPlans = from p in database.Plans_ExchangeMailbox
                                 where p.CompanyCode == companyCode || string.IsNullOrEmpty(p.CompanyCode)
                                 orderby p.MailboxPlanName
                                 select new MailboxPlanObject()
                                 {
                                     MailboxPlanID = p.MailboxPlanID,
                                     MailboxPlanName = p.MailboxPlanName,
                                     MailboxPlanDescription = p.MailboxPlanDesc,
                                     CompanyCode = p.CompanyCode,
                                     Cost = string.IsNullOrEmpty(p.Cost) ? "0.00" : p.Cost,
                                     Price = string.IsNullOrEmpty(p.Price) ? "0.00": p.Price,
                                     AdditionalGBPrice = string.IsNullOrEmpty(p.AdditionalGBPrice) ? "0.00" : p.AdditionalGBPrice,
                                     MailboxSizeInMB = p.MailboxSizeMB,
                                     MaxMailboxSizeInMB = p.MaxMailboxSizeMB == null ? p.MailboxSizeMB : (int)p.MaxMailboxSizeMB
                                 };

                if (foundPlans != null)
                    return foundPlans.ToList();
                else
                    return null;
            }
            catch (Exception ex)
            {
                this.logger.Error("Error trying to retrieve mailbox plans for company " + companyCode, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Retrieves the settings from the database
        /// </summary>
        /// <returns></returns>
        public static void GetSettings(string securityKey)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                Key = securityKey;

                var settings = (from s in database.Settings
                                select s).FirstOrDefault();

                // Populate static settings
                if (settings != null)
                {
                    SecurityKey = securityKey;
                    logger.Debug("Security Key: " + SecurityKey);

                    HostingOU = settings.BaseOU;
                    logger.Debug("Hosting OU: " + HostingOU);

                    PrimaryDC = settings.PrimaryDC;
                    logger.Debug("Domain Controller: " + PrimaryDC);

                    Username = settings.Username;
                    logger.Debug("Username: "******"Users OU: " + UsersOU);

                    SuperAdmins = settings.SuperAdmins;
                    logger.Debug("Super Admins: " + SuperAdmins);

                    BillingAdmins = settings.BillingAdmins;
                    logger.Debug("Billing Admins: " + BillingAdmins);

                    ExchangeServer = settings.ExchangeFqdn;
                    logger.Debug("Exchange Server: " + ExchangeServer);

                    ExchangePublicFolderServer = settings.ExchangePFServer;
                    logger.Debug("Exchange Public Folder Server: " + ExchangePublicFolderServer);

                    ExchangeVersion = settings.ExchangeVersion;
                    logger.Debug("Exchange Version: " + ExchangeVersion.ToString());

                    ExchangeSSLEnabled = settings.ExchangeSSLEnabled;
                    logger.Debug("Exchange SSL Enabled: " +  ExchangeSSLEnabled.ToString());

                    ExchangeConnectionType = settings.ExchangeConnectionType;
                    logger.Debug("Exchange Connection Type: " + ExchangeConnectionType);

                    ExchangeDatabases = settings.ExchDatabases;
                    logger.Debug("Exchange Databases: " + ExchangeDatabases);

                    PublicFoldersEnabled = settings.PublicFolderEnabled;
                    logger.Debug("Public Folders Enabled: " + PublicFoldersEnabled.ToString());

                    CitrixEnabled = settings.CitrixEnabled;
                    logger.Debug("Citrix Enabled: " + CitrixEnabled.ToString());

                    LyncEnabled = settings.LyncEnabled;
                    logger.Debug("Lync Enabled: " + LyncEnabled.ToString());

                    ExchangeEnabled = true;
                    logger.Debug("Exchange Enabled: " + ExchangeEnabled.ToString());

                    HostersName = settings.CompanysName;
                    logger.Debug("Hosters Name: " + HostersName);

                    CurrencySymbol = settings.CurrencySymbol;
                    logger.Debug("Currency Symbol: " + CurrencySymbol);

                    bool isResellersEnabled = false;
                    bool.TryParse(settings.ResellersEnabled.ToString(), out isResellersEnabled);
                    ResellersEnabled = isResellersEnabled;
                    logger.Debug("Resellers Enabled: " + ResellersEnabled.ToString());

                    bool allowCustomAttrib = false;
                    bool.TryParse(settings.AllowCustomNameAttrib.ToString(), out allowCustomAttrib);
                    AllowCustomNameAttribute = allowCustomAttrib;
                    logger.Debug("Allow Custom Name Attribute: " + AllowCustomNameAttribute);

                    CornerLogo = settings.BrandingCornerLogo;
                    logger.Debug("Corner Logo: " + CornerLogo);

                    LoginLogo = settings.BrandingLoginLogo;
                    logger.Debug("Login Logo: " + LoginLogo);

                    bool ipBlockingEnabled = false;
                    bool.TryParse(settings.IPBlockingEnabled.ToString(), out ipBlockingEnabled);
                    IPBlockingEnabled = ipBlockingEnabled;
                    logger.Debug("IP Blocking Enabled: " + IPBlockingEnabled.ToString());

                    int ipBlockingFailedCount = 0;
                    int.TryParse(settings.IPBlockingFailedCount.ToString(), out ipBlockingFailedCount);
                    IPBlockingFailedCount = ipBlockingFailedCount;
                    logger.Debug("IP Blocking Failed Count: " + IPBlockingFailedCount.ToString());

                    int ipBlockingInMinutes = 0;
                    int.TryParse(settings.IPBlockingLockedMinutes.ToString(), out ipBlockingInMinutes);
                    IPBlockingLockedInMinutes = ipBlockingInMinutes;
                    logger.Debug("IP Blocking Locked Out In Minutes: " + IPBlockingLockedInMinutes.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 21
0
        public List<UsersObject> GetUsers(string companyCode)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var foundUsers = from u in database.Users
                                  where u.CompanyCode == companyCode
                                  orderby u.DisplayName
                                  select new UsersObject()
                                  {
                                      CompanyCode = u.CompanyCode,
                                      sAMAccountName = u.sAMAccountName,
                                      UserPrincipalName = u.UserPrincipalName,
                                      DisplayName = u.DisplayName,
                                      Department = u.Department,
                                      IsEnabled = u.IsEnabled == null ? true : (bool)u.IsEnabled,
                                      IsResellerAdmin = u.IsResellerAdmin == null ? false : (bool)u.IsResellerAdmin,
                                      IsCompanyAdmin = u.IsCompanyAdmin == null ? false : (bool)u.IsCompanyAdmin,
                                      MailboxPlan = u.MailboxPlan == null ? 0 : (int)u.MailboxPlan,
                                      LyncPlan = u.LyncPlan == null ? 0 : (int)u.LyncPlan
                                  };

                if (foundUsers != null)
                    return foundUsers.ToList();
                else
                    return null;
            }
            catch(Exception ex)
            {
                this.logger.Error("Failed to retrieve users for company " + companyCode, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 22
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, "Admins@" + companyCode, "", true, false);
                events.NewSecurityGroup("Admins@" + companyCode);

                // Create the All Users security group
                adGroup.Create(newCompanyDistinguishedName, "AllUsers@" + companyCode, "", true, false);
                events.NewSecurityGroup("AllUsers@" + companyCode);

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

                // Add AllTSUsers to the AllTSUsers@Hosting group
                adGroup.AddMember("AllTSUsers@Hosting", "AllTSUsers@" + 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("GPOAccess@"+ resellerCode, "AllTSUsers@" + companyCode, "name");
                }
                else
                {
                    adGroup.AddMember("GPOAccess@Hosting", "AllTSUsers@" + companyCode, "name");
                }
                #endregion

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

                if (!string.IsNullOrEmpty(StaticSettings.UsersOU))
                    adOrg.AddAccessRights(customUsersOU, "AllUsers@" + 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();
            }
        }
Exemplo n.º 23
0
        public void CreateUser(UsersObject newUser)
        {
            CPDatabase database = null;
            ADGroup ldapGroup = null;
            ADUser ldapUser = null;

            CloudPanelTransaction newUserTransaction = new CloudPanelTransaction();
            try
            {
                // Insert into database
                database = new CPDatabase();

                // Make sure the user doesn't already exist
                var foundUser = (from u in database.Users
                                 where u.UserPrincipalName == newUser.UserPrincipalName
                                 select u).FirstOrDefault();

                if (foundUser != null)
                    ThrowEvent(AlertID.FAILED, "User already exists " + newUser.UserPrincipalName);
                else
                {
                    // Get the company's OU where we need to save the user
                    var companyDistinguishedName = (from c in database.Companies
                                                    where !c.IsReseller
                                                    where c.CompanyCode == newUser.CompanyCode
                                                    select c.DistinguishedName).First();

                    // Check if they are using a custom user's OU
                    if (!string.IsNullOrEmpty(StaticSettings.UsersOU))
                        companyDistinguishedName = string.Format("OU={0},{1}", StaticSettings.UsersOU, companyDistinguishedName);

                    ldapUser = new ADUser(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                    UsersObject createdUser = ldapUser.NewUser(newUser, companyDistinguishedName, StaticSettings.AllowCustomNameAttribute);
                    newUserTransaction.NewUser(createdUser.UserPrincipalName);

                    // Add the users to the groups
                    ldapGroup = new ADGroup(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                    ldapGroup.AddMember("AllUsers@" + newUser.CompanyCode, createdUser.UserPrincipalName, "upn");

                    if (newUser.IsCompanyAdmin)
                        ldapGroup.AddMember("Admins@" + newUser.CompanyCode, createdUser.UserPrincipalName, "upn");

                    // Insert into database
                    User sqlUser = new User();
                    sqlUser.UserGuid = createdUser.UserGuid;
                    sqlUser.CompanyCode = createdUser.CompanyCode;
                    sqlUser.sAMAccountName = createdUser.sAMAccountName;
                    sqlUser.UserPrincipalName = createdUser.UserPrincipalName;
                    sqlUser.DistinguishedName = createdUser.DistinguishedName;
                    sqlUser.DisplayName = createdUser.DisplayName;
                    sqlUser.Firstname = createdUser.Firstname;
                    sqlUser.Middlename = createdUser.Middlename;
                    sqlUser.Lastname = createdUser.Lastname;
                    sqlUser.Email = string.Empty;
                    sqlUser.Department = createdUser.Department;
                    sqlUser.IsResellerAdmin = createdUser.IsResellerAdmin;
                    sqlUser.IsCompanyAdmin = createdUser.IsCompanyAdmin;
                    sqlUser.MailboxPlan = 0;
                    sqlUser.TSPlan = 0;
                    sqlUser.LyncPlan = 0;
                    sqlUser.Created = DateTime.Now;
                    sqlUser.AdditionalMB = 0;
                    sqlUser.ActiveSyncPlan = 0;
                    database.Users.Add(sqlUser);

                    // Insert permissions into database
                    if (createdUser.IsCompanyAdmin)
                    {
                        UserPermission newPermissions = new UserPermission();
                        newPermissions.UserID = sqlUser.ID;
                        newPermissions.EnableExchange = createdUser.EnableExchangePerm;
                        newPermissions.DisableExchange = createdUser.DisableExchangePerm;
                        newPermissions.AddDomain = createdUser.AddDomainPerm;
                        newPermissions.DeleteDomain = createdUser.DeleteDomainPerm;
                        newPermissions.EnableAcceptedDomain = createdUser.EnableAcceptedDomainPerm;
                        newPermissions.DisableAcceptedDomain = createdUser.DisableAcceptedDomainPerm;
                        database.UserPermissions.Add(newPermissions);
                    }

                    database.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                ThrowEvent(AlertID.FAILED, ex.Message);

                // Rollback on error
                newUserTransaction.RollBack();
            }
            finally
            {
                if (ldapUser != null)
                    ldapUser.Dispose();

                if (ldapGroup != null)
                    ldapGroup.Dispose();

                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// Gets a list of companies from the database
        /// </summary>
        /// <returns></returns>
        public List<CompanyObject> GetCompanies(string resellerCode)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var companyDb = from r in database.Companies
                                where !r.IsReseller
                                where r.ResellerCode == resellerCode
                                orderby r.CompanyName
                                select r;

                List<CompanyObject> companies = new List<CompanyObject>();

                if (companyDb != null)
                {
                    foreach (var company in companyDb)
                    {
                        var domainsDb = from d in database.Domains
                                        where d.CompanyCode == company.CompanyCode
                                        select d.Domain1;

                        CompanyObject tmp = new CompanyObject();
                        tmp.CompanyID = company.CompanyId;
                        tmp.CompanyName = company.CompanyName;
                        tmp.CompanyCode = company.CompanyCode;
                        tmp.Street = company.Street;
                        tmp.City = company.City;
                        tmp.State = company.State;
                        tmp.ZipCode = company.ZipCode;
                        tmp.Country = company.Country;
                        tmp.Telephone = company.PhoneNumber;
                        tmp.Description = company.Description;
                        tmp.AdminName = company.AdminName;
                        tmp.AdminEmail = company.AdminEmail;
                        tmp.DistinguishedName = company.DistinguishedName;
                        tmp.Created = company.Created;
                        tmp.Domains = domainsDb.ToArray();

                        companies.Add(tmp);
                    }
                }

                return companies;
            }
            catch (Exception ex)
            {
                this.logger.Error("Error retrieving companies for reseller " + resellerCode, ex);

                ThrowEvent(AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 25
0
        private int? NumberOfTimesPlanInUse(int planID)
        {
            CPDatabase database = null;

            try
            {
                this.logger.Debug("Checking if plan is in use: " + planID.ToString());

                database = new CPDatabase();

                var findPlan = (from p in database.Companies
                                where p.OrgPlanID == planID
                                select p).Count();

                return findPlan;
            }
            catch (Exception ex)
            {
                this.logger.Error("Error counting the number of times plan " + planID.ToString() + " is in use.", ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 26
0
        /// <summary>
        /// Gets a specific company
        /// </summary>
        /// <param name="companyCode"></param>
        /// <returns></returns>
        public CompanyObject GetCompany(string companyCode)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                var company = (from c in database.Companies
                               from d in database.Domains.Where(a => a.CompanyCode == c.CompanyCode).DefaultIfEmpty()
                                where !c.IsReseller
                                where c.CompanyCode == companyCode
                                orderby c.CompanyName
                                select new CompanyObject()
                                {
                                    CompanyID = c.CompanyId,
                                    CompanyName = c.CompanyName,
                                    CompanyCode = c.CompanyCode,
                                    Street = c.Street,
                                    City = c.City,
                                    State = c.State,
                                    ZipCode = c.ZipCode,
                                    Country = c.Country,
                                    Telephone = c.PhoneNumber,
                                    Description = c.Description,
                                    AdminName = c.AdminName,
                                    AdminEmail = c.AdminEmail,
                                    DistinguishedName = c.DistinguishedName,
                                    Created = c.Created
                                }).FirstOrDefault();

                var domains = from d in database.Domains
                              where d.CompanyCode == company.CompanyCode
                              select d;

                company.Domains = (from d in domains select d.Domain1).ToArray();

                return company;
            }
            catch (Exception ex)
            {
                this.logger.Error("Error retrieving company " + companyCode, ex);

                ThrowEvent(AlertID.FAILED, ex.Message);
                return null;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 27
0
        public void Delete(int planID)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                // Find out if it is in use
                int? inUseNumber = NumberOfTimesPlanInUse(planID);
                if (inUseNumber != null && inUseNumber > 0)
                    ThrowEvent(Base.Enumerations.AlertID.WARNING, inUseNumber == null ? "-1" : inUseNumber.ToString());
                else
                {
                    var deletePlan = (from p in database.Plans_Organization
                                      where p.OrgPlanID == planID
                                      select p).FirstOrDefault();

                    if (deletePlan != null)
                    {
                        database.Plans_Organization.Remove(deletePlan);
                        database.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.Error("Error deleting company plan " + planID.ToString(), ex);
                ThrowEvent(Base.Enumerations.AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 28
0
        public void DeleteUser(string userPrincipalName)
        {
            CPDatabase database = null;
            ADUser ldapUser = null;

            try
            {
                ldapUser = new ADUser(StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.PrimaryDC);
                ldapUser.DeleteUser(userPrincipalName);

                // Delete from database
                database = new CPDatabase();
                database.DeleteUser(userPrincipalName);
            }
            catch (Exception ex)
            {
                ThrowEvent(AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (ldapUser != null)
                    ldapUser.Dispose();

                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 29
0
        private void AuditLogin(string username, string ipAddress, bool isValidLogin)
        {
            CPDatabase database = null;

            try
            {
                database = new CPDatabase();

                // Audit login
                AuditLogin audit = new AuditLogin();
                audit.IPAddress = ipAddress;
                audit.Username = username;
                audit.LoginStatus = isValidLogin;
                audit.AuditTimeStamp = DateTime.Now;

                database.AuditLogins.Add(audit);
                database.SaveChanges();

                this.logger.Debug(username + "attempted to login to CloudPanel. Is valid login? " + isValidLogin.ToString());
            }
            catch (Exception ex)
            {
                this.logger.Error("Error adding entry to the login audit table.", ex);

                throw;
            }
            finally
            {
                if (database != null)
                    database.Dispose();
            }
        }
Exemplo n.º 30
0
        public void DisableMailbox(string userPrincipalName)
        {
            CPDatabase database = null;
            ExchangePowershell powershell = null;

            try
            {
                database = new CPDatabase();

                // Get the user from the database
                var foundUser = (from u in database.Users
                                 where u.UserPrincipalName == userPrincipalName
                                 select u).FirstOrDefault();

                if (foundUser == null)
                    ThrowEvent(AlertID.FAILED, "Unable to find user " + userPrincipalName);
                else
                {
                    this.logger.Debug("Found user " + foundUser.UserPrincipalName + " in the database. Continuing...");

                    if (foundUser.MailboxPlan < 1)
                        this.logger.Debug("User " + foundUser.UserPrincipalName + " does not have a mailbox. Skipping...");
                    else
                    {
                        powershell = new ExchangePowershell(StaticSettings.ExchangeURI, StaticSettings.Username, StaticSettings.DecryptedPassword, StaticSettings.ExchangeUseKerberos, StaticSettings.PrimaryDC);
                        powershell.DeleteMailbox(foundUser.UserPrincipalName);

                        int r = database.DisableMailbox(foundUser.UserPrincipalName);

                        this.logger.Debug("Returned a total of " + r.ToString() + " records when calling DisableMailbox stored procedure for " + foundUser.UserPrincipalName);
                    }
                }
            }
            catch (Exception ex)
            {
                this.logger.Debug("Error deleting mailbox for " + userPrincipalName, ex);
                ThrowEvent(AlertID.FAILED, ex.Message);
            }
            finally
            {
                if (powershell != null)
                    powershell.Dispose();

                if (database != null)
                    database.Dispose();
            }
        }