예제 #1
0
		/// <summary>
		/// Verify if mail provider quotas will exceed if we will try to add Mail Domain into Hosting Space
		/// </summary>
		/// <param name="packageId">Hosting Space Id</param>
		/// <param name="mailServer">Mail Server instance which is used to get Mail Domain information from.</param>
		/// <param name="domainName">Name of the domain.</param>
		/// <returns>True, if a quota will exceed. Otherwise, false.</returns>
		protected QuotaLimit IsQuotasWillExceed(int packageId, MailServer mailServer, string domainName)
		{
            QuotaLimit result = new QuotaLimit();

		    bool quotaExceeded = false, skipOsDomains = false;
			
            // Get OS domains that are already in Hosting Space
            List<DomainInfo> spaceDomains = ServerController.GetDomains(packageId);
			// Step #1 - do not count mail domain as OS domain if OS domain with such name as mail domain already present in Hosting Space
            foreach (DomainInfo domain in spaceDomains)
            {
                if (domain.DomainName.Equals(domainName))
                {
                    skipOsDomains = true;
                    break;
                }
            }
             
            // in current scenario of importing we import domains until the quota exceeded
            // in other words let's examine whether quota will be exeeded if we will add +1 domain.
            if (skipOsDomains == false)
            {
                quotaExceeded = VerifyIfQuotaWillBeExceeded(packageId, Quotas.OS_DOMAINS, 1);
                result.IsExceeded = quotaExceeded;
                result.Message = "Mail_Domain";
            }
		    // Step #3
            if (quotaExceeded == false)
            {
                quotaExceeded = VerifyIfQuotaWillBeExceeded(packageId, Quotas.MAIL_ACCOUNTS,mailServer.GetAccounts(domainName).Length);
                result.IsExceeded = quotaExceeded;
                result.Message = "Mail_Account";
            }
		    // Step #4
            if (quotaExceeded == false)
            {
                quotaExceeded = VerifyIfQuotaWillBeExceeded(packageId, Quotas.MAIL_GROUPS,mailServer.GetGroups(domainName).Length);
                result.IsExceeded = quotaExceeded;
                result.Message = "Mail_Group";
            }
		    // Step #5
            if (quotaExceeded == false)
            {
                quotaExceeded = VerifyIfQuotaWillBeExceeded(packageId, Quotas.MAIL_LISTS,mailServer.GetLists(domainName).Length);
                result.IsExceeded = quotaExceeded;
                result.Message = "Mail_List";
            }
		    // Return validation result
			return result;
		}
예제 #2
0
        public void ImportItem(int packageId, int itemTypeId, Type itemType,
			ResourceGroupInfo group, string itemName)
        {
            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);
            if (serviceId == 0)
                return;            
            
            // Mail provider
            MailServer mail = new MailServer();
            ServiceProviderProxy.Init(mail, serviceId);

            if (itemType == typeof(MailDomain))
            {
                QuotaLimit result = IsQuotasWillExceed(packageId, mail, itemName);
				//in case quotas will exceed the space - exit the import function
				if (result.IsExceeded)
				{
				   if (result.Message.Equals("Mail_Domain"))
                    {
                        const string exceededQuota = "number of domains";
                        TaskManager.WriteWarning(
                            String.Format(
                                "Unable to import mail domain '{0}'.\r\nHosting Plan quotas will be exceeded. \r\nVerify the following quotas before importing mail domain: {1}."
                                , itemName, exceededQuota
                                )
                            );
                        return;
                    }
                    if (result.Message.Equals("Mail_Account"))
                    {
                        const string exceededQuota = "number of mail accounts";
                        TaskManager.WriteWarning(
                            String.Format(
                                "Unable to import mail domain '{0}'.\r\nHosting Plan quotas will be exceeded. \r\nVerify the following quotas before importing mail domain: {1}."
                                , itemName, exceededQuota
                                )
                            );
                        return;
                    }
                    if (result.Message.Equals("Mail_Group"))
                    {
                        const string exceededQuota = "number of groups";
                        TaskManager.WriteWarning(
                            String.Format(
                                "Unable to import mail domain '{0}'.\r\nHosting Plan quotas will be exceeded. \r\nVerify the following quotas before importing mail domain: {1}."
                                , itemName, exceededQuota
                                )
                            );
                        return;
                    }
                    if (result.Message.Equals("Mail_List"))
                    {
                        const string exceededQuota = "number of mail lists";
                        TaskManager.WriteWarning(
                            String.Format(
                                "Unable to import mail domain '{0}'.\r\nHosting Plan quotas will be exceeded. \r\nVerify the following quotas before importing mail domain: {1}."
                                , itemName, exceededQuota
                                )
                            );
                        return;
                    }

				}

                List<string> domains = new List<string>();
                domains.Add(itemName);
                try
                {
                    domains.AddRange(mail.GetDomainAliases(itemName));
                }
                catch (Exception ex)
                {
					TaskManager.WriteError(ex, "Error importing mail domain - skipped");
                    return;
                }

                MailDomain mailDomain = new MailDomain();
                mailDomain.Name = itemName;
                mailDomain.ServiceId = serviceId;
                mailDomain.PackageId = packageId;
                int mailDomainId = PackageController.AddPackageItem(mailDomain);

                // restore domains
                RestoreDomainsByMail(domains, packageId, mailDomainId);

                // add mail accounts
                try
                {                 
                    
                    MailAccount[] accounts = mail.GetAccounts(itemName);
                    foreach (MailAccount account in accounts)
                    {
                        account.ServiceId = serviceId;
                        account.PackageId = packageId;
                        //get mail account password
                        account.Password = CryptoUtils.Encrypt(account.Password);
                        PackageController.AddPackageItem(account);
                    }
                }
                catch (Exception ex)
                {
					TaskManager.WriteError(ex, "Error importing mail account");
                }

                //add mail aliases (forwardings)
                try
                {
                    MailAlias[] aliases = mail.GetMailAliases(itemName);
                    foreach (MailAlias alias in aliases)
                    {
                        alias.ServiceId = serviceId;
                        alias.PackageId = packageId;
                        PackageController.AddPackageItem(alias);
                    }
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Error importing mail aliases");
                }

                // add mail groups
                try
                {
                    MailGroup[] groups = mail.GetGroups(itemName);
                    foreach (MailGroup mailGroup in groups)
                    {
                        mailGroup.ServiceId = serviceId;
                        mailGroup.PackageId = packageId;
                        PackageController.AddPackageItem(mailGroup);
                    }
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Error importing mail group");
                }

                // add mail lists
                try
                {
                    MailList[] lists = mail.GetLists(itemName);
                    foreach (MailList list in lists)
                    {
                        list.ServiceId = serviceId;
                        list.PackageId = packageId;
                        PackageController.AddPackageItem(list);
                    }
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Error importing mail list");
                }
            }
        }