示例#1
0
        /// <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();
            }
        }