/// <summary> /// Gets a list of groups for a specific company code /// </summary> /// <param name="companyCode"></param> /// <returns></returns> public static List<ExchangeGroup> GetGroups(string companyCode) { // Initialize SQL SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString); SqlCommand cmd = new SqlCommand("SELECT * FROM DistributionGroups WHERE CompanyCode=@CompanyCode ORDER BY DisplayName", sql); try { // Add parameters cmd.Parameters.AddWithValue("@CompanyCode", companyCode); // Open Connection logger.DebugFormat("Opening connection to retrieve distribution groups"); sql.Open(); List<ExchangeGroup> groups = new List<ExchangeGroup>(); SqlDataReader r = cmd.ExecuteReader(); while (r.Read()) { logger.DebugFormat("Found distribution group {0}", r["DistinguishedName"].ToString()); ExchangeGroup newGroup = new ExchangeGroup(); newGroup.SQLID = int.Parse(r["ID"].ToString()); logger.DebugFormat("Group ID is {0}", newGroup.SQLID); newGroup.DistinguishedName = r["DistinguishedName"].ToString(); logger.DebugFormat("Group distinguished name is {0}", newGroup.DistinguishedName); newGroup.DisplayName = r["DisplayName"].ToString(); logger.DebugFormat("Group Display Name is {0}", newGroup.DisplayName); newGroup.PrimarySmtpAddress = r["Email"].ToString(); logger.DebugFormat("Group email is {0}", newGroup.PrimarySmtpAddress); bool hidden = false; bool.TryParse(r["Hidden"].ToString(), out hidden); newGroup.Hidden = hidden; logger.DebugFormat("Group hidden: {0}", newGroup.Hidden); newGroup.CompanyCode = r["CompanyCode"].ToString(); logger.DebugFormat("Group company code is {0}", newGroup.CompanyCode); groups.Add(newGroup); } r.Close(); r.Dispose(); logger.DebugFormat("Finished getting groups for {0}", companyCode); // Return collection return groups; } catch (Exception ex) { logger.ErrorFormat("Error getting groups for {0}. Error: {1}", companyCode, ex.ToString()); throw; } finally { cmd.Dispose(); sql.Dispose(); } }
/// <summary> /// Begins creating the group /// </summary> private void StartCreatingNewGroup() { ExchCmds powershell = null; ADUsers users = null; try { users = new ADUsers(Config.Username, Config.Password, Config.PrimaryDC); powershell = new ExchCmds(Config.ExchangeURI, Config.Username, Config.Password, Config.ExchangeConnectionType, Config.PrimaryDC); // User input minus the spaces string emailInput = txtEmailAddress.Text.Replace(" ", string.Empty); ExchangeGroup group = new ExchangeGroup(); group.DisplayName = txtDisplayName.Text; group.PrimarySmtpAddress = string.Format("{0}@{1}", emailInput, ddlDomains.SelectedItem.Value); group.SamAccountName = emailInput; group.CompanyCode = CPContext.SelectedCompanyCode; group.ModerationEnabled = cbMustBeApprovedByAModerator.Checked; group.Hidden = cbGroupHidden.Checked; // Check the length of the sAMAccountName (cannot exceed 19 characters) if (group.SamAccountName.Length > 19) { group.SamAccountName = group.SamAccountName.Substring(0, 18); this.logger.Debug("User's sAMAccountName was to long and had to be shortened to: " + group.SamAccountName); } // Compile the sAMAccountName string finalSamAccountName = emailInput; for (int i = 1; i < 999; i++) { if (users.DoesSamAccountNameExist(finalSamAccountName)) { this.logger.Debug("SamAccountName " + finalSamAccountName + " is already in use. Trying to find another account name..."); finalSamAccountName = group.SamAccountName + i.ToString(); // We found a match so we need to increment the number // Make sure the SamAccountName is less than 19 characters if (finalSamAccountName.Length > 19) { finalSamAccountName = finalSamAccountName.Substring(0, 18 - i.ToString().Length) + i.ToString(); // Make sure it isn't above 19 characters this.logger.Debug("New SamAccountName was too long and was trimmed to " + finalSamAccountName); } } else { // No match was found which means we can continue and break out of the loop group.SamAccountName = finalSamAccountName; this.logger.Debug("Found SamAccountName not in use: " + group.SamAccountName); break; } } // Make sure to remove any spaces string ownersString = hfModifiedOwners.Value.Trim(); string membersString = hfModifiedMembership.Value.Trim(); // Our string seperator to split the owners and members information string[] separators = { "||" }; // Collection of owners group.ManagedBy = ownersString.Split(separators, StringSplitOptions.RemoveEmptyEntries); if (group.ManagedBy == null || group.ManagedBy.Length < 1) throw new Exception("You did not select a group owner. There must be at least one group owner to create a distribution group."); // Collection of members group.MembersArray = membersString.Split(separators, StringSplitOptions.RemoveEmptyEntries); if (group.MembersArray == null || group.MembersArray.Length < 1) throw new Exception("You did not select a member. There must be at least one group member to create a distribution group."); // Go through membership approval settings group.JoinRestriction = "Open"; if (rbMembershipApprovalJoinClosed.Checked) group.JoinRestriction = "Closed"; else if (rbMembershipApprovalJoinApproval.Checked) group.JoinRestriction = "ApprovalRequired"; group.DepartRestriction = "Open"; if (rbMembershipApprovalLeaveClosed.Checked) group.DepartRestriction = "Closed"; // // Go through delivery management settings // group.RequireSenderAuthentication = true; if (rbDeliveryManagementInsideOutside.Checked) group.RequireSenderAuthentication = false; // Compile the list of "Allowed senders" (if any) List<string> allowedSenders = new List<string>(); foreach (ListItem li in lstDeliveryManagementRestrict.Items) { if (li.Selected) allowedSenders.Add(li.Value); } if (allowedSenders.Count > 0) group.WhoCanSendToGroup = allowedSenders.ToArray(); // // Go through message approval settings // group.SendModerationNotifications = "Never"; if (rbMessageApprovalNotifyAll.Checked) group.SendModerationNotifications = "Always"; else if (rbMessageApprovalNotifyInternal.Checked) group.SendModerationNotifications = "Internal"; // Compile the list of "Group Moderators" (if any) List<string> groupModerators = new List<string>(); foreach (ListItem li in lstGroupModerators.Items) { if (li.Selected) groupModerators.Add(li.Value); } if (groupModerators.Count > 0) group.GroupModerators = groupModerators.ToArray(); // Compile the list of senders that don't require approval List<string> bypassModerationSenders = new List<string>(); foreach (ListItem li in lstSendersDontRequireApproval.Items) { if (li.Selected) bypassModerationSenders.Add(li.Value); } if (bypassModerationSenders.Count > 0) group.SendersNotRequiringApproval = bypassModerationSenders.ToArray(); // Create group powershell.New_DistributionGroup(group, Retrieve.GetCompanyExchangeOU); // Add group to SQL SQLExchange.AddDistributionGroup("CN=" + group.PrimarySmtpAddress + "," + Retrieve.GetCompanyExchangeOU, CPContext.SelectedCompanyCode, group.DisplayName, group.PrimarySmtpAddress, cbGroupHidden.Checked); } catch (Exception ex) { notification1.SetMessage(controls.notification.MessageType.Error, ex.Message); } finally { if (powershell != null) powershell.Dispose(); // Refresh the distribution group view GetDistributionGroups(); } }
/// <summary> /// Gets a list of distribution groups from the database /// </summary> /// <param name="companyCode"></param> /// <returns></returns> public static List<ExchangeGroup> GetDistributionGroups(string companyCode) { SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString); SqlCommand cmd = new SqlCommand(@"SELECT DistinguishedName, DisplayName, Email, Hidden FROM DistributionGroups WHERE CompanyCode=@CompanyCode ORDER BY DisplayName", sql); List<ExchangeGroup> groups = new List<ExchangeGroup>(); try { // Add parameters cmd.Parameters.AddWithValue("@CompanyCode", companyCode); // Open connection sql.Open(); // Read SqlDataReader r = cmd.ExecuteReader(); while (r.Read()) { ExchangeGroup tmp = new ExchangeGroup(); tmp.DistinguishedName = r["DistinguishedName"].ToString(); tmp.CompanyCode = companyCode; tmp.DisplayName = r["DisplayName"].ToString(); tmp.PrimarySmtpAddress = r["Email"].ToString(); tmp.Hidden = bool.Parse(r["Hidden"].ToString()); groups.Add(tmp); } // Close and dispose r.Close(); r.Dispose(); // Return our object return groups; } catch (Exception ex) { throw; } finally { cmd.Dispose(); sql.Dispose(); } }