Ejemplo n.º 1
0
        /// <summary>
        /// Begins the process of creating the mailboxes
        /// </summary>
        private void EnableNewMailboxes(string companyCode)
        {
            ExchCmds powershell = null;

            try
            {
                // Initialize powershell and sql
                powershell = new ExchCmds(Config.ExchangeURI, Config.Username, Config.Password, Config.ExchangeConnectionType, Config.PrimaryDC);

                //
                // Get all the users and validate they selected a user and it won't put the company over their allowed limit
                //
                List<MailboxUser> usersToEnable = GetUsersToEnable();
                if (usersToEnable.Count < 1)
                    throw new Exception("You must select at least one user to enable for a mailbox. You selected zero users.");
                if (SQLLimits.IsCompanyAtMailboxLimit(companyCode, usersToEnable.Count))
                    throw new Exception("You have selected too many users that would have caused your company to have more mailboxes that your company is allowed to have. Selected fewer users and try again.");

                //
                // Get the mailbox plan
                //
                int planId = int.Parse(ddlEnableMailboxPlans.SelectedValue);
                int setSizeInMB = int.Parse(hfMailboxSizeMB.Value);
                                
                MailboxPlan plan = DbSql.Get_MailboxPlan(planId);
                plan.SetSizeInMB = setSizeInMB; // set the size that the user selected



                //
                // Format all the users email addresses based on what they selected
                //
                FormatAllUsersEmail(usersToEnable, ddlDomains.SelectedValue);


                // A string of all our problem email accounts that gave an error when enabling
                string errorEnabling = string.Empty;


                // Now loop through each user
                foreach (MailboxUser user in usersToEnable)
                {
                    if (string.IsNullOrEmpty(user.PrimarySmtpAddress.Split('@')[0]))
                        errorEnabling += user.DisplayName + ", ";
                    else
                    {
                        user.CompanyCode             = companyCode;
                        user.ActiveSyncMailboxPolicy = ddlActiveSyncPlan.SelectedIndex > 0 ? ddlActiveSyncPlan.SelectedItem.Text : null;
                        user.Database                = ddlExchangeDatabases.SelectedIndex > 0 ? ddlExchangeDatabases.SelectedValue : string.Empty;
                        

                        // Enabling the mailbox
                        powershell.Enable_Mailbox(user, plan);

                        //
                        // Insert into SQL
                        //
                        DbSql.Update_UserMailboxInfo(user.UserPrincipalName, plan.PlanID, user.PrimarySmtpAddress, (setSizeInMB - plan.SizeInMB), user.ActiveSyncMailboxPolicy);
                        
                        //
                        // Add to the queue to modify the calendar permissions 10 minutes after it has been enabled
                        //
                        DbSql.Add_DatabaseQueue(Enumerations.TaskType.MailboxCalendarPermissions, user.UserPrincipalName, user.CompanyCode, 10, Enumerations.TaskSuccess.NotStarted);
                        
                    }
                }

                // Update the notification
                if (!string.IsNullOrEmpty(errorEnabling))
                    notification1.SetMessage(controls.notification.MessageType.Warning, "Error enabling the following users because of missing data (normally first or last name): " + errorEnabling);
                else
                    notification1.SetMessage(controls.notification.MessageType.Success, "Successfully enabled " + usersToEnable.Count.ToString() + " user(s).");

            }
            catch (Exception ex)
            {
                notification1.SetMessage(controls.notification.MessageType.Error, ex.Message);

                // Refresh back to the main screen
                GetMailboxUsers();
            }
            finally
            {
                if (powershell != null)
                    powershell.Dispose();

                // Refresh the view even if there is an error
                GetMailboxUsers();
            }
        }